T-test in Pandas

it depends what sort of t-test you want to do (one sided or two sided dependent or independent) but it should be as simple as: from scipy.stats import ttest_ind cat1 = my_data[my_data[‘Category’]==’cat1′] cat2 = my_data[my_data[‘Category’]==’cat2′] ttest_ind(cat1[‘values’], cat2[‘values’]) >>> (1.4927289925706944, 0.16970867501294376) it returns a tuple with the t-statistic & the p-value see here for other t-tests … Read more

How to display progress of scipy.optimize function?

As mg007 suggested, some of the scipy.optimize routines allow for a callback function (unfortunately leastsq does not permit this at the moment). Below is an example using the “fmin_bfgs” routine where I use a callback function to display the current value of the arguments and the value of the objective function at each iteration. import … Read more

Principal component analysis in Python

Months later, here’s a small class PCA, and a picture: #!/usr/bin/env python “”” a small class for Principal Component Analysis Usage: p = PCA( A, fraction=0.90 ) In: A: an array of e.g. 1000 observations x 20 variables, 1000 rows x 20 columns fraction: use principal components that account for e.g. 90 % of the … Read more

Directly use Intel mkl library on Scipy sparse matrix to calculate A dot A.T with less memory

Look at the Python code for the scipy sparse product. Notice that it calls the compiled code in 2 passes. It looks like the mkl code does the same thing https://software.intel.com/en-us/node/468640 If request=1, the routine computes only values of the array ic of length m + 1, the memory for this array must be allocated … Read more

How to multiply two 2D RFFT arrays (FFTPACK) to be compatible with NumPy’s FFT?

Correct functions: import numpy as np from scipy import fftpack as scipy_fftpack from scipy import fft as scipy # FFTPACK RFFT 2D def fftpack_rfft2d(matrix): fftRows = scipy_fftpack.fft(matrix, axis=1) fftCols = scipy_fftpack.fft(fftRows, axis=0) return fftCols # FFTPACK IRFFT 2D def fftpack_irfft2d(matrix): ifftRows = scipy_fftpack.ifft(matrix, axis=1) ifftCols = scipy_fftpack.ifft(ifftRows, axis=0) return ifftCols.real You calculated the 2D FFT … Read more

Fit plane to a set of points in 3D: scipy.optimize.minimize vs scipy.linalg.lstsq

Least squares (scipy.linalg.lstsq) is guaranteed to converge. In fact, there is a closed form analytical solution (given by (A^T A)^-1 A^Tb (where ^T is matrix transpose and ^-1 is matrix inversion) The standard optimization problem, however, is not generally solvable – we are not guaranteed to find a minimizing value. However, for the given equation, … Read more

tech