sklearn.multiclass.OneVsOneClassifier?
class sklearn.multiclass.OneVsOneClassifier(estimator, *, n_jobs=None)
[源碼]
一對多策略
該策略包括為每個類別對配備一個分類器。在預測時,選擇獲得最多選票的類。由于該方法需要適合分類器,因此該方法的復雜度為O(n_classes ^ 2),通常比其余方法慢。但是,這種方法對于無法很好地擴展的算法(例如內核算法)可能是有利的 。這是因為每個單獨的學習問題僅涉及數據的一小部分,而相對于其余部分,完整數據集的使用次數為:
n_classes * (n_classes - 1) / 2 n_samples n_classes
在用戶指南中閱讀更多內容。
參數 | 說明 |
---|---|
estimator | estimator object 種實現擬合和決策函數(decision_function)或預測概率(predict_proba)之一的估計對象。 |
n_jobs | int or None, optional (default=None) 用于計算的數量。 None 除非joblib.parallel_backend 上下文中,否則表示1 。 -1 表示使用所有處理器。有關 更多詳細信息,請參見詞匯表。 |
屬性 | 說明 |
---|---|
estimators_ | list of n_classes * (n_classes - 1) / 2 estimators用于預測的估計量。 |
classes_ | numpy array of shape [n_classes] 包含標簽的數組。 |
n_classes_ | int 類數 |
pairwise_indices_ | list, length = len(estimators_) , or None 訓練估計量時使用的樣本索引。 None 時estimator 沒有_pairwise 屬性。 |
實例
>>> from sklearn.datasets import load_iris
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.multiclass import OneVsOneClassifier
>>> from sklearn.svm import LinearSVC
>>> X, y = load_iris(return_X_y=True)
>>> X_train, X_test, y_train, y_test = train_test_split(
... X, y, test_size=0.33, shuffle=True, random_state=0)
>>> clf = OneVsOneClassifier(
... LinearSVC(random_state=0)).fit(X_train, y_train)
>>> clf.predict(X_test[:10])
array([2, 1, 0, 2, 0, 2, 0, 1, 1, 1])
方法 | 說明 |
---|---|
decision_function (X) |
OneVsOneClassifier的決策函數。 |
fit (X, y) |
擬合基礎估計量。 |
get_params ([deep]) |
獲取此估計量的參數。 |
partial_fit (X, y[, classes]) |
部分擬合基礎估計量 |
predict (X) |
估計X中每個樣本的最佳分類標簽。 |
score (X, y[, sample_weight]) |
返回給定測試數據和標簽上的平均準確度。 |
set_params (**params) |
設置此估算器的參數。 |
_init__(estimator, *, n_jobs=None)
[源碼]
初始化self, 請參閱help(type(self))以獲得準確的說明。
decision_function(X)
[源碼]
OneVsOneClassifier的決策函數。
通過將成對分類置信度的歸一化總和添加到票中來計算樣本的決策值,以便在所有類的票均相等導致平局時在決策值之間消除歧義。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) |
返回值 | 說明 |
---|---|
Y | array-like of shape (n_samples, n_classes) 在版本0.19中進行了更改:輸出形狀已更改為(n_samples,)符合scikit-learn二進制分類的約定。 |
fit(X,y)
[源碼]
擬合基礎估計量。
參數 | 說明 |
---|---|
X | (sparse) array-like of shape (n_samples, n_features) 數據 |
y | array-like of shape (n_samples,) 多類別目標。 |
返回值 |
---|
self |
get_params(deep=True)
[源碼]
獲取此估計量的參數。
參數 | 說明 |
---|---|
deep | bool, default=True 如果為True,則將返回此估計量和作為估計量的包含子對象的參數。 |
返回值 | 說明 |
---|---|
params | mapping of string to any 參數名稱映射到其值。 |
partial_fit(X, y, classes=None)
[源碼]
部分擬合基礎估計量
當內存不足以訓練所有數據時應使用。可以在多次迭代中傳遞數據塊,其中第一次調用應具有所有目標變量的數組。
參數 | 說明 |
---|---|
X | (sparse) array-like of shape (n_samples, n_features) 數據 |
y | array-like of shape (n_samples,) 多類別目標。 |
classes | array, shape (n_classes, ) 所有對partial_fit的調用中的類。可以通過 np.unique(y_all) 獲得,其中y_all是整個數據集的目標向量。僅在partial_fit的第一次調用中需要此參數,而在后續調用中可以將其省略。 |
返回值 |
---|
self |
predict(X)
[源碼]
估計X中每個樣本的最佳類別標簽。
這是作為 argmax(decision_function(X), axis=1)
實現的,它將通過預測每個可能類對的決策結果的估計器返回具有大多數投票的類的標簽。
參數 | 說明 |
---|---|
X | (sparse) array-like of shape (n_samples, n_features) 數據 |
返回值 | 說明 |
---|---|
y | numpy array of shape [n_samples] 預測的多類別目標。 |
score(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 self.predict(X) wrt. y.的平均準確度 |
set_params(**params)
[源碼]
設置此估算器的參數。
該方法適用于簡單的估計器以及嵌套對象(例如 pipelines)。后者具有形式的參數, <component>__<parameter>
以便可以更新嵌套對象的每個組件。
參數 | 說明 |
---|---|
**params | dict 估算量參數。 |
返回值 | 說明 |
---|---|
self | object 估算量實例。 |