반응형
Notice
Recent Posts
Recent Comments
Link
관리 메뉴

bro's coding

sklearn.linear_model.Lasso.alpha값에 따른 score변화 관찰 본문

[AI]/python.sklearn

sklearn.linear_model.Lasso.alpha값에 따른 score변화 관찰

givemebro 2020. 4. 19. 21:24
반응형
alphas = [10, 1, 0.1, 0.01, 0.001, 0.0001]
train_scores = []
test_scores = []
ws = []

for alpha in alphas:
    lasso = Lasso(alpha=alpha)
    lasso.fit(X_train, y_train)
    ws.append(lasso.coef_)
    
    s1 = lasso.score(X_train, y_train)
    s2 = lasso.score(X_test, y_test)
    train_scores.append(s1)
    test_scores.append(s2)
    
display(train_scores, test_scores, ws)

 

[0.0,
 0.40725895623295394,
 0.900745787336254,
 0.9279631631543988,
 0.9311666853919297,
 0.9311995291582065]
[-0.015873329420263538,
 0.38141259918310877,
 0.8749239055051604,
 0.9113752481323003,
 0.9233208053315227,
 0.9241299402555914]
[array([ 0., -0.,  0.,  0.]),
 array([ 0.        , -0.        ,  0.11292891,  0.        ]),
 array([ 0.      , -0.      ,  0.405014,  0.      ]),
 array([-0.01103514, -0.        ,  0.2521542 ,  0.45062708]),
 array([-0.05652789, -0.05055522,  0.21445927,  0.58184325]),
 array([-0.06075528, -0.0559519 ,  0.21031884,  0.59543535])]

 

import matplotlib.pyplot as plt
import numpy as np

plt.plot(range(len(alphas)), train_scores)
plt.plot(range(len(alphas)), test_scores)
plt.plot(range(len(alphas)), 10*np.abs(np.array(ws)[:,0]), '--')
plt.xticks(range(len(alphas)),alphas)
plt.legend(['train', 'test', r'$ 10 \times \vert w_0 \vert $'],loc='lower right')
plt.title('Lasso scores for $\\alpha$\'s')
plt.xlabel('alpha')

 

반응형
Comments