Sklearn LogisticRegression超參數介紹

Sklearn LogisticRegression超參數介紹

Sklearn LogisticRegression超參數介紹

筆記一下看到的邏輯是回歸可以調整的超參數

penalty: {‘l1’, ‘l2’, ‘elasticnet’, None}, default=’l2’}

None 表示訓練的時候不給予懲罰

elasticnet 表示:l1/l2都使用

那甚麼是L1/L2?

公式如上圖,前半部是loss function,兩者都一樣,可以集中注意在後半部

L1使用的是絕對值,L2使用的是平方值。

假設目標都是把cost降低十倍,單純採用絕對值,L1的Wj加總需要從10減少到1,L2的Wj加總只需要從10減少到根號10(約3),表示L1需要更加劇烈的壓縮,會讓大多數的參數權重歸零,大多數權重歸零,就等價於feature selection的功能。

也可以參考使用的貝式先驗,下圖x軸是權重採用的數字,Y軸是機率,藍色是L1,表示在權重0的時候,採用的機率相對較大;紅色是L2,在靠近0的附近權重變化沒有L1劇烈,也表示L2有比較高的機率去保留權重而不是0

因此兩種比較

L1: 偏好讓權重歸0,類似特徵工程,適用於你對於資料的了解是少數為有效

L2: 讓權重變小,但每個參數的權重都不為零,適用於每個資料點都有意義但想避免overfitting。

tol: float, default=1e-4

tolerance 表示對於何時結束的忍受度,下圖為例,機器學習過程會不停嘗試降低cost去找到局部最佳解,每一個點就是每一次訓練,當越接近最低值的時候,改善的cost越會來越少,當改善有限時,就可以假定已經目前的解已經在最佳/局部最佳解附近。

C: float, default=1.0

必須要大於0,正規化的反向

更強正規化(越小的C)對於最小化training error會比較少,讓model相對簡單

反之較弱的正規化(越大的C)較針對讓training error更小

fit_intercept: bool, default=True

如果為True,隱含假設為參數沒有經過原點(0,0)

class_weight:dict or ‘balanced’, default=None

用來手動調整不同權重

from sklearn.linear_model import LogisticRegression# Create a logistic regression model with custom class weightsclass_weights = {0: 1, 1: 10}  # Assign higher weight to class 1model = LogisticRegression(class_weight=class_weights)model.fit(X_train, y_train)# Create a logistic regression model with balanced class weightsmodel = LogisticRegression(class_weight='balanced')model.fit(X_train, y_train)

random_state: int, Random State instance, default=None

你的隨機變數如何設定,有時候為了穩定測試時期的model讓整體更可控,會指定seed

from sklearn.linear_model import LogisticRegression# Create a logistic regression model with a fixed random statemodel = LogisticRegression(solver='liblinear', random_state=42)model.fit(X_train, y_train)

solver: {‘lbfgs’, ‘liblinear’, ‘newton-cg’, ‘newton-cholesky’, ‘sag’, ‘saga’}, default=’lbfgs’

不同種類的solver在於不同的資料大小效能不同,有些solver 支援multinomial,及多元分類,你要分的群不只有0/1兩種時使用

max_iter: int, default=100

最大訓練次數

warm_start: bool, default=False

是否延續上一次的訓練結果

幾種使用手法

先前介紹的正規化強度變更,一開始先設定C=0.1,先盡可能複雜化model,結束後再降低fitting C=1,讓model 簡單化

from sklearn.linear_model import LogisticRegression# Initial training with a small regularization parametermodel = LogisticRegression(solver='saga', warm_start=True, C=0.1)model.fit(X_train, y_train)# Adjust the regularization parameter and continue trainingmodel.C = 1.0model.fit(X_train, y_train)

如果有新的資料要繼續訓練

from sklearn.linear_model import LogisticRegression# Initial training with the first batch of datamodel = LogisticRegression(solver='saga', warm_start=True)model.fit(X_train_batch1, y_train_batch1)# Continue training with the second batch of datamodel.fit(X_train_batch2, y_train_batch2)

接下來是可以使用的Attribute

coef_: ndarray of shape (1, n_features) or (n_classes, n_features)

去看訓練不同參數的權重

from sklearn.linear_model import LogisticRegression# Sample dataX_train = [[0, 1], [1, 0], [1, 1], [0, 0]]y_train = [0, 1, 1, 0]# Train the modelmodel = LogisticRegression()model.fit(X_train, y_train)# Get the coefficientsprint(model.coef_)# output: [[ 8.02109869e-01 -4.71500832e-06]]

intercept_: ndarray of shape (1,) or (n_classes,)

印出截距

Comments

Loading comments…

Leave a Comment