我想用python中的scickit learn来计算和打印auc_score, f_score和其他指标,我是做NLP的,一开始我的集合是单词列表,我把它们矢量化来做一些预测。
我的代码 :
#Vectorization
x_test_vect = pipe_vect.transform(x_test)
#Create predictive set
y_pred = model.predict_proba(x_test_vect)
#Calculate treshold
fpr, tpr, thresholds = roc_curve(y_test, y_pred[:, 1])
spr = np.array(tpr) + (1 - np.array(fpr))
threshold = thresholds[np.argmax(spr)]
y_predi = [y >= threshold for y in y_pred[:, 1]]
#Calculate AUC score
AUC = roc_auc_score(y_test, y_predi[:, 1])
我的打印。
y_pred [[0.89882979 0.10117021]
[0.87 0.13 ]
[0.65 0.35 ]
[0.94122287 0.05877713]
[0.50868421 0.49131579]
[0.97847458 0.02152542]
[0.99 0.01 ]
[0.21 0.79 ]
[0.94 0.06 ]
[0.94604348 0.05395652]]
y_test [1 0 0 0 1 0 0 1 0 0]
y_predi [False, False, True, False, True, False, False, True, False, False]
threshold 0.2407608695652174
错误。
AUC = roc_auc_score(y_test, y_predi[:, 1])
TypeError: list indices must be integers or slices, not tuple
你是想把y_predi切错了。从打印来看,它是一个1-d的数组,你可以直接给它.当你试图做y_predi[:,1]时,它试图给所有行的第一列,但你的数组是1-d的,没有第一列,因此出现了错误。