Jappie Jappie - 1 year ago 927
Python Question

numpy.float64 is not iterable

I'm trying to print a function which uses several parameters from numpy array's and lists, but I keep getting the error "numpy.float 64 object is not iterable". I've looked at several questions on the forum about this topic and tried different answers but none seem to work (or I might be doing something wrong I'm still a beginner at python) but it all comes down to the same thing, I'm stuck and I hope you guys can help. I'm using python 2.7, this is the code:

EDIT: Included the error message and changed the print to "print(T, (obj(T),))"

from __future__ import division
import numpy as np
import random

K = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1,])
x = len(K)
#Production rates and demand rates of products setup costs and holding costs (p, d, c, h)
p = np.array([193, 247, 231, 189, 159])
d = np.array([16, 16, 21, 19, 23])
#c1 = np.array([random.random() for _ in range(x)]) use these values as test values for c
c = [0.752, 0.768, 0.263, 0.152, 0.994, 0.449, 0.431, 0.154, 0.772]
h = [0.10*c[i]/240 for i in range(x)]
n = len(p)
t = [10.76, 74.61, 47.54, 29.40, 45.00, 90.48, 17.09, 85.19, 35.33]

def obj(T):
for i in range(n):
for q in range(x):
for k in range(x):
return ((1. / T) * c[q] + sum((.5*h[k]*(p[i]-d[i])* (p[i]/d[i])*(t[k])**2)))

for T in range(200, 900):
print(T, (obj(T),))

runfile('C:/Users/Jasper/Anaconda2/Shirodkar.py', wdir='C:/Users/Jasper/Anaconda2')
Traceback (most recent call last):

File "<ipython-input-1-0cfdc6b9fe69>", line 1, in <module>
runfile('C:/Users/Jasper/Anaconda2/Shirodkar.py', wdir='C:/Users/Jasper/Anaconda2')

File "C:\Users\Jasper\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
execfile(filename, namespace)

File "C:\Users\Jasper\Anaconda2\lib\site- packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)

File "C:/Users/Jasper/Anaconda2/Shirodkar.py", line 24, in <module>
print(T, (obj(T),))

File "C:/Users/Jasper/Anaconda2/Shirodkar.py", line 21, in obj
return ((1. / T) * c[q] + sum((.5*h[k]*(p[i]-d[i])*(p[i]/d[i])*(t[k])**2)))

TypeError: 'numpy.float64' object is not iterable

Answer Source

I suspect the problem is here:

sum((.5*h[k]*(p[i]-d[i])* (p[i]/d[i])*(t[k])**2))

The end result of that expression is a float, isn't it? What is the sum() for?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download