在Numpy Array列上应用条件/过滤器

时间:2017-12-25 07:37:26

标签: python numpy matplotlib machine-learning

我有2个Numpy数组,第1行有210行,第2行有30行,两个都包含4列,我想在两个数组的第4列上应用条件/过滤器,只包含0或1。 所以,我想检测第一个数组中的0作为Train_Safe,第一个数组中的1个作为Train_Cracked,第二个数组中的0个作为Test_Safe,第二个数组中的一个作为Test_Cracked,并使用Matplotlib在3D散点图上绘制这些值,我尝试使用此代码:

    for i in X_train_merge[0:, 3]:
    if i == 0:
        x_vals_train_0 = X_train_merge[0:, 0:1]
        y_vals_train_0 = X_train_merge[0:, 1:2]
        z_vals_train_0 = X_train_merge[0:, 2:3]
    elif i == 1:
        x_vals_train_1 = X_train_merge[0:, 0:1]
        y_vals_train_1 = X_train_merge[0:, 1:2]
        z_vals_train_1 = X_train_merge[0:, 2:3]
for j in X_test_merge[0:, 3]:
    if j == 0:
        x_vals_test_0 = X_test_merge[0:, 0:1]
        y_vals_test_0 = X_test_merge[0:, 1:2]
        z_vals_test_0 = X_test_merge[0:, 2:3]
    elif j == 1:
        x_vals_test_1 = X_test_merge[0:, 0:1]
        y_vals_test_1 = X_test_merge[0:, 1:2]
        z_vals_test_1 = X_test_merge[0:, 2:3]

ax.scatter(x_vals_train_0, y_vals_train_0, z_vals_train_0, c='g', marker='o', label="Train_Safe")
ax.scatter(x_vals_train_1, y_vals_train_1, z_vals_train_1, c='b', marker='o', label="Train_Cracked")
ax.scatter(x_vals_test_0, y_vals_test_0, z_vals_test_0, c='black', marker='*', label="Test_Safe")
ax.scatter(x_vals_test_1, y_vals_test_1, z_vals_test_1, c='brown', marker='*', label="Test_Cracked")

它绘制/给出所有数据点而不将其分解/分割为Train_Safe,Train_Cracked,Test_Safe和Test_Cracked。任何有关此任务的建议/解决方案。在此先感谢。

1 个答案:

答案 0 :(得分:1)

礼貌地提供玩具数据

np.where

然后ts = a[np.where(a[:, -1] == 0), :-1].T tc = a[np.where(a[:, -1] == 1), :-1].T from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(*ts, c='g', marker='o', label="Train_Safe") ax.scatter(*tc, c='b', marker='o', label="Train_Cracked") fig.show() 是工具:

Books obj= new Books();

enter image description here