Solve TypeError: only integer scalar arrays can be converted to a scalar index Gorakh Gupta The Crazy Programmer
In this article we will go through the Type Error: only integer scalar arrays can be converted to a scalar index and what causes this error and how can we overcome this error and get some changes in the code so that we can get the proper solution to resolve it. This normally tells about that when we are trying to convert a simple array into a scalar index. The other reason why this error comes is that when we are trying to concatenate and that time we are not passing tuple or list for concatenation and this becomes a major problem.
In the examples we are taking we are trying to concatenate two arrays by function then the function will always concatenate two or more arrays of the same type as always. Concatenation is always done row-wise and column-wise and by default it always takes row-wise.
Case 1:
Suppose we are taking two arrays which are having the fruits name and then we are trying to concatenate the two arrays and for the concatenation function we are assigning other variables.
import numpy array1 = numpy.array ( [ 'Apple' , 'Orange' , 'Grapes', 'Chiku' ] ) array2 = numpy.array ( [ 'Papaya' , 'Banana' ] ) array3 = numpy.concatenate ( array1 , array2 ) print(array3)
When we are trying to concatenate, we are getting an error as we need to convert array1 and array2 into tuple or list.
Output:
PS C : \ Users \ ASUS \ Desktop \ TheCrazyProgrammer Work > python .\ test.py Traceback (most recent call last) : File " .\test.py ", line 4, in <module> array3 = numpy.concatenate ( array1 , array2 ) File "<__array_function__ internals>", line 5, in concatenate TypeError: only integer scalar arrays can be converted to a scalar index
Solution:
To solve this error we will convert both the arrya1 and array2 into Tuple for concatenation so that the Tuple function will run the code without giving any error. Here the output is the list and we got the solution to remove the error and the revised code.
import numpy array1 = numpy.array(['Apple', 'Orange', 'Grapes', 'Chiku']) array2 = numpy.array(['Papaya', 'Banana']) array3 = numpy.concatenate((array1, array2)) print(array3)
Now we have assigned tuple for concatenation then we are getting the output.
Output:
PS C : \ Users \ ASUS \ Desktop \ TheCrazyProgrammer Work > python .\ test.py ['Apple' 'Orange' 'Grapes' 'Chiku' 'Papaya' 'Banana']
Case 2
Suppose we are taking a particular list in which we are assigning some range and then we are doing some indexing operation so that we can get the random choice of the spices.
import numpy as np par_list = list(range(500)) spices = np.random.choice(range(len(par_list)), replace=False, size=200) print(par_list[spices.astype(int)])
But when we are running this code then we are getting the error.
Output:
PS C : \ Users \ ASUS \ Desktop \ TheCrazyProgrammer Work > python .\ test.py Traceback (most recent call last): File ".\test.py", line 4, in <module> print(par_list[spices.astype(int)]) TypeError: only integer scalar arrays can be converted to a scalar index
Solution:
To solve this error we are trying to convert the simple array into numpy array. Here the output is the list and we got the solution to remove the error and the revised code.
import numpy as np par_list = list(range(500)) spices = np.random.choice(range(len(par_list)), replace=False, size=100) print(np.array(par_list)[spices.astype(int)])
Output:
PS C : \ Users \ ASUS \ Desktop \ TheCrazyProgrammer Work > python .\ test.py [396 80 279 404 411 52 321 95 430 196 462 39 43 200 178 275 307 387 89 454 59 175 23 360 458 198 492 453 186 35 137 432 306 173 415 248 56 284 85 327 73 197 277 324 358 421 334 191 374 144 308 208 268 372 210 19 294 274 67 250 70 185 354 305 150 273 316 129 69 391 11 32 496 136 470 436 126 383 361 389 45 145 450 386 28 25 259 328 364 1 36 452 446 116 152 207 146 141 9 177]
Conclusion
As we have seen two cases above in which first we are given two arrays and when we are assigning the tuple then we are getting the output for the error only integer scalar arrays can be converted into scalar index. We will have to see whether we have assigned the tuple or list for the arrays for the concatenation as the list catenation can be done for the arrays.
For the next case, we have to convert the simple array to numpy array for the running of code error free. This way we are solving the error and the error most occurs for the row-wise arrays concatenation done normally in the python codes. And lastly, about the error, we have to see that by a single numpy.concatenate() method we get the error removed and for converting the arrays we use np.array () by which error gets removed.
The post Solve TypeError: only integer scalar arrays can be converted to a scalar index appeared first on The Crazy Programmer.
from The Crazy Programmer https://ift.tt/GetJknI
Comments
Post a Comment