2017-01-03 1 views
0

numpy.array에 포함 된 데이터가 있습니다 (더 큰 프로젝트에서). 사용자 입력을 기반으로 선택한 축 (dimAxisNr)을 배열의 첫 번째 차원으로 이동하고 사용자 입력 (예 : Select2 및 Select0)과 같은 하나 이상의 (첫 번째 포함하여) 차원을 슬라이싱해야합니다.사용자 정의 입력을 사용하여 numpy 슬라이싱

이 입력을 사용하여 슬라이스하는 데 필요한 정보가 들어있는 DataSelect를 생성합니다. 그러나 슬라이스 배열의 출력 크기는 인라인 인덱싱을 사용하는 출력 크기와 다릅니다. 그래서 기본적으로 입력리스트에서 '37 : 40 : 2 '와'0 : 2 '를 생성 할 방법이 필요합니다. 나는 모든 질문을 이해했는지 모르겠어요

import numpy as np 
dimAxisNr = 1 
Select2 = [37,39] 
Select0 = [0,1] 


plotData = np.random.random((102,72,145,2)) 

DataSetSize = np.shape(plotData) 
DataSelect = [slice(0,item) for item in DataSetSize] 
DataSelect[2] = np.array(Select2) 
DataSelect[0] = np.array(Select0) 


def shift(seq, n): 
    n = n % len(seq) 
    return seq[n:] + seq[:n] 

#Sort and Slice the data 

print(np.shape(plotData)) 
print(DataSelect) 

plotData = np.transpose(plotData, np.roll(range(plotData.ndim),-dimAxisNr)) 
DataSelect = shift(DataSelect,dimAxisNr) 

print(DataSelect) 
print(np.shape(plotData)) 
plotData = plotData[DataSelect] 
print(np.shape(plotData)) 

plotDataDirect = plotData[slice(0, 72, None), 37:40:2, slice(0, 2, None), 0:2] 
print(np.shape(plotDataDirect)) 

답변

0

...

그러나

문제는 내가 좋아하는 색인 ​​목록 [37, 39을 기반으로 슬라이스를 생성하려면 어떻게 "인 경우 , 40,23]? "

는 내가 대답 것이다 :과 같이 오른쪽 인덱스를 선택하는 것입니다 당신은 그냥 목록을 사용할 필요가 없습니다 :

a = np.random.rand(4,5) 
print(a) 
indices = [2,3,1] 
print(a[0:2,indices]) 

주를 그 목록 문제의 분류 : 2 , 3,1]를 [1,2,3]로부터 상이한 결과를 얻을

출력 : I 근래

>>> a 
array([[ 0.47814802, 0.42069094, 0.96244966, 0.23886243, 0.86159478], 
     [ 0.09248812, 0.85569145, 0.63619014, 0.65814667, 0.45387509], 
     [ 0.25933109, 0.84525826, 0.31608609, 0.99326598, 0.40698516], 
     [ 0.20685221, 0.1415642 , 0.21723372, 0.62213483, 0.28025124]]) 
>>> a[0:2,[2,3,1]] 
array([[ 0.96244966, 0.23886243, 0.42069094], 
     [ 0.63619014, 0.65814667, 0.85569145]]) 
0

내 질문에 대한 답변을 찾았습니다. numpy.ix_를 사용해야합니다.

import numpy as np 
dimAxisNr = 1 
Select2 = [37,39] 
Select0 = [0,1] 


plotData = np.random.random((102,72,145,2)) 

DataSetSize = np.shape(plotData) 
DataSelect = [np.arange(0,item) for item in DataSetSize] 

DataSelect[2] = Select2 
DataSelect[0] = Select0 
#print(list(37:40:2)) 

def shift(seq, n): 
    n = n % len(seq) 
    return seq[n:] + seq[:n] 

#Sort and Slice the data 

print(np.shape(plotData)) 
print(DataSelect) 

plotData = np.transpose(plotData, np.roll(range(plotData.ndim),-dimAxisNr)) 
DataSelect = shift(DataSelect,dimAxisNr) 


plotDataSlice = plotData[np.ix_(*DataSelect)] 
print(np.shape(plotDataSlice)) 

plotDataDirect = plotData[slice(0, 72, None), 37:40:2, slice(0, 2, None), 0:1] 
print(np.shape(plotDataDirect)) 
: 여기

는 작업 코드
관련 문제