%% 绘图 result = {T;A{1};Y{20}}; figure for p = 1:3 for k = 1:5 subplot(3,5,(p-1)*5+k) temp = result{p}(:,(k-1)*5+1:k*5); [m,n] = size(temp); for i = 1:m for j = 1:n if temp(i,j) > 0 plot(j,m-i,'ko','MarkerFaceColor','k'); else plot(j,m-i,'ko'); end hold on end end axis([0 6 0 12]) axis off if p == 1 title(['class' num2str(k)]) elseif p == 2 title(['pre-sim' num2str(k)]) else title(['sim' num2str(k)]) end end end
时间: 2024-02-14 13:26:50 浏览: 25
这段代码用于绘制图像。首先,将 `T`、`A{1}` 和 `Y{20}` 存储在 `result` 中。
接下来,创建一个图形窗口,并使用嵌套的 `for` 循环来生成子图。外层循环控制绘制的行数,内层循环控制绘制的列数。
在每个子图中,首先从 `result` 中提取相应的数据矩阵 `temp`,然后获取其大小。接着,使用嵌套的 `for` 循环遍历矩阵中的每个元素,并根据元素的值绘制相应的数据点。如果元素大于零,则绘制黑色实心圆点;否则,绘制黑色空心圆点。
设置坐标轴范围和关闭坐标轴显示。
最后,根据当前子图所在的行数 `p`,设置相应的标题。如果 `p` 为 1,则标题为 `'class'` 加上列数;如果 `p` 为 2,则标题为 `'pre-sim'` 加上列数;如果 `p` 为 3,则标题为 `'sim'` 加上列数。
这样,通过循环绘制了一个包含多个子图的图形窗口。
请注意,由于缺少完整的代码和数据,无法提供更详细的解释。如果需要进一步的帮助,请提供更多的上下文或代码片段。
相关问题
w = 60 h = 40 fig = plt.figure(figsize=(18, 10)) columns = 4 rows = 2 for i in range(len(mis_class)): ax = fig.add_subplot(rows, columns, i + 1) ax.set_title("Predicted result:" + Transfername(lists[mis_class[i]]) + "\n" + " Actual result: " + Transfername(Y_test[mis_class[i]])) plt.imshow(X_test[mis_class[i]], interpolation='nearest') plt.show() 对以上代码进行注释分析,写在每行后面
# 设置图片大小为宽60,高40
w = 60
h = 40
# 创建一个名为fig的Figure对象,并且设置其大小为18*10英寸
fig = plt.figure(figsize=(18, 10))
# 设置绘图区域的列数为4,行数为2
columns = 4
rows = 2
# 遍历mis_class列表,其中i为当前遍历的索引
for i in range(len(mis_class)):
# 在绘图区域中添加一个子图,位置由行数、列数、索引决定
ax = fig.add_subplot(rows, columns, i + 1)
# 设置子图的标题,包括预测结果和实际结果
ax.set_title("Predicted result:" + Transfername(lists[mis_class[i]])
+ "\n" + " Actual result: " + Transfername(Y_test[mis_class[i]]))
# 在子图中显示第mis_class[i]个测试集样本的图片
plt.imshow(X_test[mis_class[i]], interpolation='nearest')
# 显示绘图区域中的所有子图
plt.show()
神经网络输入层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()
```
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)