【问题标题】:Stacking numpy array A corresponding to array B堆叠numpy数组A对应于数组B
【发布时间】:2020-05-31 12:11:19
【问题描述】:

我有一个输入特征 X 的 numpy 数组(ndarray,每个形状 (1, 10, 4)),以及一个对应于类的标签数组 y

现在我想通过将X 中与y 中的class 0 对应的所有输入特征、y 中与class 1 对应的特征等堆叠在一起来创建一个新的数组数组。 . 这样就形成了一个新的数组数组,其中嵌套的数组等于数字类。

作为一个最小的例子,假设我有:

X = np.random.randn(200, 1, 10, 4)
a = np.zeros(100, dtype=int)
b = np.ones(100, dtype=int)
y = np.hstack((a,b))

所以,

print(X.shape)
print(y.shape)
(200, 1, 10, 4)
(200,)

然后新数组应该是这样的:

Final = [array(#all_features_of class_0), array(#all_features_of_class_1)...]

我的目的是为每个类绘制这些特征以了解它们的分布。

如果有帮助的话,每一个观察都有4 特征,所以(1, 10, 4) 中的4

【问题讨论】:

    标签: python arrays numpy multidimensional-array


    【解决方案1】:

    我的理解是否正确,这是你想要的吗?

    class0, class1=[],[]
    for i in range(len(y)):
    if y[i]==0:
        class0.append(X[i])
    else:
        class1.append(X[i])
    Final = (np.array(class0) , np.array(class1))
    

    【讨论】:

    • 是的,但是 len(class0) 给出 100len(class1) 给出 1 ,并且在 MWE 中,所有类都有 100 个元素。..
    • 两个类都是 len 100,正如我在输出中看到的那样,它只取决于 0 和 1 数组 y 的数量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 2018-06-05
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多