神经网络输入层4个神经元,隐藏层4个神经元,输出层1个神经元,这段代码报错数据维度错误,应该怎么改def result_visualization(x_test, y_test, result): cols = y_test.shape[1] y = [] pre = [] # 反转换类别的独热编码 for i in range(cols): if y_test[0][i] == 0: y.append('setosa') elif y_test[0][i] == 1: y.append('versicolor') else: y.append('virginica') for j in range(cols): if result[0][j] == 0: pre.append('setosa') elif result[0][j] == 1: pre.append('versicolor') else: pre.append('virginica') # 将特征和类别矩阵拼接起来 real = np.column_stack((x_test.T, y)) prediction = np.column_stack((x_test.T, pre)) # 转换成DataFrame类型,并添加columns df_real = pd.DataFrame(real, index=None, columns=['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width']) df_prediction = pd.DataFrame(prediction, index=None, columns=['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width']) # 将特征列转换为float类型,否则radviz会报错 df_real[['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width']] = df_real[['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width']].astype(float) df_prediction[['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width']] = df_prediction[['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width']].astype(float) # 绘图 plt.figure('真实分类') radviz(df_real, 'Species', color=['blue', 'green', 'red']) plt.figure('预测分类') radviz(df_prediction, 'Species', color=['blue', 'green', 'red']) plt.show()
时间: 2023-08-10 12:05:26 浏览: 174
由S个神经元组成的单层网络-MATLAB神经网络工具箱中的神经网络模型
可能是因为y_test的维度不是二维的,而是一维的,导致使用shape属性获取y_test的列数出错。可以在代码中加入以下语句,将y_test转换为二维的形式:
```
if len(y_test.shape) == 1:
y_test = y_test.reshape(-1, 1)
```
这段代码的作用是:如果y_test的维度是一维的,将其转换为二维的形式,行数自动计算。如果y_test的维度已经是二维的,不做任何操作。修改后的完整代码如下:
```
def result_visualization(x_test, y_test, result):
if len(y_test.shape) == 1:
y_test = y_test.reshape(-1, 1)
cols = y_test.shape[1]
y = []
pre = []
# 反转换类别的独热编码
for i in range(cols):
if y_test[0][i] == 0:
y.append('setosa')
elif y_test[0][i] == 1:
y.append('versicolor')
else:
y.append('virginica')
for j in range(cols):
if result[0][j] == 0:
pre.append('setosa')
elif result[0][j] == 1:
pre.append('versicolor')
else:
pre.append('virginica')
# 将特征和类别矩阵拼接起来
real = np.column_stack((x_test.T, y))
prediction = np.column_stack((x_test.T, pre))
# 转换成DataFrame类型,并添加columns
df_real = pd.DataFrame(real, index=None, columns=['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width', 'Species'])
df_prediction = pd.DataFrame(prediction, index=None, columns=['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width', 'Species'])
# 将特征列转换为float类型,否则radviz会报错
df_real[['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width']] = df_real[['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width']].astype(float)
df_prediction[['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width']] = df_prediction[['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width']].astype(float)
# 绘图
plt.figure('真实分类')
radviz(df_real, 'Species', color=['blue', 'green', 'red'])
plt.figure('预测分类')
radviz(df_prediction, 'Species', color=['blue', 'green', 'red'])
plt.show()
```
阅读全文