def mtelastic_model(self, X_train, y_train, X_test, y_test): # Multi-task Elastic-Net Regression Model mten_model = MultiTaskElasticNet(alpha=.1918) mten_model.fit(X_train, y_train) y_train_pred = mten_model.predict(X_train) y_test_pred = mten_model.predict(X_test) # To score the model I can either use the .score from sklearn or use the MSE R^2 from the Machine Learning Book print(mten_model.score(X_train, y_train)) print(mten_model.score(X_test, y_test)) print('MSE train: %.6f, MSE test: %.6f' % (mean_squared_error( y_train, y_train_pred), mean_squared_error(y_test, y_test_pred))) print('R^2 train: %.6f, R^2 test: %.6f' % (r2_score(y_train, y_train_pred), r2_score(y_test, y_test_pred)))
#this part is used to calculate the Multi-Task Elastic-net's score when the hyper-parameter is optimal #load necessary libs from sklearn.feature_selection import SelectKBest from sklearn.decomposition import TruncatedSVD from sklearn.linear_model import MultiTaskElasticNet from sklearn.cross_validation import train_test_split #splite dataset to get necessary sub-dataset features_train, features_test, labels_train, labels_test = train_test_split(features_sc,label_scm,test_size=0.33,random_state=42) #pre-process: dimensional reduction(SVD) svd1 = TruncatedSVD(n_components=9,random_state=1).fit(features_train) features_train = svd1.transform(features_train) svd2 = TruncatedSVD(n_components=9,random_state=1).fit(features_test) features_test = svd2.transform(features_test) #do regression mte = MultiTaskElasticNet(alpha=0.000000001,l1_ratio=0.01,random_state=1) mte.fit(features_train,labels_train) print "MultiTaskElasticNet",mte.score(features_test,labels_test) ########################################################################## #All of the codes end. #Thank you!
print "MultiTaskLasso", mtl.score(features_test, labels_test) ###################################################################### #this part is used to calculate the Multi-Task Elastic-net's score when the hyper-parameter is optimal #load necessary libs from sklearn.feature_selection import SelectKBest from sklearn.decomposition import TruncatedSVD from sklearn.linear_model import MultiTaskElasticNet from sklearn.cross_validation import train_test_split #splite dataset to get necessary sub-dataset features_train, features_test, labels_train, labels_test = train_test_split( features_sc, label_scm, test_size=0.33, random_state=42) #pre-process: dimensional reduction(SVD) svd1 = TruncatedSVD(n_components=9, random_state=1).fit(features_train) features_train = svd1.transform(features_train) svd2 = TruncatedSVD(n_components=9, random_state=1).fit(features_test) features_test = svd2.transform(features_test) #do regression mte = MultiTaskElasticNet(alpha=0.000000001, l1_ratio=0.01, random_state=1) mte.fit(features_train, labels_train) print "MultiTaskElasticNet", mte.score(features_test, labels_test) ########################################################################## #All of the codes end. #Thank you!
print "R^2: ", r2 print "\n**********测试MultiTaskElasticNet类**********" # 在初始化MultiTaskElasticNet类时, 指定超参数α和ρ, 默认值分别是1.0和0.5. multiTaskElasticNet = MultiTaskElasticNet(alpha=0.01, l1_ratio=0.7) # 拟合训练集 multiTaskElasticNet.fit(train_X, train_Y) # 打印模型的系数 print "系数:", multiTaskElasticNet.coef_ print "截距:", multiTaskElasticNet.intercept_ print '训练集R2: ', r2_score(train_Y, multiTaskElasticNet.predict(train_X)) # 对于线性回归模型, 一般使用均方误差(Mean Squared Error,MSE)或者 # 均方根误差(Root Mean Squared Error,RMSE)在测试集上的表现来评该价模型的好坏. test_Y_pred = multiTaskElasticNet.predict(test_X) print "测试集得分:", multiTaskElasticNet.score(test_X, test_Y) print "测试集MSE:", mean_squared_error(test_Y, test_Y_pred) print "测试集RMSE:", np.sqrt(mean_squared_error(test_Y, test_Y_pred)) print "测试集R2:", r2_score(test_Y, test_Y_pred) tss, rss, ess, r2 = xss(Y, multiTaskElasticNet.predict(X)) print "TSS(Total Sum of Squares): ", tss print "RSS(Residual Sum of Squares): ", rss print "ESS(Explained Sum of Squares): ", ess print "R^2: ", r2 print "\n**********测试MultiTaskElasticNetCV类**********" # 在初始化MultiTaskElasticNetCV类时, 提供一组备选的α值, MultiTaskElasticNetCV类会帮我们选择一个合适的α值. multiTaskElasticNetCV = MultiTaskElasticNetCV( alphas=[0.01, 0.1, 0.5, 1, 3, 5, 7, 10, 20, 100], cv=5) # 拟合训练集