Python 'AttributeError: object' function 'does not have attribute' min ''

First, an apology for how obvious these two issues are; I am very new to this and do not know what I am doing.

I am trying to write something to apply a Scipy function to interpolate a spline to an array of values. Currently my code is as follows:

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 hits the string

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

I get the following error:

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

and still googling etc. understood nothing. What does this mean and how to fix it?

Second question: how can I enter several lines of code at once? At the moment, if I try to copy it all and then paste it into PyLab, it only enters the top line of my code, so I need to paste it all in one line at a time. How do I get around this?

+7
source share
4 answers

If this line

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

generates an error message

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

then x is a function, and functions (generally speaking) do not have min attributes, so you cannot call some_function.min() . What is x ? In your code, you only defined it as

 x=var 

I'm not sure what var . var not built-in by default in Python, but if it is a function, you either defined it yourself for some reason, or you selected it from somewhere (say you use Sage, or you did, for example, from sympy import * or something like that.)

[Update: since you say you are using "PyLab", maybe var is numpy.var , which was imported into the 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 looks like this code is halfway between two functional states.

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

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

will work. Your test data will not be related to the fact that at least 4 points are required for interpolation, and you will receive

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

Change this 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.

+2
source

Second question: how can I enter several lines of code at once? At the moment, if I try to copy it all and then paste it into PyLab, it only enters the top line of my code, so I need to paste it all in one line at a time. How do I get around this?

Assuming you are in ipython called ipython --pylab or something similar, you can just use the magic paste command , Call it %paste or just paste if you have not 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]: 
+1
source

Int does not have a min () function, but min () is a built-in function. You will need to use min (x).

-one
source

All Articles