在使用anaconda用深度学习框架Keras进行构建神经网络做数据分析时出现报错,显示代码为“index 12 is out of bounds for axis 1 with size 3”

问题遇到的现象和发生背景

在使用anaconda进行构建神经网络做数据分析时出现报错
引用的数据wine.data图如下

img

问题相关代码,请勿粘贴截图
import pandas as pd
from sklearn.model_selection import train_test_split
import pandas,datetime,os
from sklearn import model_selection,metrics,tree,svm
from sklearn.neural_network import MLPClassifier
import matplotlib.pyplot as plt
filename = 'data\wine.data'
columns = ['分类','酒精','苹果酸','灰','灰分的碱度','镁','总酚','黄酮类化合物','非黄烷类酚类','原花色素','颜色强度','色调','稀释葡萄酒的OD280/OD315','脯氨酸']
data = pd.read_csv(filename, header = None,index_col=None,names=columns )
x=data.iloc[:,1:].values.astype(float)
y=data.iloc[:,1].values.astype(int)
x_train,x_test,y_train,y_test=model_selection.train_test_split(x,y,test_size=0.3,random_state=1)

start_time=datetime.datetime.now()
mlp=MLPClassifier(solver='lbfgs',alpha=1e-10,hidden_layer_sizes=[20,100],random_state=1)
mlp.fit(x_train,y_train)
y_train_predict=mlp.predict(x_train)
y_test_predict=mlp.predict(x_test)
print('train_mlp')
print(metrics.classification_report(y_train,y_train_predict))
print(metrics.confusion_matrix(y_train,y_train_predict))
print('\n')
print('\n')
print('\n')
print('test_mlp')
print('Accurancy',mlp.score(x_test,y_test))
end_time=datetime.datetime.now()
time_MLPC=end_time-start_time
print("time:",time_MLPC)
print(metrics.classification_report(y_test,y_test_predict))
print(metrics.confusion_matrix(y_test,y_test_predict))


from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation
# 定义模型结构
model = Sequential()
model.add(Dense(units=16, input_dim=13))
model.add(Activation('relu'))
model.add(Dense(16))
model.add(Activation('relu'))
model.add(Dense(3))
model.add(Activation('softmax'))
#定义损失函数和优化器,并编译
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=["accuracy"])
start_time=datetime.datetime.now()

y_train_ohe = np_utils.to_categorical(y_train, 3)
y_test_ohe = np_utils.to_categorical(y_test, 3)
运行结果及报错内容

问题是出在最后两行,报错如下

Traceback (most recent call last):

  File "C:\Users\HUAWEI\Desktop\数据库\20220512\20220512\作业.py", line 54, in <module>
    y_train_ohe = np_utils.to_categorical(y_train, 3)

  File "D:\anaconda\lib\site-packages\keras\utils\np_utils.py", line 71, in to_categorical
    categorical[np.arange(n), y] = 1

IndexError: index 12 is out of bounds for axis 1 with size 3

img

我的解答思路和尝试过的方法
我想要达到的结果