converting none (size-1 arrays) to scalars

Viewed 25

I am working on functions and i practice it by some codes (there is a bias because i am working on neural networks) and when i was doing square root function and i got an error:

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

Here is the code which caused it.

import matplotlib.pyplot as plt
import numpy as np
import math


def somefunc(m,bias):
    y = round(math.sqrt(m))+ bias
    return y


y = np.linspace(1, 100, 100)
z = somefunc(y,0)
plt.plot(y, z)
plt.xlabel("y")
plt.ylabel("func(y)")
plt.show()
1 Answers

You have to use numpy methods to deal with multi-arrays :

np.sqrt instead of math.sqrt

np.round instead of round

import matplotlib.pyplot as plt
import numpy as np
import math


def somefunc(m,bias):
    y = np.round(np.sqrt(m))+ bias
    return y


y = np.linspace(1, 100, 100)
z = somefunc(y,0)
plt.plot(y, z)
plt.xlabel("y")
plt.ylabel("func(y)")
plt.show()
Related