# Quick Reference of NumPy and SciPy

## Linear regression

Both NumPy and SciPy have functions for simple linear regression (see post). It is more intuitive to do in SciPy as follows:

The meaning of each output is self-explanatory in the code above.

## Find indices of an array

The “find” function in Matlab is very handy, but it doesn’t have a direct clone in NumPy.

• Most commonly, function numpy.where is the closest one to the “find” in Matlab:

will return a tuple “(array([1, 2]),)” which is the array of matched indices.

• Or, a custom function mimicking the find in Matlab should great.

Related functions on sorting, searching and counting are here.

## Find the size of a matrix

This is equivalent to the “size” function in Matlab:

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:

may just return (5,) instead of (5,1). This is the difference from the “size” function in Matlab.

## NumPy for Matlab Users

http://wiki.scipy.org/NumPy_for_Matlab_Users