{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Opérations NumPy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Arithmétique\n", "\n", "Vous pouvez facilement effectuer un tableau avec l'arithmétique tableau, ou scalaire avec l'arithmétique tableau. Voyons quelques exemples :" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "arr = np.arange(0,10)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr + arr" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr * arr" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr - arr" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/marci/anaconda/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: invalid value encountered in true_divide\n", " if __name__ == '__main__':\n" ] }, { "data": { "text/plain": [ "array([ nan, 1., 1., 1., 1., 1., 1., 1., 1., 1.])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Avertissement en cas de division par zéro, mais pas une erreur !\n", "# Seulement remplacé par nan\n", "arr/arr" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/marci/anaconda/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: divide by zero encountered in true_divide\n", " if __name__ == '__main__':\n" ] }, { "data": { "text/plain": [ "array([ inf, 1. , 0.5 , 0.33333333, 0.25 ,\n", " 0.2 , 0.16666667, 0.14285714, 0.125 , 0.11111111])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Avertissement aussi, mais pas une erreur, remplacé par l'infini\n", "1/arr" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr**3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fonctions universelles de tableau\n", "\n", "Numpy vient avec beaucoup de [fonctions universelles de tableau](http://docs.scipy.org/doc/numpy/reference/ufuncs.html), qui sont essentiellement juste des opérations mathématiques que vous pouvez utiliser pour effectuer l'opération à travers le tableau. En voici quelques unes :" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 1. , 1.41421356, 1.73205081, 2. ,\n", " 2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Prendre la racine carrée\n", "np.sqrt(arr)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1.00000000e+00, 2.71828183e+00, 7.38905610e+00,\n", " 2.00855369e+01, 5.45981500e+01, 1.48413159e+02,\n", " 4.03428793e+02, 1.09663316e+03, 2.98095799e+03,\n", " 8.10308393e+03])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Calcul exponentiel (e^)\n", "np.exp(arr)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.max(arr) # comme arr.max()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.84147098, 0.90929743, 0.14112001, -0.7568025 ,\n", " -0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.41211849])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sin(arr)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/marci/anaconda/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: divide by zero encountered in log\n", " if __name__ == '__main__':\n" ] }, { "data": { "text/plain": [ "array([ -inf, 0. , 0.69314718, 1.09861229, 1.38629436,\n", " 1.60943791, 1.79175947, 1.94591015, 2.07944154, 2.19722458])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.log(arr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Bon travail!\n", "\n", "C'est tout ce que nous avons besoin de savoir pour l'instant !" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.5" } }, "nbformat": 4, "nbformat_minor": 1 }