在python中使用scatter3D绘制列表或数组的列

时间:2018-02-01 10:43:27

标签: python scikit-learn

我想尝试一些高斯过程回归的测试,用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

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我猜这个问题是由数组数组切片造成的。如果我把它带到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')