Python'AttributeError:'function'对象没有属性'min'' [英] Python 'AttributeError: 'function' object has no attribute 'min''
问题描述
首先,对于这两个问题的显而易见性表示歉意;我对此非常陌生,不知道自己在做什么.
我正在尝试编写一些东西来将 Scipy 函数用于样条插值到一组值.我的代码目前看起来像这样:
将 numpy 导入为 np将 scipy 导入为 sp从 scipy.interpolate 导入 interp1dx=varx1 = ([0.1,0.3,0.4])y1 = [0.2,0.5,0.6]新长度 = 25new_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' 对象没有属性 'min'
到目前为止,谷歌搜索等没有发现我理解的任何内容.这是什么意思,我该如何解决?
第二个问题:如何一次输入多行代码?目前,如果我尝试复制整个内容然后将其粘贴到 PyLab 中,它只会输入我代码的第一行,因此我必须一次将整个内容粘贴到一行中.我该如何解决这个问题?
如果这一行
new_x = np.linspace(x.min(), x.max(), new_length)
正在生成错误消息
AttributeError: 'function' 对象没有属性 'min'
then x
是一个函数,函数(一般情况下)没有 min
属性,所以不能调用 some_function.min()代码>.
x
是什么?在您的代码中,您仅将其定义为
x=var
我不确定 var
是什么.var
不是 Python 中的默认内置函数,但如果它是一个函数,那么您要么出于某种原因自己定义它,要么从某个地方获取它(假设您正在使用 Sage,或者你做了一个像 from sympy import *
之类的星型导入.)
[更新:既然你说你正在使用 PyLab",那么 var
可能是 numpy.var
,它已经在 IPython 启动时导入到作用域中.我认为你的意思是在 --pylab
模式下使用 IPython.]
你也定义了x1
和y1
,但是你后面的代码引用了x
和y
,所以感觉这段代码介于两个功能状态之间.
现在 numpy
数组 do 有一个 .min()
和 .max()
方法,所以这个:
会起作用.你的测试数据不会,因为插值至少需要 4 个点,你会得到
ValueError: x 和 y 数组必须至少有 4 个条目
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?
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
这篇关于Python'AttributeError:'function'对象没有属性'min''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!