当前位置: 首页>>代码示例>>Python>>正文


Python LassoCV.score方法代码示例

本文整理汇总了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_)
开发者ID:moumitab,项目名称:Biomedical_DataAnalysis,代码行数:33,代码来源:lasso_classification_officeRecords.py

示例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)
开发者ID:rowanv,项目名称:bike-sharing,代码行数:33,代码来源:lasso.py

示例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
开发者ID:kuhanw,项目名称:project,代码行数:33,代码来源:backup.py


注:本文中的sklearn.linear_model.LassoCV.score方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。