关于Keras分类器的精确度,召回率和FMeasure的Sklearn度量标准

时间:2017-12-19 16:52:32

标签: python machine-learning scikit-learn neural-network keras

我在尝试计算精度,召回和FMeasure时遇到问题,作为评估在Tensorflow上的Keras中实现的LSTM文本分类器的度量的一部分。我知道来自Keras 2.02指标模块的these functions were removed

# 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但是我不断得到有关目标数据类型的错误。预测输出为float32格式,因为我使用Sigmoid激活函数,而标签是具有二进制分类级别的文本集合。我从Keras指标中得到了准确度评估,但是精确度,召回,测量评估是问题所在。

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

1 个答案:

答案 0 :(得分:0)

显然你没有找到model.predict的输出。事实上,对于您的情况(您使用 binary_classification ),您需要致电model.predict_classes以匹配您的班级/标签数据y