【问题标题】:Fastest way to get elements from a numpy array and create a new numpy array从 numpy 数组中获取元素并创建新的 numpy 数组的最快方法
【发布时间】:2014-06-28 14:05:04
【问题描述】:

我有 numpy 数组,名为 data,维度为 150x4 我想通过从data 中选择随机元素来创建一个名为mean 的新numpy 数组,其维度为3x4

我目前的实现是:

    cols = (data.shape[1])
    K=3
    mean = np.zeros((K,cols))
    for row in range(K):
        index = np.random.randint(data.shape[0])
        for col in range(cols):
            mean[row][col] = data[index][col]

有没有更快的方法来做同样的事情?

【问题讨论】:

    标签: python python-2.7 python-3.x numpy scipy


    【解决方案1】:

    您可以在numpy.randint(第三个参数)中指定随机整数的数量。此外,您应该熟悉numpy.array 的索引符号。在这里,您可以通过: 说明符访问一行中的所有元素。

    mean = data[np.random.randint(0,len(data),3),:]
    

    【讨论】:

      猜你喜欢
      • 2016-02-16
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2011-01-07
      • 2017-12-30
      • 2021-01-01
      • 2017-10-29
      相关资源
      最近更新 更多