Typeerror: Only size-1 arrays can be converted to Python scalars – Solution

In this short tutorial, we look at the “Only size-1 arrays can be converted to Python scalars” typeerror in python. This typeerror is quite common however there are only a handful of tutorials explaining it. In case you are looking for an in-depth expl…


This content originally appeared on DEV Community and was authored by hrishikesh1990

In this short tutorial, we look at the "Only size-1 arrays can be converted to Python scalars" typeerror in python. This typeerror is quite common however there are only a handful of tutorials explaining it. In case you are looking for an in-depth explanation of the error, I would recommend you follow step by step, if not feel free to check out the solutions.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.

Table of Content

What does "Only size-1 arrays can be converted to Python scalars" signify?

You are likely to face the above typeerror while working with NumPy and matplotlib.pyplot. This error arises when you pass an array into a function that is expecting a single value (i.e, scalar value). Python generally works with only a handful of scalar values (Int, float, bool, etc) however while dealing with NumPy we have another 24 new fundamental Python types to describe different types of scalars. You can read more about them here.

Hence you ought to be extra cautious while using Numpy. A common error I came across was this:

import numpy as ny
import matplotlib.pyplot as matplot

def ycord(xcord):
    return ny.int(xcord)

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

This is the output we receive:

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

This because the .int function only accepts single values and in our case, we are trying to pass x which is an array.

Solution 1: Code and Explanation

In the first method, we use the .vectorize function. The .vectorize function is essentially a for loop that takes in an array and returns a single numpy array. Using this method we iterate over the array x and return a single value. Thus we do not encounter the "only size-1 arrays can be converted to Python scalars" typeerror.

import numpy as ny
import matplotlib.pyplot as matplot
xcord = ny.arange(1, 5, 2.5)
ycord = ny.vectorize(ny.int)
matplot.plot(xcord, ycord(xcord))
matplot.show()

As you can see we no longer need to define a function as we have used the .int function inside the .vectorize function, which loops through the array and returns a single array that is accepted by the .int function.

Solution 2: Code and Explanation

Another method I came across utilizes the .astype methods, this method casts the array into a specified type in our case int. Although this method works, I personally recommend the first solution as this method as it casts a string into an int which is not a desirable method.

import numpy as ny
import matplotlib.pyplot as matplot

def ycord(xcord):
    return xcord.astype(int)

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

These are two solutions that can be used when faced with the Typeerror: Only size-1 arrays can be converted to Python scalars error.

Do let me know your thoughts/ queries in the comment sections below. :)


This content originally appeared on DEV Community and was authored by hrishikesh1990


Print Share Comment Cite Upload Translate Updates
APA

hrishikesh1990 | Sciencx (2021-05-11T11:09:59+00:00) Typeerror: Only size-1 arrays can be converted to Python scalars – Solution. Retrieved from https://www.scien.cx/2021/05/11/typeerror-only-size-1-arrays-can-be-converted-to-python-scalars-solution/

MLA
" » Typeerror: Only size-1 arrays can be converted to Python scalars – Solution." hrishikesh1990 | Sciencx - Tuesday May 11, 2021, https://www.scien.cx/2021/05/11/typeerror-only-size-1-arrays-can-be-converted-to-python-scalars-solution/
HARVARD
hrishikesh1990 | Sciencx Tuesday May 11, 2021 » Typeerror: Only size-1 arrays can be converted to Python scalars – Solution., viewed ,<https://www.scien.cx/2021/05/11/typeerror-only-size-1-arrays-can-be-converted-to-python-scalars-solution/>
VANCOUVER
hrishikesh1990 | Sciencx - » Typeerror: Only size-1 arrays can be converted to Python scalars – Solution. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/11/typeerror-only-size-1-arrays-can-be-converted-to-python-scalars-solution/
CHICAGO
" » Typeerror: Only size-1 arrays can be converted to Python scalars – Solution." hrishikesh1990 | Sciencx - Accessed . https://www.scien.cx/2021/05/11/typeerror-only-size-1-arrays-can-be-converted-to-python-scalars-solution/
IEEE
" » Typeerror: Only size-1 arrays can be converted to Python scalars – Solution." hrishikesh1990 | Sciencx [Online]. Available: https://www.scien.cx/2021/05/11/typeerror-only-size-1-arrays-can-be-converted-to-python-scalars-solution/. [Accessed: ]
rf:citation
» Typeerror: Only size-1 arrays can be converted to Python scalars – Solution | hrishikesh1990 | Sciencx | https://www.scien.cx/2021/05/11/typeerror-only-size-1-arrays-can-be-converted-to-python-scalars-solution/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.