"For more info, see the related FAQ entry: https://www.scipy.org/scipylib/faq.html#why-both-numpy-linalg-and-scipy-linalg-what-s-the-difference."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Broadcasting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Broadcasting is the name given to the method that NumPy uses to allow array arithmetic between arrays with a different shape or size.\n",
"\n",
"Although the technique was developed for NumPy, it has also been adopted more broadly in other numerical computational libraries, such as Theano, TensorFlow, and Octave.\n",
"\n",
"Broadcasting solves the problem of arithmetic between arrays of differing shapes by in effect replicating the smaller array along the last mismatched dimension.\n",
"\n",
" The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes.\n",
"\n",
"— Broadcasting, SciPy.org\n",
"\n",
"See : https://docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3]\n",
"2\n",
"[3 4 5]\n"
]
}
],
"source": [
"# scalar and one-dimensional\n",
"a = np.array([1, 2, 3])\n",
"print(a)\n",
"b = 2\n",
"print(b)\n",
"c = a + b\n",
"print(c)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [1 2 3]]\n",
"2\n",
"[[3 4 5]\n",
" [3 4 5]]\n"
]
}
],
"source": [
"# scalar and two-dimensional\n",
"A = np.array([[1, 2, 3], [1, 2, 3]])\n",
"print(A)\n",
"b = 2\n",
"print(b)\n",
"C = A + b\n",
"print(C)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [1 2 3]]\n",
"[1 2 3]\n",
"[[2 4 6]\n",
" [2 4 6]]\n"
]
}
],
"source": [
"# one-dimensional and two-dimensional\n",
"A = np.array([[1, 2, 3], [1, 2, 3]])\n",
"print(A)\n",
"b = np.array([1, 2, 3])\n",
"print(b)\n",
"C = A + b\n",
"print(C)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Limitations of Broadcasting\n",
"\n",
"Broadcasting is a handy shortcut that proves very useful in practice when working with NumPy arrays.\n",
"\n",
"That being said, it does not work for all cases, and in fact imposes a strict rule that must be satisfied for broadcasting to be performed.\n",
"\n",
"Arithmetic, including broadcasting, can only be performed when the shape of each dimension in the arrays are equal or one has the dimension size of 1. The dimensions are considered in reverse order, starting with the trailing dimension; for example, looking at columns before rows in a two-dimensional case."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Vectorization\n",
"\n",
"- The vectorize function : https://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html\n",
With `IPython` and `Spyder`, Python plus these fundamental scientific packages constitutes a very good alternative to Matlab, that is technically very similar (using the libraries Blas and Lapack). Matlab has a JustInTime (JIT) compiler so that Matlab code is generally faster than Python. However, we will see that Numpy is already quite efficient for standard operations and other Python tools (for example `pypy`, `cython`, `numba`, `pythran`, `theano`...) can be used to optimize the code to reach the performance of optimized Matlab code.
The advantage of Python over Matlab is its high polyvalency (and nicer syntax) and there are notably several other scientific Python packages (see our notebook `pres13_doc_applications.ipynb`):
%% Cell type:markdown id: tags:
-[sympy](http://www.sympy.org) for symbolic computing,
-[pandas](http://pandas.pydata.org/), [statsmodels](http://www.statsmodels.org), [seaborn](http://seaborn.pydata.org/) for statistics,
-[h5py](http://www.h5py.org/), [h5netcdf](https://pypi.python.org/pypi/h5netcdf) for hdf5 and netcdf files,
-[mpi4py](https://pypi.python.org/pypi/mpi4py) for MPI communications,
-[opencv](https://pypi.python.org/pypi/opencv-python), [scikit-image](http://scikit-image.org/) for image processing,
-[pyopencl](https://pypi.python.org/pypi/pyopencl), [pycuda](https://mathema.tician.de/software/pycuda/), [theano](http://deeplearning.net/software/theano/), [tensorflow](https://www.tensorflow.org/) for speed and GPU computing,
-[scikit-learn](http://scikit-learn.org), [keras](https://keras.io/), [mxnet](http://mxnet.io/) for machine learning,
-[bokeh](http://bokeh.pydata.org) for display data efficiently,
-[mayavi](http://docs.enthought.com/mayavi/mayavi/) for 3D visualization,
-[qtpy](https://pypi.python.org/pypi/QtPy), [kivy](https://kivy.org) for GUI frameworks
- ...
%% Cell type:markdown id: tags:
## A short introduction on NumPy
Code using `numpy` usually starts with the import statement
%% Cell type:code id: tags:
``` python
importnumpyasnp
```
%% Cell type:markdown id: tags:
NumPy provides the type `np.ndarray`. Such array are multidimensionnal sequences of homogeneous elements. They can be created for example with the commands:
16.6 ms +- 220 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
246 us +- 16.7 us per loop (mean +- std. dev. of 7 runs, 1000 loops each)
Additions: ratio times (Python / NumPy): 67.47967479674797
%% Cell type:markdown id: tags:
This shows that when you need to perform mathematical operations on a lot of homogeneous numbers, it is more efficient to use `numpy` arrays.
%% Cell type:markdown id: tags:
# Manipulating NumPy arrays
%% Cell type:markdown id: tags:
## Access elements
Elements in a `numpy` array can be accessed using indexing and slicing in any dimension. It also offers the same functionalities available in Fortan or Matlab.
### Indexes and slices
For example, we can create an array `A` and perform any kind of selection operations on it.
The mask is in fact a particular case of the advanced indexing capabilities provided by NumPy. For example, it is even possible to use lists for indexing:
In the previous example, we manipulated a one dimensional array containing quadruplets of data. This functionality can be used to load images into arrays and save arrays to images.
It can also be used when loading data of different types from a file with `np.genfromtxt`.
%% Cell type:markdown id: tags:
#### NumPy and SciPy sub-packages:
We already saw `numpy.random` to generate `numpy` arrays filled with random values. This submodule also provides functions related to distributions (Poisson, gaussian, etc.) and permutations.
%% Cell type:markdown id: tags:
To perform linear algebra with dense matrices, we can use the submodule `numpy.linalg`. For instance, in order to compute the determinant of a random matrix, we use the method `det`
If the data are sparse matrices, instead of using `numpy`, it is recommended to use `scipy`.
%% Cell type:code id: tags:
``` python
fromscipy.sparseimportcsr_matrix
print(csr_matrix([[1,2,0],[0,0,3],[4,0,5]]))
```
%%%% Output: stream
(0, 0) 1
(0, 1) 2
(1, 2) 3
(2, 0) 4
(2, 2) 5
%% Cell type:markdown id: tags:
#### SciPy or NumPy ?
`scipy` also provides a submodule for linear algebra `scipy.linalg`. It provides an extension of `numpy.linalg`.
For more info, see the related FAQ entry: https://www.scipy.org/scipylib/faq.html#why-both-numpy-linalg-and-scipy-linalg-what-s-the-difference.
%% Cell type:markdown id: tags:
### Broadcasting
%% Cell type:markdown id: tags:
Broadcasting is the name given to the method that NumPy uses to allow array arithmetic between arrays with a different shape or size.
Although the technique was developed for NumPy, it has also been adopted more broadly in other numerical computational libraries, such as Theano, TensorFlow, and Octave.
Broadcasting solves the problem of arithmetic between arrays of differing shapes by in effect replicating the smaller array along the last mismatched dimension.
The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes.
— Broadcasting, SciPy.org
See : https://docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html
%% Cell type:code id: tags:
``` python
# scalar and one-dimensional
a=np.array([1,2,3])
print(a)
b=2
print(b)
c=a+b
print(c)
```
%%%% Output: stream
[1 2 3]
2
[3 4 5]
%% Cell type:code id: tags:
``` python
# scalar and two-dimensional
A=np.array([[1,2,3],[1,2,3]])
print(A)
b=2
print(b)
C=A+b
print(C)
```
%%%% Output: stream
[[1 2 3]
[1 2 3]]
2
[[3 4 5]
[3 4 5]]
%% Cell type:code id: tags:
``` python
# one-dimensional and two-dimensional
A=np.array([[1,2,3],[1,2,3]])
print(A)
b=np.array([1,2,3])
print(b)
C=A+b
print(C)
```
%%%% Output: stream
[[1 2 3]
[1 2 3]]
[1 2 3]
[[2 4 6]
[2 4 6]]
%% Cell type:markdown id: tags:
#### Limitations of Broadcasting
Broadcasting is a handy shortcut that proves very useful in practice when working with NumPy arrays.
That being said, it does not work for all cases, and in fact imposes a strict rule that must be satisfied for broadcasting to be performed.
Arithmetic, including broadcasting, can only be performed when the shape of each dimension in the arrays are equal or one has the dimension size of 1. The dimensions are considered in reverse order, starting with the trailing dimension; for example, looking at columns before rows in a two-dimensional case.
%% Cell type:markdown id: tags:
### Vectorization
- The vectorize function : https://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html