sourcecode

TypeError: 정수 스칼라 배열만 1D numpy 인덱스 배열로 스칼라 인덱스로 변환할 수 있습니다.

codebag 2023. 5. 8. 22:12
반응형

TypeError: 정수 스칼라 배열만 1D numpy 인덱스 배열로 스칼라 인덱스로 변환할 수 있습니다.

저는 제공된 bin 확률을 기반으로 훈련 세트에서 요소를 무작위로 선택하는 함수를 작성하고 싶습니다.저는 설정된 인덱스를 11개의 빈으로 나눈 다음 이에 대한 사용자 정의 확률을 만듭니다.

bin_probs = [0.5, 0.3, 0.15, 0.04, 0.0025, 0.0025, 0.001, 0.001, 0.001, 0.001, 0.001]

X_train = list(range(2000000))

train_probs = bin_probs * int(len(X_train) / len(bin_probs)) # extend probabilities across bin elements
train_probs.extend([0.001]*(len(X_train) - len(train_probs))) # a small fix to match number of elements
train_probs = train_probs/np.sum(train_probs) # normalize
indices = np.random.choice(range(len(X_train)), replace=False, size=50000, p=train_probs)
out_images = X_train[indices.astype(int)] # this is where I get the error

다음 오류가 발생합니다.

TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array

제가 작성한 인덱스 배열을 이미 확인했기 때문에 이것이 이상하다고 생각합니다.그것은 1-D이고, 정수이고, 스칼라입니다.

제가 무엇을 빠뜨리고 있나요?

참고: 합격을 시도했습니다.indices와 함께astype(int)같은 오류입니다.

아마도 오류 메시지는 다소 오해를 불러일으킬 수 있지만, 요점은X_trainnumpy 배열이 아닌 목록입니다.어레이 인덱싱을 사용할 수 없습니다.먼저 배열로 만들기:

out_images = np.array(X_train)[indices.astype(int)]

사용할 때마다 이 오류가 발생합니다.np.concatenate잘못된 방법:

>>> a = np.eye(2)
>>> np.concatenate(a, a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 6, in concatenate
TypeError: only integer scalar arrays can be converted to a scalar index

올바른 방법은 두 배열을 튜플로 입력하는 것입니다.

>>> np.concatenate((a, a))
array([[1., 0.],
       [0., 1.],
       [1., 0.],
       [0., 1.]])

다음 오류 메시지를 생성하는 간단한 경우:

In [8]: [1,2,3,4,5][np.array([1])]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-55def8e1923d> in <module>()
----> 1 [1,2,3,4,5][np.array([1])]

TypeError: only integer scalar arrays can be converted to a scalar index

작동하는 몇 가지 변형:

In [9]: [1,2,3,4,5][np.array(1)]     # this is a 0d array index
Out[9]: 2
In [10]: [1,2,3,4,5][np.array([1]).item()]    
Out[10]: 2
In [11]: np.array([1,2,3,4,5])[np.array([1])]
Out[11]: array([2])

기본 파이썬 목록 인덱싱은 numpy의 인덱싱보다 더 제한적입니다.

In [12]: [1,2,3,4,5][[1]]
....
TypeError: list indices must be integers or slices, not list

편집을

다시 보기

indices = np.random.choice(range(len(X_train)), replace=False, size=50000, p=train_probs)

indices정수의 1d 배열이지만 스칼라는 아닙니다.50000개의 정수 배열입니다.목록에 있는지 배열에 있는지 여부에 관계없이 목록을 한 번에 여러 인덱스로 인덱싱할 수 없습니다.

이 오류를 발생시킬 수 있는 또 다른 경우는 다음과 같습니다.

>>> np.ndindex(np.random.rand(60,60))
TypeError: only integer scalar arrays can be converted to a scalar index

실제 모양을 사용하면 수정할 수 있습니다.

>>> np.ndindex(np.random.rand(60,60).shape)
<numpy.ndindex object at 0x000001B887A98880>

올바른 인수를 전달하고 있는지 확인합니다.사이먼과 비슷하게, 저는 두 개의 배열을 전달하고 있었습니다.np.all하나의 배열만 허용하는 경우, 즉 두 번째 배열이 축으로 해석되었음을 의미합니다.

x_train을 사용해 보십시오.형체를 갖추다[]

언급URL : https://stackoverflow.com/questions/50997928/typeerror-only-integer-scalar-arrays-can-be-converted-to-a-scalar-index-with-1d

반응형