神经网络输入层和隐藏层神经元数量从20改为5时,这段代码怎么改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'])
时间: 2023-11-28 16:04:53 浏览: 114
当神经网络输入层和隐藏层神经元数量从20改为5时,由于特征数量仍然为4,因此输入层神经元数量应该仍然为4,隐藏层神经元数量应该为5。因此,你需要将代码中的列名从5个改为4个,即去掉隐藏层的神经元数量这一列。修改后的代码如下:
```
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'])
```
这样修改后,特征列的数量与神经网络的输入层神经元数量相匹配,可以正常运行。
相关问题
神经网络输入层和隐藏层神经元从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()
当你将输入层和隐藏层神经元从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. 在绘制图像时,需要将颜色列表中的黄色去掉,因为在数据集中只有蓝色、绿色和红色三种颜色,没有黄色。
神经网络输入层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()
可能是因为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()
```
阅读全文