且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

类型错误:只有长度为1的阵列可以尝试拟合指数的数据转换到Python标量

更新时间:2022-10-14 17:56:50

非numpy的功能()

math.log10 ()没有很好地与numpy的阵列玩。只需更换线路引发错误有:

  M = np.log10(np.abs(X))

除此之外,在 np.polyfit(),因为它缺少一个参数(并且没有分配的结果反正进一步使用)的调用将无法工作。

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

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))

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).