無正則的線性回歸模型
l1正則的lasso模型
l2正則的ridge regression(嶺回歸)模型
開方均方誤差(rooted mean squared error,rmse)
平均絕對誤差(mean absolute error,mae)
# 1. 生成學習器例項
lr = linearregression(
)#2. 在訓練集上訓練學習器
lr.fit(x_train, y_train)
#3.用訓練好的學習器對訓練集/測試集進行**
y_train_pred = lr.predict(x_train)
y_test_pred = lr.predict(x_test)
# rmse(開方均方誤差)
rmse_train = np.sqrt(mean_squared_error(y_train,y_train_pred)
)rmse_test = np.sqrt(mean_squared_error(y_test,y_test_pred)
("rmse on training set :"
, rmse_train)
("rmse on test set :"
, rmse_test)
# r2 score
r2_score_train = r2_score(y_train,y_train_pred)
r2_score_test = r2_score(y_test,y_test_pred)
("r2_score on training set :"
, r2_score_train)
("r2_score on test set :"
, r2_score_test)
from sklearn.linear_model import lassocv
from sklearn.metrics import r2_score #評價回歸**模型的效能
#設定超引數搜尋範圍
#alphas = [ 0.01, 0.1, 1, 10,100]
#生成乙個lassocv例項
#lasso = lassocv(alphas=alphas)
lasso = lassocv(
)#訓練(內含cv)
lasso.fit(x_train, y_train)
#測試y_test_pred_lasso = lasso.predict(x_test)
y_train_pred_lasso = lasso.predict(x_train)
#評估,使用r2_score評價模型在測試集和訓練集上的效能
print
('the r2 score of lassocv on test is'
, r2_score(y_test, y_test_pred_lasso)
)print
('the r2 score of lassocv on train is'
, r2_score(y_train, y_train_pred_lasso)
)
from sklearn.linear_model import ridgecv
from sklearn.metrics import r2_score #評價回歸**模型的效能
#設定超引數(正則引數)範圍
alphas =
[0.01
,0.1,1
,10,100
]#n_alphas = 20
#alphas = np.logspace(-5,2,n_alphas)
#生成乙個ridgecv例項
ridge = ridgecv(alphas=alphas, store_cv_values=
true
)#模型訓練
ridge.fit(x_train, y_train)
#**y_test_pred_ridge = ridge.predict(x_test)
y_train_pred_ridge = ridge.predict(x_train)
#評估,使用r2_score評價模型在測試集和訓練集上的效能
print
('the r2 score of ridgecv on test is'
, r2_score(y_test, y_test_pred_ridge)
)print
('the r2 score of ridgecv on train is'
, r2_score(y_train, y_train_pred_ridge)
)
from sklearn.metrics import mean_squared_error
rmse_train = np.sqrt(mean_squared_error(y_train,y_train_pred)
)rmse_test = np.sqrt(mean_squared_error(y_test,y_test_pred)
)print
("rmse on training set :"
, rmse_train)
print
("rmse on test set :"
, rmse_test)
r2_score_train = r2_score(y_train,y_train_pred)
r2_score_test = r2_score(y_test,y_test_pred)
print
("r2_score on training set :"
, r2_score_train)
print
("r2_score on test set :"
, r2_score_test)
#設定超引數搜尋範圍
#alphas = [ 0.01, 0.1, 1, 10,100]
#生成乙個lassocv例項
#lasso = lassocv(alphas=alphas)
lasso = lassocv(
)#2.模型訓練
lasso.fit(x_train, y_train)
alpha = lasso.alpha_
print
("best alpha :"
, alpha)
ridgecv預設的score是mean squared errors
#1. 設定超引數搜尋範圍,生成學習器例項
#ridgecv(alphas=(0.1, 1.0, 10.0), fit_intercept=true, normalize=false, scoring=none, cv=none, gcv_mode=none, store_cv_values=false)
alphas =
[0.01
,0.1,1
,10,100
,1000
]ridge = ridgecv(alphas = alphas, store_cv_values=
true
)#2. 用訓練資料度模型進行訓練
#ridgecv採用的是廣義交叉驗證(generalized cross-validation),留一交叉驗證(n-折交叉驗證)的一種有效實現方式
ridge.fit(x_train, y_train)
#通過交叉驗證得到的最佳超引數alpha
alpha = ridge.alpha_
print
("best alpha :"
, alpha)
機器學習 1 線性回歸
2 linear regression with multiple variables multivariate linear regression 多變數線性回歸 3 gradient descent in practice 3.2 learning rate 3.3 features and p...
機器學習之1 線性回歸
在最近一段時間,對機器學習進行了學習,但感覺效果不是很好。一日與朋友聊起此事,他建議建個部落格,一來梳理一下所學內容,二來和大家一起學習交流。因此,建立了此部落格,如果您發現博文中有不當之處,歡迎您來郵指明,我的郵箱為212352807 qq.com 在機器學習中,基本分為兩大問題 監督學習 sup...
機器學習筆記1 線性回歸
資料探勘 人工智慧等領域中存在兩個 分類和回歸 先說回歸 一些簡單的數學題,求解問題往往通過求解未知數,也就是 通過給定的自變數和函式,通過函式求解得到未知數。而機器學習是,通過給定的自變數和函式的解,去求解函式。即求解function x y。通過樣本中大量的特徵 x 和目標變數 y 求得這個函式...