Python'AttributeError:'function'对象没有属性'min''

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15930454/
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 21:24:16  来源:igfitidea点击:

Python 'AttributeError: 'function' object has no attribute 'min''

pythonnumpyattributesattributeerror

提问by Tom Robinson

Firstly, apologies for how obvious these two questions seem to be; I'm very very new to this and don't have a clue what I'm doing.

首先,对这两个问题的明显程度表示歉意;我对此非常陌生,不知道我在做什么。

I'm trying to write something to apply the Scipy function for spline interpolation to an array of values. My code currently looks like this:

我正在尝试编写一些东西来将 Scipy 函数用于样条插值到值数组。我的代码目前看起来像这样:

import numpy as np
import scipy as sp
from scipy.interpolate import interp1d

x=var
x1 = ([0.1,0.3,0.4])
y1 = [0.2,0.5,0.6]

new_length = 25
new_x = np.linspace(x.min(), x.max(), new_length)
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)

but when it gets to the line

但是当它到达线路时

new_x = np.linspace(x.min(), x.max(), new_length)

I get the following error:

我收到以下错误:

AttributeError: 'function' object has no attribute 'min'

and so far googling etc has turned up nothing that I understand. What does this mean and how do I fix it?

到目前为止,谷歌搜索等没有发现我理解的任何内容。这是什么意思,我该如何解决?

Second question: how do I input more than one line of code at once? At the moment, if I try to copy the whole thing and then paste it into PyLab, it only inputs the top line of my code, so I have to paste the whole thing in one line at a time. How do I get round this?

第二个问题:如何一次输入多行代码?目前,如果我尝试复制整个内容然后将其粘贴到 PyLab 中,它只会输入我代码的第一行,因此我必须一次将整个内容粘贴到一行中。我该如何解决这个问题?

回答by Inbar Rose

Change that line to:

将该行更改为:

new_x = np.linspace(min(x), max(x), new_length)

minand maxare not attributes of lists, they are their own functions.

min并且max不是列表的属性,它们是它们自己的函数。

回答by Harlin

Int's don't have a min() function but min() is a builtin function. You'll need to use min(x).

Int 没有 min() 函数,但 min() 是一个内置函数。您需要使用 min(x)。

回答by DSM

If this line

如果这条线

new_x = np.linspace(x.min(), x.max(), new_length)

is generating the error message

正在生成错误消息

AttributeError: 'function' object has no attribute 'min'

then xis a function, and functions (in general) don't have minattributes, so you can't call some_function.min(). What is x? In your code, you've only defined it as

thenx是一个函数,而函数(通常)没有min属性,所以你不能调用some_function.min(). 什么是x?在您的代码中,您仅将其定义为

x=var

I'm not sure what varis. varisn't a default builtin in Python, but if it's a function, then either you've defined it yourself for some reason or you've picked it up from somewhere (say you're using Sage, or you did a star import like from sympy import *or something.)

我不确定是什么varvar不是 Python 中的默认内置函数,但如果它是一个函数,那么要么您出于某种原因自己定义它,要么从某个地方获取它(假设您使用的是 Sage,或者您做了一个星形导入,例如from sympy import *或者其他的东西。)

[Update: since you say you're "using PyLab", probably varis numpy.varwhich has been imported into scope at startup in IPython. I think you really mean "using IPython in --pylabmode.]

[更新:既然你说你在“使用 PyLab”,可能varnumpy.var在 IPython 启动时被导入到作用域中。我认为你的意思是“在--pylab模式下使用 IPython 。]

You also define x1and y1, but then your later code refers to xand y, so it sort of feels like this code is halfway between two functional states.

您还定义了x1and y1,但是后来的代码引用了xand y,所以感觉这段代码介于两个功能状态之间。

Now numpyarrays dohave a .min()and .max()method, so this:

现在numpy数组确实有一个.min()and.max()方法,所以这个:

>>> x = np.array([0.1, 0.3, 0.4, 0.7])
>>> y = np.array([0.2, 0.5, 0.6, 0.9])
>>> new_length = 25
>>> new_x = np.linspace(x.min(), x.max(), new_length)
>>> new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)

would work. Your test data won't because the interpolation needs at least 4 points, and you'd get

会工作。你的测试数据不会,因为插值需要至少 4 个点,你会得到

ValueError: x and y arrays must have at least 4 entries

回答by askewchan

Second question: how do I input more than one line of code at once? At the moment, if I try to copy the whole thing and then paste it into PyLab, it only inputs the top line of my code, so I have to paste the whole thing in one line at a time. How do I get round this?

第二个问题:如何一次输入多行代码?目前,如果我尝试复制整个内容然后将其粘贴到 PyLab 中,它只会输入我代码的第一行,因此我必须一次将整个内容粘贴到一行中。我该如何解决这个问题?

Assuming you're in ipythoncalled as ipython --pylabor something similar, then you can simply use the pastemagic command. Call it as %pasteor simply pasteif you haven't defined pasteas another variable:

假设你在ipython被称为ipython --pylab或类似的东西,那么你可以简单地使用paste神奇的命令。如果您尚未将其定义为另一个变量,则将其称为%paste或简称为:pastepaste

In [8]: paste
import numpy as np
import scipy as sp
from scipy.interpolate import interp1d

x=var
x1 = ([0.1,0.3,0.4])
y1 = [0.2,0.5,0.6]

new_length = 25
new_x = np.linspace(x.min(), x.max(), new_length)
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)

## -- End pasted text --
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-b4e41f59d719> in <module>()
      3 from scipy.interpolate import interp1d
      4 
----> 5 x=var
      6 x1 = ([0.1,0.3,0.4])
      7 y1 = [0.2,0.5,0.6]

NameError: name 'var' is not defined

In [9]: 

回答by Stephen G Tuggy

I encountered a similar error when I called timezone.nowinstead of timezone.now(). I then tried to format the DateTimevalue that I was expecting. But it wasn't a DateTime; it was a function. This resulted in an error message about 'Month' not being an attribute of 'function'.

当我调用timezone.now而不是timezone.now(). 然后我尝试格式化DateTime我期望的值。但它不是一个DateTime; 这是一个函数。这导致了关于“Month”不是“function”的属性的错误消息。

The fix was to simply add the parentheses after now. This called the nowfunction and returned its result, instead of returning the now functionobject itself.

解决方法是简单地在now. 这会调用now函数并返回其结果,而不是返回now function对象本身。

Silly mistake, I know. But not easy to troubleshoot.

愚蠢的错误,我知道。但不容易排除故障。