.. _numpytricksrst: ================================== Points d’implémentation avec numpy ================================== .. only:: html **Links:** :download:`notebook `, :downloadlink:`html `, :download:`PDF `, :download:`python `, :downloadlink:`slides `, :githublink:`GitHub|_doc/notebooks/numpy/numpy_tricks.ipynb|*` Quelques écritures efficaces et non efficaces avec `numpy `__. .. code:: ipython3 from jyquickhelper import add_notebook_menu add_notebook_menu() .. contents:: :local: accéder à un élément en particulier ----------------------------------- .. code:: ipython3 import numpy mat = numpy.zeros((5, 5)) for i in range(mat.shape[0]): for j in range(mat.shape[1]): mat[i, j] = i * 10 + j mat .. parsed-literal:: array([[ 0., 1., 2., 3., 4.], [10., 11., 12., 13., 14.], [20., 21., 22., 23., 24.], [30., 31., 32., 33., 34.], [40., 41., 42., 43., 44.]]) .. code:: ipython3 mat[2, 3], mat[2][3] .. parsed-literal:: (23.0, 23.0) .. code:: ipython3 %timeit mat[2, 3] .. parsed-literal:: 116 ns ± 4.49 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each) .. code:: ipython3 %timeit mat[2][3] .. parsed-literal:: 319 ns ± 57 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) Les deux écritures ont l’air identique puisqu’elle retourne le même résultat. Néanmoins, ``mat[2][3]`` crée un tableau temporaire puis extrait un élément. Les éléments ne sont pas recopiés mais un objet intermédiaire est créé. .. code:: ipython3 mat[2] .. parsed-literal:: array([20., 21., 22., 23., 24.])