df_real = pd.DataFrame(real, index=None, columns=['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width', 'Species'])
时间: 2024-01-18 09:01:54 浏览: 27
这段代码是使用 Pandas 库中的 DataFrame 函数创建了一个名为 df_real 的数据框,其中包含了一些被称为鸢尾花数据集的真实数据,这些数据包括四个特征(萼片长度、萼片宽度、花瓣长度和花瓣宽度)以及对应的鸢尾花的种类。数据框中的每一行代表一朵鸢尾花,每一列代表一个特征或者种类。该数据框的索引被设置为默认值。
相关问题
神经网络输入层和隐藏层神经元数量从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'])
当神经网络输入层和隐藏层神经元数量从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. 在绘制图像时,需要将颜色列表中的黄色去掉,因为在数据集中只有蓝色、绿色和红色三种颜色,没有黄色。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)