I need to convolve a (128,128,128) numpy array m with a (35,35,35) kernel k.
I am looking for something like Matlab's convn function
convn(m,k,'same')
The most similar function I found in python is
ndimage.convolve()
but is not the same.
Can anyone suggest a method of doing this as easy as calling that function in Matlab?
Related
I'm trying to convert a radial distribution function for my use, but the code I'm looking at uses a cKDTree. The problem is that I want to use only numpy in my function.
Does anyone know an equivalent function in numpy that can be used or a way to make an equivalent "tree"?
I was just wondering how to go from mvnrnd([4 3], [.4 1.2], 300); in MATLAB code to np.random.multivariate_normal([4,3], [[x_1 x_2],[x_3 x_4]], 300) in Python.
My doubt namely lays on the sigma parameter, since, in MATLAB, a 2D vector is used to specify the covariance; whereas, in Python, a matrix must be used.
What is the theoretical meaning on that and what is the practical approach to go from one to another, for instance, in this case? Also, is there a rapid, mechanical way?
Thanks for reading.
Although python expects a matrix, it is essentially a symmetric covariance matrix. So it has to be a square matrix.
In 2x2 case, a symmetric matrix will have mirrored non diagonal elements.
I believe in python, it should look like [[.4 1.2],[1.2 .4]]
Maybe it's ill advised doing this in the first place, but I'm trying to multiply a (k,k) matrix with (k,1) random vector, and I want to do this M times. I want to do this in one calculation, so having a (k,M) matrix and multiplying each column by my (k,K) matrix. Similar to how you would multiply a scalar with a vector. Is this possible without a loop?
Not in pure Python. The numpy package is universally used for numerical computation in Python. It provides several ways of doing this kind of vectorized matrix multiplication, of which the most common is probably numpy.matmul():
https://docs.scipy.org/doc/numpy/reference/generated/numpy.matmul.html
Say I have predictor array x=numpy.array(n,px) and a predicted array y=numpy.array(n, py)
What would be the best way in python to calculate all regression (linear) from x to each dimension of y (1...py)?
The output of the whole thing would be a matrix (py, px) (for each output, px parameters).
I could of course easily iterate over outputs dimensions (for each computing normal single output multivariate input OLS), however that would be inefficient as I will recalculate the pseudo inverse matrix of x.
Is there any efficient implementation out there?
Could not find any (neither http://wiki.scipy.org/Cookbook/OLS)
I figured scikit-learn would have done this already, so I looked at the source code and discovered that they use scipy.linalg.lstsq (see line 379).
According to the docs, the scipy version of lstsq does indeed accept a matrix as the b parameter. (Actually the numpy version accepts a matrix value as well.)
Maybe these are what you're looking for ?
The fit() method of sklearn.linear_model.LinearRegression accepts multi-target output, so this is now handled natively in sklearn. Just use a 2-dimensional array for the y value of fit(X,y) of shape (n_samples, n_targets).
I'm translating some code from MATLAB to Python and I'm stuck with the corrmtx() MATLAB function. Is there any similar function in Python, or how could I replace it?
The spectrum package has such a function.
How about:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.toeplitz.html
The matlab docs for corrmtx state:
X = corrmtx(x,m) returns an (n+m)-by-(m+1) rectangular Toeplitz matrix
X, such that X'X is a (biased) estimate of the autocorrelation matrix
for the length n data vector x.
The scipy function gives the Toeplitz matrix, although I'm not sure if the implementations are identical.
Here is a list of alternatives that can help you in translating your code, all of which contain that function:
scipy (toeplitz | corrmtx)
spectrum (corrmtx)
The following is a link to another post that tells you how to use numpy for the auto correlation since it seems to be the default funcationality of corrmtx
Additional Information:
Finding the correlation matrix in Python
Unbiased Estimation of Covariance Matrix