sklearn.linear_model.RidgeClassifier?

class sklearn.linear_model.RidgeClassifier(alpha=1.0, *, fit_intercept=True, normalize=False, copy_X=True, max_iter=None, tol=0.001, class_weight=None, solver='auto', random_state=None)

[源碼]

使用Ridge回歸的分類器。

該分類器首先將目標值轉換為{-1, 1},然后將問題視為回歸任務(在多類情況下為多輸出回歸)。

用戶指南中閱讀更多內容。

參數 說明
alpha float, default=1.0
正則強度,必須為正浮點數。正則化改善了問題的狀況,并減少了估計的方差。較大的值表示更強的正則化。在其他線性模型中,Alpha對應于1 / (2C),例如 LogisticRegressionsklearn.svm.LinearSVC
fit_intercept bool, default=True
是否計算此模型的截距。如果設置為false,則在計算中將不使用截距(如數據已經中心化)。
normalize bool, default=False
fit_intercept設置為False 時,將忽略此參數。如果為True,則在回歸之前通過減去均值并除以l2-范數來對回歸變量X進行歸一化。如果你希望標準化,請先使用 sklearn.preprocessing.StandardScaler,然后調用fit 估算器并設置normalize=False
copy_X bool, default=True
如果為True,將復制X;否則,X可能會被覆蓋。
max_iter int, default=None
共軛梯度求解器的最大迭代次數。默認值由scipy.sparse.linalg確定。
tol float, default=1e-3
優化算法的精度。
class_weight dict or ‘balanced’, default=None
{class_label: weight}的形式與類別關聯的權重。如果沒有給出,所有類別的權重都應該是1。

“balanced”模式使用y的值來自動調整為與輸入數據中的類頻率成反比的權重。如n_samples / (n_classes * np.bincount(y))
solver {‘auto’, ‘svd’, ‘cholesky’, ‘lsqr’, ‘sparse_cg’, ‘sag’, ‘saga’}, default=’auto’
用于計算例程的求解器:
- “auto”會根據數據類型自動選擇求解器。
- “ svd”使用X的奇異值分解來計算Ridge系數。對于奇異矩陣,比“ Cholesky”更穩定。
- 'cholestsky'使用標準的scipy.linalg.solve函數獲得封閉形式的解決方案。
- 'sparse_cg'使用scipy.sparse.linalg.cg中的共軛梯度求解器。作為一種迭代算法,對于大規模數據(可以設置tolmax_iter),此求解器比“ Cholesky”更合適。
- “ lsqr”使用專用的正則化最小二乘算法scipy.sparse.linalg.lsqr。它是最快的,并且使用了迭代過程。
- “sag”使用隨機平均梯度下降,而“saga”是它的無偏和更靈活的版本。這兩種方法都使用了迭代過程,當n_samples和n_features都很大時,通常比其他求解器更快。請注意, “sag”和“saga”的快速收斂只能保證在具有大致相同規模的特性上。你可以使用sklearn.preprocessing中的縮放器對數據進行預處理。

*0.17版本中的新功能:*隨機平均梯度下降求解器。

0.19版中的新功能: SAGA求解器。
random_state int, RandomState instance, default=None
solver=='sag'或'saga'時使用,用于隨機打亂數據。有關詳細信息,請參見詞匯表
屬性 說明
coef_ ndarray of shape (1, n_features) or (n_classes, n_features)
決策函數中特征的系數。
當給定問題為二分類時,coef_形狀為(1,n_features)。
intercept_ float or ndarray of shape (n_targets,)
決策函數中的截距。如果設置fit_intercept = False,則截距為0.0 。
n_iter_ None or ndarray of shape (n_targets,)
每個目標的實際迭代次數。僅適用于sag和lsqr求解器。其他求解器將返回None。
classes_ ndarray of shape (n_classes,)
類別標簽。

另見

對于多類別分類,以“一對多”的方法訓練n_class分類器。具體而言,這是通過利用Ridge中的多變量響應支持來實現的。

示例

>>> from sklearn.datasets import load_breast_cancer
>>> from sklearn.linear_model import RidgeClassifier
>>> X, y = load_breast_cancer(return_X_y=True)
>>> clf = RidgeClassifier().fit(X, y)
>>> clf.score(X, y)
0.9595...

方法

方法 說明
decision_function(self, X) 預測樣本的置信度得分。
fit(self, X, y[, sample_weight]) 擬合Ridge分類器模型。
get_params(self[, deep]) 獲取此估計器的參數。
predict(self, X) 預測X中樣本的類別標簽。
score(self, X, y[, sample_weight]) 返回給定測試數據和標簽上的平均準確度。
set_params(self, **params) 設置此估計器的參數。
__init__(self, alpha=1.0, *, fit_intercept=True, normalize=False, copy_X=True, max_iter=None, tol=0.001, class_weight=None, solver='auto', random_state=None)

[源碼]

初始化self, 請參閱help(type(self))以獲得準確的說明。

decision_function(self, X)

[源碼]

預測樣本的置信度得分。

樣本的置信度分數是該樣本到超平面的符號距離。

參數 說明
X array_like or sparse matrix, shape (n_samples, n_features)
樣本數據。
返回值 說明
array, shape=(n_samples,) if n_classes == 2 else (n_samples, n_classes)
每個(樣本,類別)組合的置信度得分。在二分類情況下,self.classes_ [1]的置信度得分> 0表示將預測該類。
fit(self,X,y,sample_weight = None )

[源碼]

擬合Ridge分類器模型。

參數 說明
X {ndarray, sparse matrix} of shape (n_samples, n_features)
訓練數據
y ndarray of shape (n_samples,)
目標標簽。
sample_weight float or ndarray of shape (n_samples,), default=None
每個樣本的權重,如果使用浮點數,每個樣品的權重都相同。

0.17版中的新功能:sample_weight支持Classifier
返回值 說明
self object
估計器的實例。
get_params(self,deep = True )

[源碼]

獲取此估計器的參數。

參數 說明
deep bool, default=True
如果為True,返回此估計器和所包含子對象的參數。
返回值 說明
params mapping of string to any
參數名稱映射到其值。
predict(self, X)

[源碼]

預測X中樣本的類別標簽。

參數 說明
X array_like or sparse matrix, shape (n_samples, n_features)
樣本數據
返回值 說明
C array, shape [n_samples]
每個樣本的預測類別標簽。
score(self,X,y,sample_weight = None )

[源碼]

返回給定測試數據和標簽上的平均準確度。

在多標簽分類中,這是子集準確性,這是一個嚴格的指標,因為你需要為每個樣本正確預測對應的標簽集。

參數 說明
X array-like of shape (n_samples, n_features)
測試樣本。
y array-like of shape (n_samples,) or (n_samples, n_outputs)
X的真實標簽。
sample_weight array-like of shape (n_samples,), default=None
樣本權重。
返回值 說明
score float
預測標簽與真實標簽的平均準確度
set_params(self, **params)

[源碼]

設置并驗證估計器的參數。

參數 說明
**params dict
估計器參數。
返回值 說明
self object
估計器實例。

sklearn.linear_model.RidgeClassifier使用示例?