15

首先,为这两个问题看起来如此明显而道歉;我对此非常陌生,不知道我在做什么。

我正在尝试编写一些东西来将用于样条插值的 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 中,它只会输入我的代码的第一行,所以我必须一次将整个内容粘贴到一行中。我该如何解决这个问题?

4

6 回答 6

10

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
于 2013-04-10T15:55:04.103 回答
3

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

假设你被ipython称为或类似ipython --pylab东西,那么你可以简单地使用魔法命令。如果您没有定义为另一个变量,则将其称为或简单地调用它:paste%pastepastepaste

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]: 
于 2013-04-10T17:30:22.383 回答
2

将该行更改为:

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

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

于 2013-04-10T15:43:56.560 回答
1

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

解决方法是在 . 之后简单地添加括号now。这调用了now函数并返回了它的结果,而不是返回now function对象本身。

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

于 2019-02-15T22:39:16.530 回答
0

我遇到了类似的问题。

我的错误是我将变量和函数名称命名为相同。

我纠正了这一点,错误消失了。

于 2021-01-16T16:04:16.710 回答
-1

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

于 2013-04-10T15:46:06.250 回答