【问题标题】:Plotting columns of an list or array with scatter3D in python在python中使用scatter3D绘制列表或数组的列
【发布时间】:2018-02-01 19:50:00
【问题描述】:

我想在 scikit-learn 中尝试使用不同内核的高斯过程回归进行一些测试并绘制结果。我还使用LeaveOneOut 函数来训练 GPR,然后对一个输入数据进行测试。

输入和目标数据是np.arrray,从文件中读取,两者都是(5x3):

inputData = np.array([[30.1678, -173.569, 725.724],
 [29.9895, -173.34,  725.76 ],
 [29.9411, -173.111, 725.768],
 [29.9306, -173.016, 725.98 ],
 [29.6754, -172.621, 725.795]])

targetData = np.array([[14.8016, -175.911, 779.752],
[14.7319, -175.483, 779.504],
[14.5022, -175.087, 779.388],
[14.4904, -174.576, 779.416],
[14.4881, -174.058, 779.452]])

使用LeaveOneOut,我使用 (4x3) 输入数据训练 GPR,并迭代测试 (1x3) 输入数据,并将其附加到 listnp.array 中。在指定内核的 for 循环中完成 LeaveOneOut 之后,我想用 scatter3Dplot3D 绘制预测。但是我收到以下错误:

ax1.scatter3D(inputDataTrainAppend[:, 0], inputDataTrainAppend[:, 1], inputDataTrainAppend[:, 2], s=15, c='b', label=u'InputData')
TypeError: list indices must be integers or slices, not tuple 

如果我将每个预测附加到一个列表中并将其转换为np.array,那么我得到:

ax1.plot3D(inputDataTrainAppendArr[:, 0], inputDataTrainAppendArr[:, 1], inputDataTrainAppendArr[:, 2], 'b:', label=u'InputDataLine')

op_flags=[op_flag], itershape=shape, order='C').itviews[0]
ValueError: input operand has more dimensions than allowed by the axis remapping

我的测试脚本如下:

loo = LeaveOneOut()
inputDataTrainAppend= []
inputDataTestAppend= []

kernels= [
      1.0 * RBF(length_scale=1.0, length_scale_bounds=(1e-1, 10.0)),
      1.0 * RBF(length_scale=100.0, length_scale_bounds=(1e-2, 1e3)) + WhiteKernel(noise_level=1, noise_level_bounds=(1e-10, 1e+1)),
      1.0 * ExpSineSquared(length_scale=1.0, periodicity=3.0, length_scale_bounds=(0.1, 10.0), periodicity_bounds=(1.0, 10.0)),
      1.0 * Matern(length_scale=1.0, length_scale_bounds=(1e-1, 10.0), nu=1.5)
        ]

for index, kernel in enumerate(kernels):

    for trainIdx, testIdx in loo.split(inputData):

        inputDataTrain= inputData[trainIdx]
        inputDataTest= inputData[testIdx]

        inputDataTrainAppend.append(inputDataTrain)
        inputDataTestAppend.append(inputDataTest)


    plt.figure(num= None, figsize= (50, 50), dpi= 100, facecolor= 'w', edgecolor= 'k')
    ax1 = plt.axes(projection='3d')

    #inputDataTrainAppendArr= np.array(inputDataTrainAppend)
    #inputDataTestAppendArr= np.array(inputDataTestAppend)

    ax1.scatter3D(inputDataTrainAppend[:, 0], inputDataTrainAppend[:, 1], inputDataTrainAppend[:, 2], s=15, c='b', label=u'InputData')
    ax1.plot3D(inputDataTrainAppend[:, 0], inputDataTrainAppend[:, 1], inputDataTrainAppend[:, 2], 'b:', label=u'InputDataLine')

    #ax1.scatter3D(inputDataTrainAppendArr[:, 0], inputDataTrainAppendArr[:, 1], inputDataTrainAppendArr[:, 2], s=15, c='b', label=u'InputData')
    #ax1.plot3D(inputDataTrainAppendArr[:, 0], inputDataTrainAppendArr[:, 1], inputDataTrainAppendArr[:, 2], 'b:', label=u'InputDataLine')

    ax1.set_xlabel('$x$')
    ax1.set_ylabel('$y$')
    ax1.set_zlabel('$z$')
    ax1.set_xlim(-200, 800)
    ax1.set_ylim(-200, 800)
    ax1.set_zlim(-200, 800)
    plt.legend(loc='upper left')
    plt.show()

如果我还想打印附加列表的每一列,我会得到

print('inputDataTrainAppend[: 0]:', inputDataTrainAppend[:, 0], '\n')
print('inputDataTrainAppend[: 1]:', inputDataTrainAppend[:, 1], '\n')
print('inputDataTrainAppend[: 2]:', inputDataTrainAppend[:, 2], '\n') 



inputDataTestAppend[: 0]: [[  30.1678 -173.569   725.724 ]
[  29.9895 -173.34    725.76  ]
[  29.9411 -173.111   725.768 ]
[  29.9306 -173.016   725.98  ]
[  29.6754 -172.621   725.795 ]]

print('inputDataTrainAppend[: 1]:', inputDataTrainAppend[:, 1], '\n')
IndexError: index 1 is out of bounds for axis 1 with size 1

感谢您的帮助

【问题讨论】:

    标签: python scikit-learn


    【解决方案1】:

    我猜问题出在数组数组的切片上。如果我用numpy.ndarray.flatten 把它调暗一点,它就会起作用

    ax1.scatter3D(inputDataTrainAppendArr[..., 0].flatten(), inputDataTrainAppendArr[..., 1].flatten(), inputDataTrainAppendArr[..., 2].flatten(), s=15, c='lime', label=u'PredictedData')
    ax1.plot3D(inputDataTrainAppendArr[..., 0].flatten(), inputDataTrainAppendArr[..., 1].flatten(), inputDataTrainAppendArr[..., 2].flatten(), 'lime', label=u'PredictedDataLine')
    

    【讨论】:

      猜你喜欢
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 2013-08-29
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 2018-08-29
      相关资源
      最近更新 更多