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

邮差的信 提交于 2021-01-20 19:22:40

问题


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:

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?


回答1:


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 x is a function, and functions (in general) don't have min attributes, so you can't call some_function.min(). What is x? In your code, you've only defined it as

x=var

I'm not sure what var is. var isn'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.)

[Update: since you say you're "using PyLab", probably var is numpy.var which has been imported into scope at startup in IPython. I think you really mean "using IPython in --pylab mode.]

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

Now numpy arrays do have a .min() and .max() method, so this:

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

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



回答2:


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?

Assuming you're in ipython called as ipython --pylab or something similar, then you can simply use the paste magic command. Call it as %paste or simply paste if you haven't defined paste as another variable:

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]: 



回答3:


Change that line to:

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

min and max are not attributes of lists, they are their own functions.




回答4:


I encountered a similar error when I called timezone.now instead of timezone.now(). I then tried to format the DateTime value 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'.

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

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




回答5:


I got the similar issue.

My error was I named a variable and function name as same.

I corrected that and error vanished.




回答6:


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



来源:https://stackoverflow.com/questions/15930454/python-attributeerror-function-object-has-no-attribute-min

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!