Home > Software design >  TypeError: only integer scalar arrays can be converted to a scalar - error in 2 basic arrays
TypeError: only integer scalar arrays can be converted to a scalar - error in 2 basic arrays

Time:07-06

I'm a new learner, and I am learning based on some videos. In the video the code faced no problem. However, I faced with a Traceback. I can't figure out what the problem is. Here is my code:

import numpy as np
arr_bis = np.array ([[1,2,3],[7,1,6],[9,6,3]])
arr1 = arr_bis[:,1]
arr2 = arr_bis[:,2]
print (f"ARR1{arr1}")
print (f"ARR2{arr2}")
print (np.concatenate(arr1, arr2), axis = 0)

I want to concatenate two arrays. This is the error: Traceback (most recent call last): File "", line 7, in File "<array_function internals>", line 180, in concatenate TypeError: only integer scalar arrays can be converted to a scalar index

Can somebody tell me what is the problem please?

CodePudding user response:

You are using numpy.concatenate incorrectly.

The first parameter should be an iterable of the arrays to concatenate

numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")

np.concatenate((arr1, arr2), axis=0)

output: array([2, 1, 6, 3, 6, 3])

In your case, not only you passed arr2 as the second parameter, but you also passed the axis=0 parameter to the wrong function (here to print):

print(                             # print function
      np.concatenate(arr1, arr2),  # first parameter of print
                                   # with incorrect calling
      axis = 0                     # second (invalid) parameter of print
)

CodePudding user response:

You missed a parenthesis in your print.

print (np.concatenate(arr1, arr2), axis = 0)

should be

print (np.concatenate((arr1, arr2), axis = 0))
  • Related