TypeError: only size-1 arrays can be converted to python scalars.

In this article, we will discuss: What is TypeError: only size-1 arrays can be converted to python scalars? When does it occur? How can we solve this error? Table of Contents What is TypeError: only …

only size-1 arrays can be converted

In this article, we will discuss:

  • What is TypeError: only size-1 arrays can be converted to python scalars?
  • When does it occur?
  • How can we solve this error?

What is TypeError: only size-1 arrays can be converted to python scalars?

The “TypeError: only size-1 arrays can be converted to python scalars” is a python error.

This error is divided into two parts:

Type error: means the error arises because there is an issue in input type.

Only size-1 arrays can be converted to python scalars: means you can only convert size-1 arrays into scalars.

When does it occur?

“TypeError: only size-1 arrays can be converted to python scalars” occurs when you are working with python modules/libraries like: NumPy and Matplotlib. 

When you have defined the function which is expecting a single valued parameter but it is getting an array rather than a single value.

Let’s take simple programming examples to understand it better.

Example1:

#importing numpy library
#importing matplotlib 

import numpy as ny
import matplotlib.pyplot as matplot

#defining custom functions
def ycord(xcord):
    return ny.int(xcord)

#defining co-ordinates
xcord = ny.arange(1, 5, 3.6)
matplot.plot(xcord, ycord(xcord))
matplot.show()

Output:

TypeError                                 Traceback (most recent call last)
<ipython-input-12-cdbe51d1247a> in <module>
      8 
      9 xcord = ny.arange(1, 5, 3.6)
---> 10 matplot.plot(xcord, ycord(xcord))
     11 matplot.show()

<ipython-input-12-cdbe51d1247a> in ycord(xcord)
      4 
      5 def ycord(xcord):
----> 6     return ny.int(xcord)
      7 
      8 

TypeError: only size-1 arrays can be converted to Python scalars

In the above example, we are getting an “TypeError: only size-1 arrays can be converted to python scalars”, because the int function accepts the single values as a parameter and here, we have passed the “x” which is an array.

How can we solve TypeError: only size-1 arrays can be converted to python scalars?

We can solve “TypeError: only size-1 arrays can be converted to python scalars” by using the following methods:

  1. .vectorize function
  2. .astype method

Let’s solve the above Example1 by using “.vectorize function”

Vectorize function is a function which works as a loop which takes an array and returns a single numpy array.

Solution Example1 by using method1 (.vectorize):

#importing Numpy
#importing matplot library

import numpy as ny
import matplotlib.pyplot as matplot

#defining custom function
xcord = ny.arange(1, 5, 3.5)

#using .vectorize function
ycord = ny.vectorize(ny.int)

matplot.plot(xcord, ycord(xcord))
matplot.show()

Output:

only size-1 arrays can be converted

In above example, we used the int function inside the .vectorize function which will return a single array that is accepted by the .int function.

Let’s solve the above Example1 by using “.astype method”.astype method is used to cast an array into a particular type in our case it is “int”.

Solution Example1 by using method2 (.astype):

#importing numpy library
#importing matplot library

import numpy as ny
import matplotlib.pyplot as matplot

#defining your custom function
def ycord(xcord):
    return xcord.astype(int)


xcord = ny.arange(1, 5, 3.5)
matplot.plot(xcord, ycord(xcord))
matplot.show()

Output:

only size-1 arrays can be converted

In above example, we used the int function inside the .astype method which will return a single array that is accepted by the .int function.

Conclusion:

In this article, we discussed the “TypeError: only size-1array can be converted to python scalars”.

Why it occurs and possible solutions explained with the help of simple examples.

Suggested Read: 500 Internal server error

Leave a Comment