修正以下代码ntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans(train_X.T, 3, 2, error=0.005, maxiter=1000, init=None) train_u, _, _, _, _, _, = fuzz.cluster.cmeans_predict(train_X.T, cntr, 2, error=0.005, maxiter=1000) train_predictions = np.argmax(train_u, axis=0) train_accuracy = accuracy_score(train_y, train_predictions) print(train_y) print(train_predictions) train_y = np.array(train_y) train_u = np.array(train_u) train_auc = roc_auc_score(train_y, train_u, multi_class='ovo') loss = np.sum((train_u - train_y) ** 2) loss_curve.append(loss) accuracy_curve.append(train_accuracy) auc_curve.append(train_auc)
时间: 2023-10-06 09:06:41 浏览: 96
修正后的代码如下:
```
cntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans(train_X.T, 3, 2, error=0.005, maxiter=1000, init=None)
train_u, _, _, _, _, _, = fuzz.cluster.cmeans_predict(train_X.T, cntr, 2, error=0.005, maxiter=1000)
train_predictions = np.argmax(train_u, axis=0)
train_accuracy = accuracy_score(train_y, train_predictions)
print(train_y)
print(train_predictions)
train_y = np.array(train_y)
train_u = np.array(train_u)
train_auc = roc_auc_score(train_y, train_u.T, multi_class='ovo')
loss = np.sum((train_u - train_y.reshape(-1,1)) ** 2)
loss_curve.append(loss)
accuracy_curve.append(train_accuracy)
auc_curve.append(train_auc)
```
主要修改了两处错误:
1. 在计算 ROC AUC 时,需要将 `train_u` 转置,而且 `train_y` 和 `train_u` 都应该是二维数组。
2. 在计算 loss 时,需要将 `train_y` 改为二维数组,并且使用 `reshape` 方法将其转换为列向量。同时,因为两个数组的形状不同,需要使用广播机制进行计算。
阅读全文