numpy - TypeError: only length-1 arrays can be converted to Python scalars issue -
i have following python script:
import math import numpy np scores = [3.0, 1.0, 0.2] y = [0,0,0] = 0 j = 0 k = 0 sum = 0 def myfunc(x): global sum global global j if not y: s in scores: y[i] = 0.5 * scores[i] + 0.2 = i+1 if sum == 0: s2 in scores: sum = sum + math.exp(y[j]) j = j+1 print sum return math.exp(x)/sum s3 in scores: print(myfunc(scores[k])) k = k+1 i have 2 issues here:
- i'm not getting expected output
sum, makes total result wrong when run script, following output:
traceback (most recent call last): file "myfunc.py", line 40, in <module> plt.plot(x, myfunc(scores).t, linewidth=2) file "myfunc.py", line 30, in myfunc return math.exp(x)/sum typeerror: length-1 arrays can converted python scalars
omitting number values @ beginning, why getting error? how can solve it?
thanks.
looks want this:
import numpy np def func(scores): y = 0.5 * scores + 0.2 return scores / np.sum(np.exp(y)) scores = np.array([3.0, 1.0, 0.2]) res in func(scores): print(res) output:
0.339460254992 0.113153418331 0.0226306836661 now can plot it:
from matplotlib import pyplot plt plt.plot(func(scores).t, linewidth=2) 
Comments
Post a Comment