Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否有一种简单的方法可以一次对数组中的几个元素进行采样?_Python_Numpy - Fatal编程技术网

Python 是否有一种简单的方法可以一次对数组中的几个元素进行采样?

Python 是否有一种简单的方法可以一次对数组中的几个元素进行采样?,python,numpy,Python,Numpy,为了更快地进行一阶分析,我需要对一组数据进行采样。如果我想一次检查一个点,我可以通过切片来完成: import numpy as np array = np.arange(0,20000) samplespace = 5000 sampledarray = array[::samplespace] 但是,我需要分析几个元素。我能想到的唯一方法是使用for循环: import numpy as np array = np.arange(0,20000) samplespace = 5000 n

为了更快地进行一阶分析,我需要对一组数据进行采样。如果我想一次检查一个点,我可以通过切片来完成:

import numpy as np
array = np.arange(0,20000)
samplespace = 5000
sampledarray = array[::samplespace]
但是,我需要分析几个元素。我能想到的唯一方法是使用for循环:

import numpy as np
array = np.arange(0,20000)
samplespace = 5000
n = 3
sampledarray = array[0::samplespace]
for i in range(1,n):
    arraysample_i = array[i::samplespace]
    indices = np.linspace(i,len(sampledarray),len(arraysample_i)).astype(int)
    sampledarray = np.insert(sampledarray,indices,arraysample_i)
print(sampledarray)
>>> [ 0 1 2 5000 5001 5002 10000 10001 10002 15000 15001 15002 ]
我担心如果我使用大数组并在多个维度中进行采样,那么循环运行会花费很多时间。有没有更简单、更快的方法来实现这一点?

将numpy作为np导入
进口itertools
数组=np.arange(020000)
样本空间=5000
INDEX=itertools.chain.from_iterable((ind表示范围内的ind(i,len(数组),samplespace))表示范围内的i(n))
输出=数组[列表(索引)]
输出:

array([    0,  5000, 10000, 15000,     1,  5001, 10001, 15001,     2,
        5002, 10002, 15002])
array([    0,     1,     2,  5000,  5001,  5002, 10000, 10001, 10002,
       15000, 15001, 15002])
输出:

array([    0,  5000, 10000, 15000,     1,  5001, 10001, 15001,     2,
        5002, 10002, 15002])
array([    0,     1,     2,  5000,  5001,  5002, 10000, 10001, 10002,
       15000, 15001, 15002])

感谢Amird建议使用itertools;这是一个我以前没用过的软件包,但最终给了我所需要的

下面是我最后做的:

import numpy as np
import itertools
array = np.arange(0,20000)
samplespace = 5000
n = 3

iterate = itertools.count(start=0,step=samplespace)
num = int(len(array)/samplespace)
idx = np.array([next(iterate) for _ in range(num)])
idxlist = np.zeros(0)
for i in range(n):
    idxi = np.copy(idx)+i
    idxlist = np.append(idxlist,idxi)
idxlist = np.sort(idxlist).astype(int)

sampledarray = array[idxlist]
print(sampledarray)
>>> [ 0 1 2 5000 5001 5002 10000 10001 10002 15000 15001 15002 ]
这使我能够相当轻松地将其扩展到更多维度,而不是使用我的大型数据集,我只需操作索引数组:

import numpy as np
import itertools
array = np.empty((200,200),dtype=object)
# I know this is a lousy way to define the array but it works well for illustrative purposes
for i in range(200):
    for j in range(200):
        array[i,j] = (i,j)
samplespace = 50
n = 3

iteratex = itertools.count(start=0,step=samplespace)
iteratey = itertools.count(start=0,step=samplespace)
num = int(len(array)/samplespace)
idxx = np.array([next(iteratex) for _ in range(num)])
idxy = np.array([next(iteratey) for _ in range(num)])
idxlistx = np.zeros(0)
idxlisty = np.zeros(0)
for i in range(n):
    idxxi = np.copy(idxx)+i
    idxyi = np.copy(idxy)+i
    idxlistx = np.append(idxlistx,idxxi)
    idxlisty = np.append(idxlisty,idxyi)
idxlistx = np.sort(idxlistx).astype(int)
idxlisty = np.sort(idxlisty).astype(int)

# Having to index the array twice seems awkward, even though I understand it is necessary 
# for array broadcasting if the two index arrays are of different lengths
sampledarray = array[idxlistx,:]
sampledarray = sampledarray[:,idxlisty]
print(sampledarray)

>>>[[(0, 0) (0, 1) (0, 2) (0, 50) (0, 51) (0, 52) (0, 100) (0, 101) (0, 102)
  (0, 150) (0, 151) (0, 152)]
 [(1, 0) (1, 1) (1, 2) (1, 50) (1, 51) (1, 52) (1, 100) (1, 101) (1, 102)
  (1, 150) (1, 151) (1, 152)]
 [(2, 0) (2, 1) (2, 2) (2, 50) (2, 51) (2, 52) (2, 100) (2, 101) (2, 102)
  (2, 150) (2, 151) (2, 152)]
 [(50, 0) (50, 1) (50, 2) (50, 50) (50, 51) (50, 52) (50, 100) (50, 101)
  (50, 102) (50, 150) (50, 151) (50, 152)]
 [(51, 0) (51, 1) (51, 2) (51, 50) (51, 51) (51, 52) (51, 100) (51, 101)
  (51, 102) (51, 150) (51, 151) (51, 152)]
 [(52, 0) (52, 1) (52, 2) (52, 50) (52, 51) (52, 52) (52, 100) (52, 101)
  (52, 102) (52, 150) (52, 151) (52, 152)]
 [(100, 0) (100, 1) (100, 2) (100, 50) (100, 51) (100, 52) (100, 100)
  (100, 101) (100, 102) (100, 150) (100, 151) (100, 152)]
 [(101, 0) (101, 1) (101, 2) (101, 50) (101, 51) (101, 52) (101, 100)
  (101, 101) (101, 102) (101, 150) (101, 151) (101, 152)]
 [(102, 0) (102, 1) (102, 2) (102, 50) (102, 51) (102, 52) (102, 100)
  (102, 101) (102, 102) (102, 150) (102, 151) (102, 152)]
 [(150, 0) (150, 1) (150, 2) (150, 50) (150, 51) (150, 52) (150, 100)
  (150, 101) (150, 102) (150, 150) (150, 151) (150, 152)]
 [(151, 0) (151, 1) (151, 2) (151, 50) (151, 51) (151, 52) (151, 100)
  (151, 101) (151, 102) (151, 150) (151, 151) (151, 152)]
 [(152, 0) (152, 1) (152, 2) (152, 50) (152, 51) (152, 52) (152, 100)
  (152, 101) (152, 102) (152, 150) (152, 151) (152, 152)]]