【发布时间】:2025-11-22 10:35:01
【问题描述】:
我在尝试计算精度、召回率和 FMeasure 作为评估在 Tensorflow 上的 Keras 中实现的 LSTM 文本分类器的指标时遇到问题。我知道 these functions were removed 来自 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,但是我不断收到有关目标数据类型的错误。预测的输出是float32 格式,因为我使用的是 Sigmoid 激活函数,而标签是具有二进制分类级别的文本集合。我从 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
【问题讨论】:
-
异常看起来很简单。
X_train、X_test、y_train和y_test中的每个元素的类型是什么?看起来你可能有一堆 0 和 1,并且可能还有一些无关的元素。
标签: python machine-learning scikit-learn neural-network keras