2015-05-12 163 views
1

關於SVMs的文檔意味着存在一個名爲'classes_'的屬性,它有望揭示模型如何在內部表示類。識別sklearn模型的類

我想獲得這些信息,以便解釋像'predict_proba'這樣的函數的輸出,該函數可以爲多個樣本生成類的概率。希望知道(完全隨機選擇的值,只是爲了舉例說明)

model.classes_ 
>> [1, 2, 4] 

手段,我可​​以假設

model.predict_proba([[1.2312, 0.23512, 6.], [3.7655, 8.2353, 0.86323]]) 
>> [[0.032, 0.143, 0.825], [0.325, 0.143, 0.532]] 

裝置,該概率轉換爲相同的數量級的類,即對於第一組特徵:

probability of class 1: 0.032 
probability of class 2: 0.143 
probability of class 4: 0.825 

但是在SVM上調用'classes_'會導致錯誤。有沒有一種好的方法來獲取這些信息?我無法想象在訓練完模型後無法再訪問它。

編輯: 我建立我的模型是多還是少這樣的方式:

from sklearn.svm import SVC 
from sklearn.grid_search import GridSearchCV 
from sklearn.pipeline import Pipeline, FeatureUnion 


pipeline = Pipeline([ 
    ('features', FeatureUnion(transformer_list[ ... ])), 
    ('svm', SVC(probability=True)) 
]) 
parameters = { ... } 
grid_search = GridSearchCV(
    pipeline, 
    parameters 
) 

grid_search.fit(get_data(), get_labels()) 
clf = [elem for elem in grid_search.estimator.steps if elem[0] == 'svm'][0][1] 

print(clf) 
>> SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0, 
    kernel='rbf', max_iter=-1, probability=True, random_state=None, 
    shrinking=True, tol=0.001, verbose=False) 
print(clf.classes_) 
>> Traceback (most recent call last): 
    File "path/to/script.py", line 284, in <module> 
    File "path/to/script.py", line 181, in re_train 
    print(clf.classes_) 
AttributeError: 'SVC' object has no attribute 'classes_' 

回答

2

,你正在尋找它的不合身的管道grid_search.estimatorclasses_屬性僅在擬合後才存在,因爲分類器需要看到y

你想要的是使用最佳參數設置進行訓練的估算器,即grid_search.best_estimator_

下面的工作:

CLF = grid_search.best_estimator_.named_steps [ 'SVM'] 打印(clf.classes_)

[和classes_不正是你想象的那樣。

+0

謝謝,它現在可以工作=) – Arne

1

有在sklearn一類的領域,這可能意味着你調用了錯誤的模型,看下面的例子中,我們可以看到,有類看classes_場時:

>>> import numpy as np 
>>> from sklearn.svm import SVC 
>>> X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]]) 
>>> y = np.array([1, 1, 2, 2]) 
>>> clf = SVC(probability=True) 
>>> clf.fit(X, y) 
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0, 
    kernel='rbf', max_iter=-1, probability=True, random_state=None, 
    shrinking=True, tol=0.001, verbose=False) 
>>> print clf.classes_ 
[1 2] 
>>> print clf.predict([[-0.8, -1]]) 
[1] 
>>> print clf.predict_proba([[-0.8, -1]]) 
[[ 0.92419129 0.07580871]] 
+0

我明白了。也許我的問題是,我的模型隱藏在GridSearchCV中。我更新了更多信息的初始文章,以及更具體的錯誤消息。 – Arne