类型错误:在尝试以指数方式拟合数据时,只能将长度为 1 的数组转换为 Python 标量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21687581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:23:33  来源:igfitidea点击:

TypeError: only length-1 arrays can be converted to Python scalars while trying to exponentially fit data

pythonarraysloggingscalar

提问by user3291404

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

回答by Tom Pohl

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

非 numpy 函数喜欢math.abs()math.log10()不适合 numpy 数组。只需将引发错误的行替换为:

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

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

回答by Eric Leschinski

Here is another way to reproduce this error in Python2.7 with numpy:

这是在 Python2.7 中使用 numpy 重现此错误的另一种方法:

import numpy as np
a = np.array([1,2,3])
b = np.array([4,5,6])
c = np.concatenate(a,b)   #note the lack of tuple format for a and b
print(c) 

The np.concatenatemethod produces an error:

np.concatenate方法产生错误:

TypeError: only length-1 arrays can be converted to Python scalars

If you read the documentation around numpy.concatenate, then you see it expects a tuple of numpy array objects. So surrounding the variables with parens fixed it:

如果您阅读有关numpy.concatenate的文档,那么您会看到它需要一个 numpy 数组对象的元组。所以用括号围绕变量修复它:

import numpy as np
a = np.array([1,2,3])
b = np.array([4,5,6])
c = np.concatenate((a,b))  #surround a and b with parens, packaging them as a tuple
print(c) 

Then it prints:

然后它打印:

[1 2 3 4 5 6]

What's going on here?

这里发生了什么?

That error is a case of bubble-up implementation - it is caused by duck-typing philosophy of python. This is a cryptic low-level error python guts puke up when it receives some unexpected variable types, tries to run off and do something, gets part way through, the pukes, attempts remedial action, fails, then tells you that "you can't reformulate the subspace responders when the wind blows from the east on Tuesday".

该错误是冒泡实现的一个案例 - 它是由 python 的鸭子类型哲学引起的。这是一个神秘的低级错误 python 当它收到一些意想不到的变量类型时会呕吐,试图逃跑并做某事,中途通过,呕吐,尝试补救措施,失败,然后告诉你“你可以”当周二风从东方吹来时,重新制定子空间响应器”。

In more sensible languages like C++ or Java, it would have told you: "you can't use a TypeA where TypeB was expected". But Python does it's best to soldier on, does something undefined, fails, and then hands you back an unhelpful error. The fact we have to be discussing this is one of the reasons I don't like Python, or its duck-typing philosophy.

在像 C++ 或 Java 这样更明智的语言中,它会告诉你:“你不能在需要 TypeB 的地方使用 TypeA”。但是 Python 最好坚持下去,做了一些未定义的事情,失败了,然后把一个无用的错误交还给你。我们必须讨论这个事实是我不喜欢 Python 或其鸭子类型哲学的原因之一。