【问题标题】:Fill rows of an (10000,2) array using another array's values as indices使用另一个数组的值作为索引填充 (10000,2) 数组的行
【发布时间】:2021-12-10 01:03:48
【问题描述】:

我有一个数组 idnl,其中包含从 0 到 10000 的非连续数字

u = rng.integers(0, 100, size = N)/100.0
temp = (np.cumsum(priors))
thresholds = [temp[0], temp[1],1]
indl = (np.argwhere(u <= thresholds[l]))
Nl = np.size(indl)

数组 x 是 [10000, 2] 用零填充。

x = np.zeros((n,N)).transpose()

我想生成随机数的正态分布并将每个有序对复制到 x 中由 indl 索引的行。 matlab 代码为x(:,indl) = mvrnd(...etc..)

x[indl,:] = rng.multivariate_normal(meanVectors[l,:], covMatrices[l,:,:], Nl)

x 应该是这样的 -

[0,0
0,0
0.53, 0.98
0,0
0.46, 0.56
0.87, 0.23
.
.
.]

但我不断收到这样的错误:

ValueError: shape mismatch: value array of shape (5106,2)  could not be broadcast to indexing result of shape (1,5106,10000)

我一直在这为hoooours....请帮助!

【问题讨论】:

    标签: python arrays numpy matlab data-science


    【解决方案1】:

    看看 numpy.reshape>

    我会用

    np.reshape(x, (-1:2))
    

    这将创建一个二维列表。

    [[0,0], [0,0], [0.53, 0.98] ....]
    

    然后您可以使用 for 循环遍历列表。

    for arr_element in x:
        print(arr_element[0], arr_element[1])
    

    【讨论】:

      猜你喜欢
      • 2017-05-17
      • 2019-09-23
      • 2020-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-13
      • 2018-01-22
      • 1970-01-01
      相关资源
      最近更新 更多