2017-08-11 38 views
2

更新的問題: 我這樣做,但我得到了同樣的結果,既精度和召回是因爲我使用average ='binary'?使用sklearn獲得相同的精度和召回(K-NN)值

但是當我使用平均= '宏觀' 我收到此錯誤信息:

測試定製審覈 messageC:\ Python27 \ LIB \站點包\ sklearn \指標\ classification.py:976 : DeprecationWarning:從版本0.18開始,當使用平均精確度/召回率/ F-分數時,二進制輸入將不是 。請 使用average ='binary'僅報告正面課堂表現。
「正級性能」,DeprecationWarning)

這是我更新的代碼:

path = 'opinions.tsv' 
data = pd.read_table(path,header=None,skiprows=1,names=['Sentiment','Review']) 
X = data.Review 
y = data.Sentiment 
#Using CountVectorizer to convert text into tokens/features 
vect = CountVectorizer(stop_words='english', ngram_range = (1,1), max_df = .80, min_df = 4) 
X_train, X_test, y_train, y_test = train_test_split(X,y,random_state=1, test_size= 0.2) 
#Using training data to transform text into counts of features for each message 
vect.fit(X_train) 
X_train_dtm = vect.transform(X_train) 
X_test_dtm = vect.transform(X_test) 




#Accuracy using KNN Model 
KNN = KNeighborsClassifier(n_neighbors = 3) 
KNN.fit(X_train_dtm, y_train) 
y_pred = KNN.predict(X_test_dtm) 
print('\nK Nearest Neighbors (NN = 3)') 



#Naive Bayes Analysis 
tokens_words = vect.get_feature_names() 
print '\nAnalysis' 
print'Accuracy Score: %f %%'% (metrics.accuracy_score(y_test,y_pred)*100) 
print "Precision Score: %f%%" % precision_score(y_test,y_pred, average='binary') 
print "Recall Score: %f%%" % recall_score(y_test,y_pred, average='binary') 

通過使用上面的代碼,我得到相同價值的準確率和召回

謝謝你回答我的問題,非常感謝。

回答

0

要計算precisionrecall指標,你應該從sklearn.metrics導入根據的方法。

如文檔中表示,它們的參數是真正的1-d陣列和預測標籤:

from sklearn.metrics import precision_score 
from sklearn.metrics import recall_score 

y_true = [0, 1, 2, 0, 1, 2] 
y_pred = [0, 2, 1, 0, 0, 1] 

print('Calculating the metrics...') 

recision_score(y_true, y_pred, average='macro') 
>>> 0.22 

recall_score(y_true, y_pred, average='macro') 
>>> 0.33 
+0

謝謝回答,但我還是在計算精度和召回了一些問題,我得到的相同的值 你能看看我更新的問題嗎?非常感謝 –

+0

這是不是一個問題,你收到精度和召回指標相同的價值。這只是數據的一個特點。要添加,您收到的消息不是錯誤,它表示您的輸入是二進制的,即。即無論是零還是一個,所以您應該使用'average ='binary''來計算精度和召回指標。 –

相關問題