前兩天介紹了線性回歸和幾種正規化方法的概念,今天來講要如何使用python實作,以及幾種常用參數介紹
我這邊python版本是3.9.13
#c.NotebookApp.browser = u''
c.NotebookApp.browser = u'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
#c.NotebookApp.notebook_dir = u''
c.NotebookApp.notebook_dir = u'D:\\JupyterNotebook'
from sklearn.linear_model import ElasticNet
alphas = np.logspace(-6, 6, 100) #正規化係數
mdl = ElasticNet()
for a in alphas:
mdl.set_params(alpha=a,l1_ratio=1)
mdl.fit(X_std, y)
print(mdl.coef_)
from sklearn.linear_model import ElasticNet
alphas = np.logspace(-6, 6, 100) #正規化係數
mdl = ElasticNet()
for a in alphas:
mdl.set_params(alpha=a,l1_ratio=0)
mdl.fit(X_std, y)
print(mdl.coef_)
from sklearn.linear_model import ElasticNet
alphas = np.logspace(-6, 6, 100) #正規化係數
mdl = ElasticNet()
for a in alphas:
mdl.set_params(alpha=a,l1_ratio=0.5)
mdl.fit(X_std, y)
print(mdl.coef_)