本文整理汇总了Python中sklearn.linear_model.LassoCV.score方法的典型用法代码示例。如果您正苦于以下问题:Python LassoCV.score方法的具体用法?Python LassoCV.score怎么用?Python LassoCV.score使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.linear_model.LassoCV
的用法示例。
在下文中一共展示了LassoCV.score方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: len
# 需要导入模块: from sklearn.linear_model import LassoCV [as 别名]
# 或者: from sklearn.linear_model.LassoCV import score [as 别名]
p = 180
K = 10 # K-fold CV
y = y.reshape(n)
alphas = np.exp(np.linspace(np.log(0.01),np.log(10),100)) # Using log-scale
N = len(alphas) # Number of lasso parameters
scores = np.zeros(N)
alpha = np.zeros(N)
from sklearn.linear_model import LassoCV
from sklearn.feature_selection import SelectFromModel
for i in range(N):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
clf = LassoCV(n_alphas = 100, cv = K)
clf = clf.fit(X_train,y_train)
scores[i] = clf.score(X_test,y_test)
alpha[i] = clf.alpha_
scores = np.asarray(scores)
max_score_index = np.argmax(scores)
best_alpha = alpha[max_score_index]
print(best_alpha)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
clf = Lasso(alpha=best_alpha)
#clf = LassoCV(n_alphas = 100, cv = K, precompute='auto', n_jobs=2, normalize='True')
clf = clf.fit(X_train,y_train)
scores = clf.score(X_test,y_test)
print(predictor_var[0])
print("clf.coef_",clf.coef_)
示例2: r2_score
# 需要导入模块: from sklearn.linear_model import LassoCV [as 别名]
# 或者: from sklearn.linear_model.LassoCV import score [as 别名]
y_pred_lasso1.describe()
print lasso1
print 'Lasso R^2 score:'
print r2_score(y_test, y_pred_lasso1)
#0.2604
print 'Lasso Mean Squared Error:'
print mean_squared_error(y_test, y_pred_lasso1)
#24232
print 'Lasso Root Mean Squared Log Error:'
print rmsle(y_test, y_pred_lasso1)
#6.089
#Cross-validate the LASSO-penalized linear regression
lasso2 = LassoCV(cv = 15) #cv specifies the number of cross-validation folds to
lasso2_fit = lasso2.fit(X_train, y_train)
lasso2_path = lasso2.score(X_train, y_train)
#run on each penalty-parameter value
plt.plot(-np.log(lasso2_fit.alphas_),
np.sqrt(lasso2_fit.mse_path_).mean(axis = 1))
plt.ylabel('RMSE (avg. across folds)')
plt.xlabel(r'\$-\\log(\\lambda)\$')
# Indicate the lasso parameter that minimizes the average MSE across
#folds
plt.axvline(-np.log(lasso2_fit.alpha_), color = 'red')
alpha = lasso2_fit.alpha_
lasso3 = Lasso(alpha = alpha)
示例3: precision_score
# 需要导入模块: from sklearn.linear_model import LassoCV [as 别名]
# 或者: from sklearn.linear_model.LassoCV import score [as 别名]
print coef_path_forest_cv.feature_importances_
forest_prediction = coef_path_forest_cv.predict(X)
forest_score = coef_path_forest_cv.score(X,y)
print "Forest_score:%.3g" % forest_score
forest_cv_score = cross_validation.cross_val_score(coef_path_forest_cv, X, y, n_jobs=2, cv=5)
print forest_cv_score
print "########LASSO######"
coef_path_lasso_cv.fit(X,y)
print coef_path_lasso_cv.get_params
print "alphas:"
print coef_path_lasso_cv.alphas_
print "coef_:"
print coef_path_lasso_cv.coef_
lasso_prediction = coef_path_lasso_cv.predict(X)
lasso_score = coef_path_lasso_cv.score(X,y)
print "Lasso_score:%.3g" % lasso_score
#print "Lasso precision:%.3g" % precision_score(y, lasso_predict)
#print "Lasso_confusion matrix:"
#print confusion_matrix(y, lasso_prediction)
lasso_cv_score = cross_validation.cross_val_score(coef_path_lasso_cv, X, y, n_jobs=2, cv=5)
print lasso_cv_score
plt.figure()
plt.hist2d(y, lasso_prediction)
plt.ylabel("Predicted Values")
plt.xlabel("Truth Values")
plt.title("Lasso Linear Regression")
plt.savefig("figures/lasso_predicted_truth.png")
print "#######ELASTIC#####"
coef_path_elastic_cv.fit(X,y)
print coef_path_elastic_cv.get_params