【发布时间】:2020-07-12 23:28:21
【问题描述】:
我想在 python 中使用 sklearn.metrics 计算和打印精度、召回率、fscore 和支持。 我是 doig NLP,所以我的 y_test 和 y_pred 基本上是矢量化步骤之前的单词。
下面的一些信息可以帮助你:
y_test: [0 0 0 1 1 0 1 1 1 0]
y_pred [0.86 0.14 1. 0. 1. 0. 0.04 0.96 0.01 0.99 1. 0. 0.01 0.99
0.41 0.59 0.02 0.98 1. 0. ]
x_train 50
y_train 50
x_test 10
y_test 10
x_valid 6
y_valid 6
y_pred dimension: (20,)
y_test dimension: (10,)
完整的引用错误:
Traceback (most recent call last):
File "C:\Users\iduboc\Documents\asd-dev\train.py", line 324, in <module>
precision, recall, fscore, support = score(y_test, y_pred)
File "C:\Users\iduboc\Python1\envs\asd-v3-1\lib\site-packages\sklearn\metrics\classification.py", line 1415, in precision_recall_fscore_support
pos_label)
File "C:\Users\iduboc\Python1\envs\asd-v3-1\lib\site-packages\sklearn\metrics\classification.py", line 1239, in _check_set_wise_labels
y_type, y_true, y_pred = _check_targets(y_true, y_pred)
File "C:\Users\iduboc\Python1\envs\asd-v3-1\lib\site-packages\sklearn\metrics\classification.py", line 71, in _check_targets
check_consistent_length(y_true, y_pred)
File "C:\Users\iduboc\Python1\envs\asd-v3-1\lib\site-packages\sklearn\utils\validation.py", line 205, in check_consistent_length
" samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [10, 20]
我的代码:
from sklearn.metrics import precision_recall_fscore_support as score
precision, recall, fscore, support = score(y_test, y_pred)
print('precision: {}'.format(precision))
print('recall: {}'.format(recall))
print('fscore: {}'.format(fscore))
print('support: {}'.format(support))
我预测值的代码:
elif clf == 'rndforest':
# No validation data in rnd forest
x_train = np.concatenate((x_train, x_valid))
y_train = np.concatenate((y_train, y_valid))
model = RandomForestClassifier(n_estimators=int(clf_params['n_estimators']),
max_features=clf_params['max_features'])
model.fit(pipe_vect.transform(x_train), y_train)
datetoday = datetime.today().strftime('%d-%b-%Y-%H_%M')
model_name_save = abspath(os.path.join("models", dataset, name_file + '-' +
vect + reduction + '-rndforest'\
+ datetoday + '.pickle'))
print("Model d'enregistrement : ", model_name_save)
x_test_vect = pipe_vect.transform(x_test)
y_pred = model.predict_proba(x_test_vect)
【问题讨论】:
-
您正在尝试比较不同维度的向量,
y_pred dimension (20,)和y_test dimension: (10,)。检查您如何生成这些数据集,在您的代码中我们看不到pipe_vect在做什么。请删除所有不必要的代码并给我们一个最小可重现示例 -> stackoverflow.com/help/minimal-reproducible-example(包括pipe_vect定义)
标签: python machine-learning scikit-learn deep-learning nlp