简体   繁体   English

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

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

I have a problem trying to compute precision, recall and FMeasure as part of the metrics for evaluating an LSTM text classifier implemented in Keras on Tensorflow. 我在尝试计算精度,召回率和FMeasure作为评估在Tensorflow上的Keras中实现的LSTM文本分类器的度量标准的一部分时遇到问题。 I'm aware that these functions were removed from Keras 2.02 metrics module. 我知道这些功能已从 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)

As an alternative I'm parsing the fitted model and predicted output as object into sklearn.metrics.classification_report however I keep getting the errors about the data types of the targets. 作为替代方案,我将拟合的模型和预测的输出作为对象解析为sklearn.metrics.classification_report但是我不断收到有关目标数据类型的错误。 The predicted output is of the float32 format since I'm using the Sigmoid activation function, while the labels is a collection of text with binary levels of classification. 由于我使用的是Sigmoid激活函数,因此预测的输出为float32格式,而标签是具有二进制分类级别的文本的集合。 I get the accuracy evaluation from the Keras metrics but the precison, recall, fmeasure evaluation is the problem. 我从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

Obviously you did not find out what the output of model.predict . 显然,您没有发现model.predict的输出。 In fact, for your case (you used binary_classification there) you need to call model.predict_classes to match your class/label data y . 实际上,对于您的情况(在那里使用了binary_classification ),您需要调用model.predict_classes来匹配您的类/标签数据y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sklearn.metrics.precision_recall_fscore_support的输出解释 - Interpretation of the output of sklearn.metrics.precision_recall_fscore_support sklearn.metrics.precision_recall_curve:为什么精度和重新调用返回的数组而不是单个值 - sklearn.metrics.precision_recall_curve: Why are the precision and recall returned arrays instead of single values 绘制阈值(precision_recall 曲线)matplotlib/sklearn.metrics - Plotting Threshold (precision_recall curve) matplotlib/sklearn.metrics 了解 tf.keras.metrics.Precision and Recall 进行多类分类 - Understanding tf.keras.metrics.Precision and Recall for multiclass classification Keras 2.3.0 指标准确度、精度和召回率的值相同 - Same value for Keras 2.3.0 metrics accuracy, precision and recall sklearn 的 Plot 精度和召回率 - Plot precision and recall with sklearn 使用 sklearn 获得精度和召回率 - Getting Precision and Recall using sklearn 是否可以将精度、召回率、f1_score 等 sklearn 评估指标应用于我的问题? - Is it possible to apply sklearn evaluation metrics such as precision, recall, f1_score on my problem? sklearn.metrics.precision_recall_curve 中的估计概率(probas_pred)是多少? - What is estimated probability(probas_pred) in sklearn.metrics.precision_recall_curve? Python Scikit-调用sklearn.metrics.precision_recall_curve时输入形状错误 - Python Scikit - bad input shape when calling sklearn.metrics.precision_recall_curve
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM