Typeerror: Only size-1 arrays can be converted to Python scalars - Solution
Tags: python, programming, tutorial
Published: 2021-05-11 11:09:59

In this short tutorial, we look at the typeerror "Only size-1 arrays can be converted to Python scalars" 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 directly.

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

## Table of Contents - Typeerror: only size-1 arrays can be converted to Python scalars

- [What does "Typeerror: Only size-1 arrays can be converted to Python scalars" signify?](#typeerror)
- [Solutions 1: Code & Explanation](#solution1)
- [Solution 2: Code & Explanation](#solution2)

### What does "Only size-1 arrays can be converted to Python scalars" signify? <a name="typeerror"></a>

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](https://numpy.org/doc/stable/reference/arrays.scalars.html).

Due to the above errors, you ought to be extra cautious while using NumPy. A common error I came across was this:

```python
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()
```

For the above input, this is the output we receive:

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

This is 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<a name="solution1"></a>

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.

```python
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<a name="solution2"></a>

Another method I came across utilizes the .astype method, 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 casts a string into an int which is not a desirable method.

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