Typeerror: Only size-1 arrays can be converted to Python scalars - Solution | DevsDay.ru

IT-блоги Typeerror: Only size-1 arrays can be converted to Python scalars - Solution

dev.to 11 мая 2021 г.


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. :)

Источник: dev.to

Наш сайт является информационным посредником. Сообщить о нарушении авторских прав.

python programming tutorial