Home > Blockchain >  ValueError: Cross index must be 1 dimensional
ValueError: Cross index must be 1 dimensional

Time:11-27

1.why this error occur while working with np.ix_. what foolish mistake i been doing ?

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
r


print(r)

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).reshape(2)
print(a)

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

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

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

  • Related