I am trying to look for a Python function that is the same as MATLAB's 'fit' function.
Specifically I am looking for something similar to the following MATLAB code:
fi = fit(bins, z, cubicinterp)
where z and bins are lists.
Try SciPy's curve_fit() for general fitting of a function.
For specific interpolations, have a look at SciPy's interploation functions.
Honestly, I think there are hundreds of Python functions to do exactly what you want (and much more).
Here is one example using NumPy, but seriously, just google it and you will be amazed:
numpy.polyfit
Related
The task is to get datasets as well as ideal functions through csv and choose ideal functions on the basis of how they minimize the sum of all y-deviations squared (Least-Square).
I am basically confused about this task so can someone explain this to me as i m trying to learn python.
No, there is not a built-in function to do that. There is an implementation here that uses numpy, pandas and matplotlib.
is there a filter function of kalman in Python that works in the same way as the Kalman function of matlab?
[kest] = kalman(sys,Qn,Rn)
The idea is that the function receives as parameters a space of states and the respective weight matrices (it is to implement an LQR controller)
You can use the pyKalman library. See the sin example followed by the filter example.
It is not exactly like Matlab but it is easy enough to use.
I finally found the ovtave source code for the kalman filter and implemented it in python. Anyway, thank you very much
Are there functions in python that will fill out missing values in a matrix for you, by using collaborative filtering (ex. alternating minimization algorithm, etc). Or does one need to implement such functions from scratch?
[EDIT]: Although this isn't a matrix-completion example, but just to illustrate a similar situation, I know there is an svd() function in Matlab that takes a matrix as input and automatically outputs the singular value decomposition (svd) of it. I'm looking for something like that in Python, hopefully a built-in function, but even a good library out there would be great.
Check out numpy's linalg library to find a python SVD implementation
There is a library fancyimpute. Also, sklearn NMF
I am porting some matlab code to python using scipy and got stuck with the following line:
Matlab/Octave code
[Pxx, f] = periodogram(x, [], 512, 5)
Python code
f, Pxx = signal.periodogram(x, 5, nfft=512)
The problem is that I get different output on the same data. More specifically, Pxx vectors are different. I tried different windows for signal.periodogram, yet no luck (and it seems that default scypy's boxcar window is the same as default matlab's rectangular window) Another strange behavior is that in python, first element of Pxx is always 0, no matter what data input is.
Am i missing something? Any advice would be greatly appreciated!
Simple Matlab/Octave code with actual data: http://pastebin.com/czNeyUjs
Simple Python+scipy code with actual data: http://pastebin.com/zPLGBTpn
After researching octave's and scipy's periodogram source code I found that they use different algorithm to calculate power spectral density estimate. Octave (and MATLAB) use FFT, whereas scipy's periodogram use the Welch method.
As #georgesl has mentioned, the output looks quite alike, but still, it differs. And for porting reason it was critical. In the end, I simply wrote a small function to calculate PSD estimate using FFT, and now output is the same. According to timeit testing, it works ~50% faster (1.9006s vs 2.9176s on a loop with 10.000 iterations). I think it's due to the FFT being faster than Welch in scipy's implementation, of just being faster.
Thanks to everyone who showed interest.
I faced the same problem but then I came across the documentation of scipy's periodogram
As you would see there that detrend='constant' is the default argument. This means that python automatically subtracts the mean of the input data from each point. (Read here). While Matlab/Octave do no such thing. I believe that is the reason why the outputs are different. Try specifying detrend=False, while calling scipy's periodogram you should get the same output as Matlab.
After reading the Matlab and Scipy documentation, another contribution to the different values could be that they use different default window function. Matlab uses a Hamming window, and Scipy uses a Hanning. The two window functions and similar but not identical.
Did you look at the results ?
The slight differences between the two results may comes from optimizations/default windows/implementations/whatever etc.
I am currently trying to move from Matlab to Python and succeeded in several aspects. However, one function in Matlab's Signal Processing Toolbox that I use quite regularly is the impinvar function to calculate a digital filter from its analogue version.
In Scipy.signal I only found the bilinear function to do something similar. But, in contrast to the Matlab bilinear function, it does not take an optional argument to do some pre-warping of the frequencies. I did not find any impinvar (impulse invariance) function in Scipy.
Before I now start to code it myself I'd like to ask whether there is something that I simply overlooked? Thanks.
There is a Python translation of the Octave impinvar function in the PyDynamic package which should be equivalent to the Matlab version.