类型错误:在尝试以指数方式拟合数据时,只能将长度为 1 的数组转换为 Python 标量 [英] TypeError: only length-1 arrays can be converted to Python scalars while trying to exponentially fit data

查看:71
本文介绍了类型错误:在尝试以指数方式拟合数据时,只能将长度为 1 的数组转换为 Python 标量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

f=np.loadtxt('Single Small Angle 1.txt',unpack=True,skiprows=2)
g=np.loadtxt('Single Small Angle 5.txt',unpack=True,skiprows=2)

x = f-g[:,:11944]
t=range(len(x))
m=math.log10(abs(x))

np.polyfit(t,m)

plt.plot(t,abs(x))
plt.show()

我只是不确定如何解决我的问题.它一直在说:

I'm just not sure on how to fix my issue. It keeps saying:

m=math.log10(abs(x))
TypeError: only length-1 arrays can be converted to Python scalars

推荐答案

非 numpy 函数,如 math.abs()math.log10() 不与 numpy 数组一起玩得很好.只需将引发错误的行替换为:

Non-numpy functions like math.abs() or math.log10() don't play nicely with numpy arrays. Just replace the line raising an error with:

m = np.log10(np.abs(x))

除此之外,np.polyfit() 调用将不起作用,因为它缺少一个参数(并且您没有分配结果以供进一步使用).

Apart from that the np.polyfit() call will not work because it is missing a parameter (and you are not assigning the result for further use anyway).

这篇关于类型错误:在尝试以指数方式拟合数据时,只能将长度为 1 的数组转换为 Python 标量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆