numpy.float64不可迭代 [英] numpy.float64 is not iterable
问题描述
我试图打印一个函数,它使用numpy数组和列表中的几个参数,但是我一直收到错误numpy.float 64 object is not iterable。我已经在论坛上看到了关于这个主题的几个问题,并尝试了不同的答案,但似乎没有任何工作(或者我可能做错了什么,我仍然是一个Python初学者),但一切都归结于同一件事情,我卡住了,我希望你们能帮上忙。编辑:包含错误消息并将打印改为print(T,(obj(T),));我使用的是Python 2.7,这是代码: )
from __future__ import division
import numpy as np
import random
K = np.array([1,1,1,1,1,1,1,1,1])$ b $ bx = len(K)
#产品的生产率和需求率(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)])使用这些值作为c
c = [0.752,0.768, 0.263,0.152,0.994,0.444,0.431,0.1544,0.772]
h = [0.10 * c [i] / 240对于范围(x)中的i]
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):$ i
在范围(n)中:$在范围(x)中为b $ b:q在范围(x)中为
:
return((1. / (t [k] *(p [i] -d [i])*(p [i] / d [i])* * 2)))
for T in range(200,900):
print(T,(obj(T),))
runfile(' C:/Users/Jasper/Anaconda2/Shirodkar.py',wdir ='C:/ Users / Jasper / Anaconda2')
Traceback(最近的最后一次调用):
文件< ; ipython-input-1-0cfdc6b9fe69>,第1行,在< module>
runfile('C:/Users/Jasper/Anaconda2/Shirodkar.py',wdir ='C:/ Users / Jasper / Anaconda2')
文件C:\Users\\ \\ Jasper \Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py,第714行,在runfile中
execfile(filename,namespace)
文件C:\ Users \Jasper\Anaconda2\lib\site- packages\spyderlib\widgets\externalshell\sitecustomize.py,第74行,在execfile中
exec(编译(脚本文本,文件名,'exec'),glob,loc)
在< module>文件中的第24行C:/Users/Jasper/Anaconda2/Shirodkar.py
print(T,(obj(T),))
文件C:/Users/Jasper/Anconda2/Shirodkar.py,第21行,在obj
返回((1. / T)* c [q] + sum((.5 * h [k] *(p [i] -d [i])*(p [i] / d [i] [k])** 2)))
TypeError:'numpy.float64'对象不可迭代
我怀疑问题出在这里:
sum ((.5 * h [k] *(p [i] -d [i])*(p [i] / d [i])*(t [k])** 2))
该表达式的最终结果是一个浮点数,不是吗?总和()是多少?
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
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?
这篇关于numpy.float64不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!