Python 中数组的对数
from numpy import *
from pylab import plot,show
q=10
time = range(q)
mlist=empty(q)
nlist=empty(q)
m=.9
n=(m+(1e-6))
b=3
for t in range(q):
mlist[t]=m
nlist[t]=n
m=(b*(1-m)*m)
n=(b*(1-n)*n)
zlist = mlist-nlist
zlist = abs(zlist)
plot(time, log(zlist))
show()
然后我想绘制时间图,log(zlist)
,每次运行该程序时,我都会收到此错误。 “情节(时间,日志(zlist)) TypeError: only length-1 arrays can be conversion to Python scalars"
有什么想法如何使 zlist 不是数组以便我可以获取日志,或者只是我的程序出了什么问题? 其他一切都很好,只有一个问题。 (仅供注意,我导入了 pylab、numpy 和 math)
编辑:q 值相对不重要,但理想情况下最终会在 500-1000 范围内。 m 和 n 必须在 0 和 1 之间,b 必须在 0 和 4 之间。
编辑 X2:它现在似乎正在工作,我不知道为什么,但它可能是 a)从数学导入日志为好吧,或者b,负值问题,但无论如何,它运行良好。感谢所有贡献者!
from numpy import *
from pylab import plot,show
q=10
time = range(q)
mlist=empty(q)
nlist=empty(q)
m=.9
n=(m+(1e-6))
b=3
for t in range(q):
mlist[t]=m
nlist[t]=n
m=(b*(1-m)*m)
n=(b*(1-n)*n)
zlist = mlist-nlist
zlist = abs(zlist)
plot(time, log(zlist))
show()
then I want to plot the graph of time,log(zlist)
and everytime I run the program, i get this error.
"plot(time,log(zlist))
TypeError: only length-1 arrays can be converted to Python scalars"
Any ideas how to either make zlist not an array so I can take the log, or just what is going wrong in my program?
Everything else works well, just that one problem.
(just for note, i have imported pylab, numpy, and math)
EDIT: The q value is relatively unimportant, but ideally will eventually be up in the 500-1000 range. and m and n have to be between 0 and 1, and b has to be between 0 and 4.
EDIT X2: It seems to be working now, i'm not sure why but it could either be a)importing log from math as well, or b, the negative value problem, but regardless, it's working well. Thank you to everyone who contributed!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑您在
from numpy import *
之后执行了from math import *
,这意味着log
实际上是math.log
> 这不适用于数组。您确实不应该在脚本中使用
import *
。相反,你应该做或者更好
I suspect that you did
from math import *
afterfrom numpy import *
which means thatlog
is reallymath.log
which won't work on an array.You should really not use
import *
in scripts. Instead, you should door, better
尝试
将日志函数应用于 zlist 中的每个数字。
Try
That will apply the log function to each number in zlist.