Scipy.signal method 'filtfilt()' doesn't recognized correctly - python

It's my first time working with scipy.signal library and I am experimenting an error with the method filtfilt().
This is the code I am trying to execute:
Fs = 1000
# s is an array of numbers
a=signal.firwin(10, cutoff=0.5/(Fs/2))
ss = s - np.mean(s)
se = signal.filtfilt(a, 1, ss, method="gust")
When I execute this code I get the next error:
TypeError: filtfilt() got an unexpected keyword argument 'method'
But in the documentation of the method it is clearly shown that the parameter 'method' exists.
What could be the problem?

I would guess you have different versions of scipy in use. The documentation of filtfilt says the 'gust' method was added in 0.16. I assume the method parameter does not exist in earlier versions.

Related

Can't specify initialization method in Exponential smoothing

When going through the exponential smoothing tutorial provided by statsmodels, I had an issue with specifying an initialization method in anaconda notebook. The error would return:
TypeError: init() got an unexpected keyword argument 'initialization_method'
It would reference the first line in the cell:
fit1 = Holt(air, initialization_method="estimated").fit(smoothing_level=0.8, smoothing_trend=0.2, optimized=False)
When looking for the solution, I downgraded my prompt-toolkit but that didn't do anything. I can't find anyone else having this problem, any ideas?

Reticulate error: AttributeError: 'list' object has no attribute 'shape'

I am trying to run a Python function fitKCA within the R environment through the package reticulate.
The function is sourced correctly and all I do is to call the function:
fitKCA(z = bh$V1, q = 0.1)
and pass the two arguments:
bh$V1, a column (of type dbl) of a tibble
q, a scalar
Nevertheless, I get the following error message:
Error in py_call_impl(callable, dots$args, dots$keywords):AttributeError: 'list' object has no attribute 'shape'
In my understanding, the column of the tibble extracted through the $ sign is of the R type list and this clashes with the Python numpy library and hence has no shape attribute.
So my questions are:
how can I avoid this error?
How do R objects match Python objects (i.e. if I use the $ sign is it compatible with numpy or pandas? Or what if I use instead bh[,2] or bh[,"V1"], is there any difference?)?
Many thanks and forgive my evident lack of computer science background.
I guess you can try this:
fitKCA(z = as.matrix(bh$V1), q = 0.1)

Recurrent problem using scipy.optimize.fmin

I am encountering some problems when translating the following code from MATLAB to Python:
Matlab code snippet:
x=M_test %M_test is a 1x3 array that holds the adjustment points for the function
y=R_test %R_test is also a 1x3 array
>> M_test=[0.513,7.521,13.781]
>> R_test=[2.39,3.77,6.86]
expo3= #(b,x) b(1).*(exp(-(b(2)./x).^b(3)));
NRCF_expo3= #(b) norm(y-expo3(b,x));
B0_expo3=[fcm28;1;1];
B_expo3=fminsearch(NRCF_expo3,B0_expo3);
Data_raw.fcm_expo3=(expo3(B_expo3,Data_raw.M));
The translated (python) code:
expo3=lambda x,M_test: x[0]*(1-exp(-1*(x[1]/M_test)**x[2]))
NRCF_expo3=lambda R_test,x,M_test: np.linalg.norm(R_test-expo3,ax=1)
B_expo3=scipy.optimize.fmin(func=NRCF_expo3,x0=[fcm28,1,1],args=(x,))
For clarity, the object function 'expo3' wants to go through the adjustment points defined by M_test.
'NRCF_expo3' is the function that wants to be minimised, which is basically the error between R_test and the drawn exponential function.
When I run the code, I obtain the following error message:
B_expo3=scipy.optimize.fmin(func=NRCF_expo3,x0=[fcm28,1,1]),args=(x,))
NameError: name 'x' is not defined
There are a lot of similar questions that I have perused.
If I delete the 'args' from the optimization function, as numpy/scipy analog of matlab's fminsearch
seems to indicate it is not necessary, I obtain the error:
line 327, in function_wrapper
return function(*(wrapper_args+args))
TypeError: <lambda>() missing 2 required positional arguments: 'x' and 'M_test'
There are a lot of other modifications that I have tried, following examples like Using scipy to minimize a function that also takes non variational parameters or those found in Open source examples, but nothing works for me.
I expect this is probably quite obvious, but I am very new to Python and I feel like I am looking for a needle in a haystack. What am I not seeing?
Any help would be really appreciated. I can also provide more code, if that is necessary.
I think you shouldn't use lambdas in your code, make instead a single target function with your three parameters (see PEP8). There is a lot of missing information in you post, but for what I can infer, you want something like this:
from scipy.optimize import fmin
# Define parameters
M_TEST = np.array([0.513, 7.521, 13.781])
X_ARR = np.array([2.39,3.77,6.86])
X0 = np.array([10, 1, 1]) # whatever your variable fcm28 is
def nrcf_exp3(r_test, m_test, x):
expo3 = x[0] * (1 - np.exp(-(x[1] / m_test) ** x[2]))
return np.linalg.norm(r_test - expo3)
fmin(nrcf_exp3, X0, args=(M_TEST, X_ARR))

numpy having no "isin" attribute

I tried using the np.isin() function but everytime I do, it returns me the error:
AttributeError: 'module' object has no attribute 'isin'
here is exactly what I do
import numpy as np
a = np.arange(9).reshape((3,3))
test = np.arange(5)
print np.isin(a, test)
I havent found any information about this problem, I use the latest version of numpy and havent had any problem with other numpy module, why does it returns me this error?
The isin function was added in NumPy 1.13:
New np.isin function, improves on in1d.
You're probably using an older version.
Reading through the Notes section of the docs shows
New in version 1.13.0.
I suspect that if you do
print(np.__version__)
you will see something less than 1.13.0.
Following the [source] link in the docs I find that:
def isin(element, test_elements, assume_unique=False, invert=False):
"..."
element = np.asarray(element)
return in1d(element, test_elements, assume_unique=assume_unique,
invert=invert).reshape(element.shape)
It's not doing anything that you can't already do with in1d.
The containing file can be downloaded, and used from your own directory. It has an enhanced unique.

How to use user defined metric in KernelDensity from scikit-learn (python)

I'm using scikit-learn (0.14) and trying to implement a user defined metric for my KernelDensity estimation.
Following code is an example how my code is structured:
def myDistance(x,y):
return np.sqrt(sum((x - y)**2))
dt=DistanceMetric.get_metric("pyfunc",func=myDistance)
kernelModel=KernelDensity(algorithm='ball_tree',metric='pyfunc')
kernelModel.fit(X)
According to the documentation, the BallTree algorithm should accept user defined metrics.
If I run this code the way given here, I get following error:
TypeError: __init__() takes exactly 1 positional argument (0 given)
The error seems to come from:
sklearn.neighbors.dist_metrics.PyFuncDistance.__init__
I don't understand this. If i check what 'dt' in the code above gives me, I get what I expect. dt.pairwise(X) returns the correct values.
What am I doing wrong?
Thanks in advance.
Solution is
kernelModel=KernelDensity(...,metric='pyfunc',metric_params={"func":myDistance})
The call to Distancemetric.get_metric is not necessary.
M

Categories

Resources