1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 作图:plt.scatter() “x and y must be the same size“

作图:plt.scatter() “x and y must be the same size“

时间:2020-08-17 00:36:14

相关推荐

作图:plt.scatter() “x and y must be the same size“

目的:绘制真实值-预测值的散点图,横坐标为“样本编号”, 纵坐标为“y值”

预期图像:

Bug描述:

def scatter_true_pred(y_true, y_predict):num_test = y_true.shape[0]plt.figure(figsize=(30, 10), dpi= 200, facecolor='w', edgecolor='k')plt.xticks(fontsize=12);plt.yticks(fontsize=12)plt.xlabel("样本编号", fontsize=20);plt.ylabel("翘曲值(归一化后)", fontsize=20)plt.title("预测值与测试样本",fontsize=30)print("num_test", num_test, "num_y_true", y_true.shape[0])true_point = plt.scatter(num_test, y_true, color='blue', marker='o')pre_point = plt.scatter(num_test, y_predict, color='green', marker='^')#添加图例plt.legend((true_point, pre_point), ("真实值","预测值"), loc='upper left')

这行num_test = y_true.shape[0] 中的num_test的类型是一个单独的整数,无法对应所有的样本编号, scatter 函数要求传入一个数组, 如ndarray

修改方案:

#将数字转化为数组,该数组包含从1~最大样本编号的所有值num_test = np.arange(1, y_predict.shape[0]+1, 1)

注意!!! arange(起步值, 最终值, 步长) 是左闭右开的, 返回值类型是ndarray

test_arange = np.arange(1, 10, 1)test_arange, type(test_arange)#(array([1, 2, 3, 4, 5, 6, 7, 8, 9]), numpy.ndarray)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。