2013-10-02 3 views
2

그래서 여기 내가 가진 x1vals : 여기파이썬 벡터의 특정 인덱스 목록에서 구성 요소를 선택하는 방법은 무엇입니까?

>>> x1Vals 
[-0.33042515829906227, -0.1085082739900165, 0.93708611747433213, -0.19289496973017362, -0.94365384912207761, 0.43385903975568652, -0.46061140566051262, 0.82767432358782367, -0.24257307936591843, -0.1182761514447952, -0.29794617763330011, -0.87410892638408, -0.34732294121174467, 0.40646145339571249, -0.64082861589870865, -0.45680189916940073, 0.4688889876175073, -0.89399689430691298, 0.53549621114138612] 

그리고 내가

>>> np.where(np.dot(XValsOnly,newweights) > 0) 

>>>(array([ 1, 2, 4, 5, 6, 8, 9, 13, 15, 16]),) 

을 선택합니다 그러나 x1Vals 지수의 목록입니다 나는 x1Vals의 값을 matlab에 방법을 얻을 때, 나는 이 오류가 발생합니다 :

>>> x1Vals[np.where(np.dot(XValsOnly,newweights) > 0)] 

Traceback (most recent call last): 
    File "<pyshell#69>", line 1, in <module> 
    x1Vals[np.where(np.dot(XValsOnly,newweights) > 0)] 
TypeError: list indices must be integers, not tuple 
>>> np.where(np.dot(XValsOnly,newweights) > 0) 

이 방법이 있습니까?

답변

1

x1Vals은 공상 색인 생성을 지원하지 않는 list 개체입니다. 배열을 만들어야합니다.

x1Vals = np.array(x1Vals) 

그리고 접근 방법이 효과적 일 것입니다.

더 빠른 방법은 np.take을 사용하는 것입니다 :

np.take(x1Vals, np.where(np.dot(XValsOnly,newweights) > 0)) 
+0

또는 더 간단하고 더 빨리 take','x1Vals'보다는 [np.dot (XValsOnly, newweights)> 0]'. 또한 여기에서'take '는'(N,)'대신에'(1, N)'모양을 반환합니다. – askewchan

관련 문제