python 使用SVM时,轴1超出维度为1的数组的界限

qgelzfjb  于 2023-04-28  发布在  Python
关注(0)|答案(1)|浏览(182)

训练数据

df = pd.read_excel('C:/Users/Ram Prakash/Downloads/Data.xlsx', 
sheet_name = 'Multiclass')
X = df.drop('Fault Type', axis =1)
y = df.iloc[0:, 10]
y = le.fit_transform(y)

# Train Test split
X_train, X_test, y_train, y_test = train_test_split(X, y, 
test_size=0.3, random_state=2021)
scaler = StandardScaler().fit(X_train)
X_train, X_test = scaler.transform(X_train), 
scaler.transform(X_test)

我正在尝试将数据分类为多个类。然而,我一直得到这个错误。
验证码:

##Classification by Default parameters
# Fit SVM classifier
clf_default = SVC(kernel='rbf')
clf = OneVsOneClassifier(clf_default).fit(X_train, y_train)
print('(Cross Validation) AUC Score:', np.mean(cross_val_score(estimator=clf, X=X_train, y=y_train, cv=5, scoring = 'roc_auc')))

# Show result
print('(Test set) Confusion Matrix:')
c = label_binarize(y_test, classes = labels)
print(confusion_matrix(y_test, clf.predict(X_test)))
print('(Test set) AUC Score:', roc_auc_score(y_test, clf.predict(X_test), average = 'macro', multi_class = 'ovo'))

在运行代码时,我得到以下错误。

Traceback (most recent call last):
  File "C:\Users\Ram Prakash\AppData\Local\Temp\ipykernel_23060\2986435798.py", line 6, in <cell line: 5>
    print('(Test set) AUC Score:', roc_auc_score(y_test, clf_default.predict(X_test), average = None, multi_class = 'ovo'))
  File "C:\Anaconda\lib\site-packages\sklearn\metrics\_ranking.py", line 566, in roc_auc_score
    return _multiclass_roc_auc_score(
  File "C:\Anaconda\lib\site-packages\sklearn\metrics\_ranking.py", line 638, in _multiclass_roc_auc_score
    if not np.allclose(1, y_score.sum(axis=1)):
  File "C:\Anaconda\lib\site-packages\numpy\core\_methods.py", line 48, in _sum
    return umr_sum(a, axis, dtype, out, keepdims, initial, where)
numpy.AxisError: axis 1 is out of bounds for array of dimension 1

错误显示在相应的行中

---> 50 print('(Test set) AUC Score:', roc_auc_score(y_test, clf.predict(X_test), average = 'macro', multi_class = 'ovo'))
n53p2ov0

n53p2ov01#

roc_auc_score()应该取类概率或到决策边界的距离(见此处)。SVC使用decision_function(),因此您应该将该行更改为:

print('(Test set) AUC Score:', roc_auc_score(y_test, clf.decision_function(X_test), average = 'macro', multi_class = 'ovo'))

相关问题