簡體   English   中英

Keras分類器的Sklearn精度,召回率和FMeasure度量

[英]Sklearn Metrics of precision, recall and FMeasure on Keras classifier

我在嘗試計算精度,召回率和FMeasure作為評估在Tensorflow上的Keras中實現的LSTM文本分類器的度量標准的一部分時遇到問題。 我知道這些功能已從 Keras 2.02指標模塊中刪除

# create the model
embedding_vector_length = 32
model = Sequential()
# load the dataset with word embedding but only keep the top n words, zero the rest
model.add(Embedding(top_words, embedding_vector_length, input_length=max_tweet_length)) 

model.add(LSTM(100))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(X_train, y_train, epochs=3, batch_size=64)

# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
print(scores)

# print the classification report
from sklearn.metrics import classification_report
predicted = model.predict(X_test)
report = classification_report(y_test, predicted)
print(report)

作為替代方案,我將擬合的模型和預測的輸出作為對象解析為sklearn.metrics.classification_report但是我不斷收到有關目標數據類型的錯誤。 由於我使用的是Sigmoid激活函數,因此預測的輸出為float32格式,而標簽是具有二進制分類級別的文本的集合。 我從Keras指標獲得了准確性評估,但是精確度,召回率,fmeasure評估才是問題。

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/root/anaconda3/envs/py35/lib/python3.5/site-packages/sklearn/metrics/classification.py", line 1261, in precision_score
    sample_weight=sample_weight)
  File "/root/anaconda3/envs/py35/lib/python3.5/site-packages/sklearn/metrics/classification.py", line 1025, in precision_recall_fscore_support
    y_type, y_true, y_pred = _check_targets(y_true, y_pred)
  File "/root/anaconda3/envs/py35/lib/python3.5/site-packages/sklearn/metrics/classification.py", line 81, in _check_targets
    "and {1} targets".format(type_true, type_pred))
ValueError: Classification metrics can't handle a mix of binary and continuous targets

顯然,您沒有發現model.predict的輸出。 實際上,對於您的情況(在那里使用了binary_classification ),您需要調用model.predict_classes來匹配您的類/標簽數據y

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM