神经网络输入层和隐藏层神经元从20改为5时,这一段代码怎么改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('virginica') for j in range(cols): if result[0][j] == 0: pre.append('setosa') elif result[0][j] == 1: pre.append('virginica') else: pre.append('unknown') # 将特征和类别矩阵拼接起来 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', 'yellow']) plt.figure('预测分类') radviz(df_prediction, 'Species', color=['blue', 'green', 'red', 'yellow']) plt.show()
时间: 2023-08-17 22:05:14 浏览: 158
Neural-Networks-Visualization:使用P5.JS和Synaptic.js用神经网络模拟二进制分类器
当你将输入层和隐藏层神经元从20改为5时,你需要对这段代码进行如下修改:
```python
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', '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()
```
主要的修改如下:
1. 在反转换类别的独热编码时,需要将第三类的判断也加上,因为数据集中共有三个类别。
2. 在绘制图像时,需要将颜色列表中的黄色去掉,因为在数据集中只有蓝色、绿色和红色三种颜色,没有黄色。
阅读全文