Last modified: Jun, 2014
Both NumPy and SciPy have functions for simple linear regression (see post). It is more intuitive to do in SciPy as follows:
from scipy import stats
x = [1, 2, 3, 4]
y = [2, 4, 7, 9]
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
The meaning of each output is self-explanatory in the code above.
The “find” function in Matlab is very handy, but it doesn’t have a direct clone in NumPy.
import numpy as np
a = np.array([6,7,8,9])
print np.where((a > 6) & (a <9))
will return a tuple “(array([1, 2]),)” which is the array of matched indices.
Related functions on sorting, searching and counting are here.
This is equivalent to the “size” function in Matlab:
(m, n) = mtx.shape
where “mtx” is a matrix.
However, there is a caveat: if the matrix is an 1D array, then the returned tuple may miss the corresponding “1”. For example:
import numpy as np
a = np.linspace(1,10, 10)
print a.reshape(5,2)[:,0].shape
may just return (5,) instead of (5,1). This is the difference from the “size” function in Matlab.
http://wiki.scipy.org/NumPy_for_Matlab_Users
http://penandpants.com/2012/03/09/reading-text-tables-with-python/