2013-04-10 182 views
7

首先,对这两个问题似乎有多明显是道歉的;我对此非常非常新,并且不知道我在做什么。Python'AttributeError:'function'object has no attribute'min''

我试图写一些东西来将样条插值的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) 

,但是当它到达行

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

我收到以下错误:

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

,到目前为止,谷歌搜索等已经变成了什么我明白。这是什么意思,我该如何解决?

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

+3

请尽量保持问题,只有一个问题,你的第一个(编程)问题是,在这个网站可以接受的,但是,你的第二个(非编程)问题与StackOverflow无关,应该以超级用户的身份询问。 – 2013-04-10 15:46:24

+0

如果你从'scipy.interpolate import interp1d'执行'''',你可以简单地调用'interp1d(...)'。如果你只有'scipy as sp',你必须把它叫做'sp.interpolate.interp1d(...)'你不需要这样做,除非你想单独调用'interp1d(...)'以及'scipy'像'sp.interp()'中的另一个函数,而不显式地从scipy import interp'导入和'from scipy.interpolate import interp1d' – askewchan 2013-04-10 17:23:55

回答

2

更改该行:

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

minmax不是列表的属性,他们是自己的功能。

+0

谢谢 - 修正了这个问题。我现在得到'TypeError:'函数'对象不可兼容'。 – 2013-04-10 15:53:26

+0

我想你想在'x1'上运行这个,因为我不确定'x = var'是什么意思。但'x1'是一个列表。 – 2013-04-10 15:56:22

-1

Int's没有min()函数,但min()是内建函数。你需要使用min(x)。

8

如果该行

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

生成错误消息

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

然后x是一个函数,和功能(一般)不具有min属性,所以可以不致电some_function.min()。什么是x?在你的代码,你只把它定义为

x=var 

我不知道什么是varvar不是Python中的默认内建函数,但如果它是一个函数,那么无论您是由于某种原因自己定义它,还是从某处(例如使用Sage,或者您做过明星导入如from sympy import *或其他东西)。

[更新:既然你说你是“使用PyLab”,可能varnumpy.var已在IPython启动时导入到范围内。我觉得你真的是在--pylab模式“下使用IPython的。]

也可以定义x1y1,但随后你以后的代码是指xy,所以它有点感觉这段代码在两个中途之间的功能状态。

现在numpy阵列.min().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) 

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

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

奇妙 - 这一切都很好,没有给我任何错误信息。用x = var我试图把x变成一个变量,但是对于代码的其余部分来说实际上并没有任何意义,所以我不确定为什么我这么做了!非常感谢,非常感谢。 – 2013-04-10 16:04:21

1

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?

假设你在ipython称为ipython --pylab或类似的东西的时候,那么你可以简单地使用the paste magic command。称其为%paste或者干脆paste如果您还没有定义paste另一个变量:

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