目的:绘制真实值-预测值的散点图,横坐标为“样本编号”, 纵坐标为“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)

 

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐