You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何重塑Predict和Target列表形状以适配Matplotlib绘图?

解决数组形状匹配问题以用于Matplotlib绘图

你可以用NumPy调整两个数组的形状,让它们维度一致,以下是两种简单可行的方案:

方案1:把一维的Predict改成二维(和Target形状对齐)

先把原生列表转成NumPy数组,再用reshape方法改成(1,4)的形状:

import numpy as np

# 转成NumPy数组
Predict = np.array([50.47426986694336, 50.587158203125, 50.58975601196289, 50.911197662353516])
Target = np.array([[51.339996337890625, 51.79999923706055, 51.09000015258789, 64.86000061035156]])

# 重塑Predict为(1,4)的二维数组
Predict_reshaped = Predict.reshape(1, 4)
# 现在两者形状完全一致,可直接用于绘图

方案2:把二维的Target改成一维(和Predict形状对齐)

用flatten()或者ravel()方法直接把二维数组展平成一维:

import numpy as np

Predict = np.array([50.47426986694336, 50.587158203125, 50.58975601196289, 50.911197662353516])
Target = np.array([[51.339996337890625, 51.79999923706055, 51.09000015258789, 64.86000061035156]])

# 展平Target为一维数组
Target_flattened = Target.flatten()
# 现在两者形状匹配,能直接绘图

绘图验证示例

调整完形状后,直接用Matplotlib就能绘图,比如画折线图:

import matplotlib.pyplot as plt

# 用方案1的情况
plt.plot(Predict_reshaped[0], label='预测值')
plt.plot(Target[0], label='目标值')

# 用方案2的情况可以换成下面两行
# plt.plot(Predict, label='预测值')
# plt.plot(Target_flattened, label='目标值')

plt.legend()
plt.show()

内容的提问来源于stack exchange,提问作者Mouna Ahmen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 18:42:05