線性模型

常用的線性模型有線性迴歸、嶺迴歸、套索迴歸、邏輯迴歸和線性 SVM 等。

線性迴歸

原理

線性迴歸(linear regression)是一種迴歸分析技術。線性迴歸試圖學習到一個線性模型以儘可能準確地預測實值輸出標記。通過在數據集上建立線性模型,建立代價函數(loss function),最終以優化代價函數爲目標確定模型參數w和b,從而得到模型用以後續的預測。

具體用法
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression


def linear_regression1():
    # n_features: 特徵個數 = n_informative() + n_redundant + n_repeated
    # n_informative:多信息特徵的個數
    # n_redundant:冗餘信息,informative特徵的隨機線性組合
    # n_repeated :重複信息,隨機提取n_informative和n_redundant 特徵
    # n_classes:分類類別
    # n_clusters_per_class :某一個類別是由幾個cluster構成的
    X, y = make_regression(n_samples=200, n_features=2, n_informative=2, noise=20, random_state=3)
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=8)
    lr = LinearRegression().fit(X_train, y_train)
    print('--------------')
    print('lr.coef_:{}'.format(lr.coef_[:]))
    print('lr.intercept_:{}'.format(lr.intercept_))
    print('----------')
    print('測試數據集得分:{:.2f}'.format(lr.score(X_test, y_test)))
    print('-------------')

運行結果

--------------
lr.coef_:[97.41730944 54.49354732]
lr.intercept_:-0.05991175060145615
----------
測試數據集得分:0.97
-------------

嶺迴歸

原理

嶺迴歸是一種能夠避免過擬合的線性模型。在嶺迴歸中,模型會保留所有的特徵變量,但是會減小特徵變量的係數值,讓特徵變量對預測結果的影響變小,在嶺迴歸中是通過改變其 alpha 參數來控制減小特徵變量係數的程度。這種通過保留全部特徵變量,只是降低特徵變量的係數值來避免過擬合的方法,我們稱之爲 L2 正則化。

具體用法
from sklearn.linear_model import Ridge
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split


def linear_regression1():
    # 糖尿病情數據集
    X, y = load_diabetes().data, load_diabetes().target
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=8)
    # alpha 越大,則越降低過擬合程度
    ridge = Ridge(alpha=0.5).fit(X_train, y_train)
    print('--------------')
    print('ridge.coef_:{}'.format(ridge.coef_[:]))
    print('ridge.intercept_:{}'.format(ridge.intercept_))
    print('----------')
    print('訓練數據集得分:{:.2f}'.format(ridge.score(X_train, y_train)))
    print('測試數據集得分:{:.2f}'.format(ridge.score(X_test, y_test)))

運行結果

--------------
ridge.coef_:[  34.51271596 -128.2391615   367.7570263   266.8969371   -27.64762407
  -57.6591586  -162.73744155  105.62127781  281.34740313  125.36349347]
ridge.intercept_:152.514304568997
----------
訓練數據集得分:0.48
測試數據集得分:0.47

套索迴歸

原理

套索迴歸也會將係數限制在非常接近 0 的範圍內,但它進行限制的方式稍微有點不同,我們稱之爲 L1 正則化。L1 正則化會導致在使用套索迴歸的時候,有一部分特徵的係數會正好等於 0 。也就是說,有一些特徵在使用套索迴歸的時候會徹底被模型忽略掉,這也可以看成是模型對於特徵進行自動選擇的一種方式。把一些特徵忽略掉,從而突出體現模型中最重要的那些特徵。

具體用法
from sklearn.linear_model import Lasso
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
import numpy as np


def linear_regression1():
    # 糖尿病情數據集
    X, y = load_diabetes().data, load_diabetes().target
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=8)
    # alpha 越大,則使用特徵數越少
    lasso = Lasso(alpha=0.1, max_iter=100000).fit(X_train, y_train)
    print('--------------')
    print('lasso.coef_:{}'.format(lasso.coef_[:]))
    print('lasso.intercept_:{}'.format(lasso.intercept_))
    print('----------')
    print('訓練數據集得分:{:.2f}'.format(lasso.score(X_train, y_train)))
    print('測試數據集得分:{:.2f}'.format(lasso.score(X_test, y_test)))
    print('套索迴歸使用的特徵數:{}'.format(np.sum(lasso.coef_ != 0)))

運行結果

--------------
lasso.coef_:[   0.         -181.40911617  537.71961152  355.19720303 -105.43471481
   -0.         -198.99829878    0.          440.25133426   48.25573121]
lasso.intercept_:152.43690667906043
----------
訓練數據集得分:0.52
測試數據集得分:0.48
套索迴歸使用的特徵數:7

彈性網模型

原理

彈性網模型(ElasticNet)綜合了嶺迴歸和套索迴歸的懲罰因子。在實踐中這個模型的效果是做好的,然而代價是用戶需要調整量兩個參數,一個是 L1 正則化參數,另一個是 L2 正則化參數。

具體用法
from sklearn.linear_model import ElasticNet
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
import numpy as np


def linear_regression1():
    # 糖尿病情數據集
    X, y = load_diabetes().data, load_diabetes().target
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=8)
    elastic_net = ElasticNet(alpha=1, l1_ratio=1, max_iter=100000).fit(X_train, y_train)
    print('--------------')
    print('elastic_net.coef_:{}'.format(elastic_net.coef_[:]))
    print('elastic_net.intercept_:{}'.format(elastic_net.intercept_))
    print('----------')
    print('訓練數據集得分:{:.2f}'.format(elastic_net.score(X_train, y_train)))
    print('測試數據集得分:{:.2f}'.format(elastic_net.score(X_test, y_test)))
    print('彈性網迴歸使用的特徵數:{}'.format(np.sum(elastic_net.coef_ != 0)))

運行結果

--------------
elastic_net.coef_:[  0.          -0.         384.73421807  72.69325545   0.
   0.          -0.           0.         247.88881314   0.        ]
elastic_net.intercept_:152.6882498532522
----------
訓練數據集得分:0.36
測試數據集得分:0.37
彈性網迴歸使用的特徵數:3
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章