Home > Enterprise >  Why getting Cross index error using np.ix_
Why getting Cross index error using np.ix_

Time:11-29

Why does this error occur while working with np.ix_?

import numpy as np

a = np.arange(1,3).reshape(1,2)
print(a)

k_r = [[1,1],[1,-1]]

r = np.zeros((10,10),int)
r[np.ix_([a],[a])]= k_r

print(r)

Error:

ValueError: Cross index must be 1 dimensional

CodePudding user response:

Think I understand the problem. First you should remove the "[]" around your a's like this

r[np.ix_(a,a)]= k_r

It seems like np.ix_(a, a) wants a on the format: a = [x, x] where x is. Your current a is on the format: [[1 2]] An alternative way of creating your a can be:

a = [i for i in range(1,3)]

If this does not solve your problem you can check out this page: https://numpy.org/doc/stable/reference/generated/numpy.ix_.html

CodePudding user response:

The error tells you that the cross index must be one-dimensional, but a = np.arange(1,3).reshape(1,2) creates a 2D array. Furthermore, you're adding yet another dimension (sort of, but not really) by wrapping the arrays in lists, r[np.ix_([a],[a])].

Fix these things and you get:

import numpy as np

a = np.arange(1,3)
print(a)

k_r = [[1,1],[1,-1]]

r = np.zeros((10,10),int)
r[np.ix_(a,a)]= k_r
print(r)
  • Related