ValueError:只能使用MultiIndex进行元组索引

时间:2019-02-20 11:46:24

标签: python-3.x machine-learning

对于多标签分类问题,我正在尝试绘制精密度和召回曲线。

示例代码摘自“创建多标签数据,拟合并预测”部分下的“ https://scikit-learn.org/stable/auto_examples/model_selection/plot_precision_recall.html#sphx-glr-auto-examples-model-selection-plot-precision-recall-py”。

我正在尝试将其放入我的代码中,但是当我尝试下面的代码时,出现错误提示为“ ValueError: Can only tuple-index with a MultiIndex”。

train_df.columns.values

array(['DefId', 'DefectCount', 'SprintNo', 'ReqName', 'AreaChange',
   'CodeChange', 'TestSuite'], dtype=object)

Test Suite是要预测的值

X_train = train_df.drop("TestSuite", axis=1)
Y_train = train_df["TestSuite"]
X_test  = test_df.drop("DefId", axis=1).copy()

classes->我对testsuite的值非常满意

from sklearn.preprocessing import label_binarize

# Use label_binarize to be multi-label like settings
Y = label_binarize(Y_train, classes=np.array([0, 1, 2,3,4])
n_classes = Y.shape[1]



# We use OneVsRestClassifier for multi-label prediction
from sklearn.multiclass import OneVsRestClassifier

# Run classifier
classifier = OneVsRestClassifier(svm.LinearSVC(random_state=3))
classifier.fit(X_train, Y_train)
y_score = classifier.decision_function(X_test)


from sklearn.metrics import precision_recall_curve
from sklearn.metrics import average_precision_score
import pandas as pd

# For each class
precision = dict()
recall = dict()
average_precision = dict()
#n_classes = Y.shape[1]
for i in range(n_classes):
    precision[i], recall[i], _ = precision_recall_curve(Y_train[:, i], y_score[:, i])
    average_precision[i] = average_precision_score(Y_train[:, i], y_score[:, i])

Input Data -> Values has been categorised

0 个答案:

没有答案