2012-02-01 92 views
6
  • 我適合邏輯迴歸模型,並使用訓練基於訓練數據集的模型以下
import scikits as sklearn 
from sklearn.linear_model import LogisticRegression 
lr = LogisticRegression(C=0.1, penalty='l1') 
model = lr.fit(training[:,0:-1], training[:,-1) 
  • 我有一個交叉驗證數據集,其中包含一個與輸入矩陣相關的標籤,可以作爲

CV [: - 1]SciPy的/ numpy的/ scikits - 計算精度/召回分數基於兩個陣列

  • 我跑我的交叉驗證數據集對訓練的模型,返回我的0和1的列表基於預測

cv_predict = model.predict(cv [:,0:-1])

問題

我想根據acutal標籤和預測標籤計算精度和召回分數。有沒有一種標準的方法來使用numpy/scipy/scikits?

謝謝

回答

21

是有,請參閱文檔:http://scikit-learn.org/stable/modules/classes.html#classification-metrics

你也應該有一個看看sklearn.metrics.classification_report實用程序:

>>> from sklearn.metrics import classification_report 
>>> from sklearn.linear_model import SGDClassifier 
>>> from sklearn.datasets import load_digits 

>>> digits = load_digits() 
>>> n_samples, n_features = digits.data.shape 
>>> n_split = n_samples/2 

>>> clf = SGDClassifier().fit(digits.data[:n_split], digits.target[:n_split]) 

>>> predictions = clf.predict(digits.data[n_split:]) 
>>> expected = digits.target[n_split:] 

>>> print classification_report(expected, predictions) 
      precision recall f1-score support 

      0  0.90  0.98  0.93  88 
      1  0.81  0.69  0.75  91 
      2  0.94  0.98  0.96  86 
      3  0.94  0.85  0.89  91 
      4  0.90  0.93  0.91  92 
      5  0.92  0.92  0.92  91 
      6  0.92  0.97  0.94  91 
      7  1.00  0.85  0.92  89 
      8  0.71  0.89  0.79  88 
      9  0.89  0.83  0.86  92 

avg/total  0.89  0.89  0.89  899 
+0

這是真棒,謝謝@ogrisel – daydreamer 2012-02-01 13:43:31