python-pour-finance/02-NumPy/1-tableaux-NumPy.ipynb

962 lines
20 KiB
Plaintext
Raw Permalink Normal View History

2023-08-21 15:12:19 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# NumPy \n",
"\n",
"NumPy (ou Numpy) est une bibliothèque d'Algèbre Linéaire pour Python, la raison de son importance pour la Finance avec Python, c'est que preque toutes les bibliothèques de l'écosystème Data Science Python sont construites sur un bloc NumPy. De plus nous l'utiliserons pour générer des données pour nos exemples d'analyse plus tard!\n",
"\n",
"Numpy est aussi extrêmement rapide, de part ses liens avec le langage C. Pour pour d'informations sur l'utilisation des tableaux plutôt que des listes, regardez ce post [StackOverflow](http://stackoverflow.com/questions/993984/why-numpy-instead-of-python-lists).\n",
"\n",
"Nous n'apprendrons que les bases de NumPy, pour commencer nous avons besoin de l'installer!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instructions d'installation\n",
"\n",
"### NumPy est déjà inclus dans votre environnement! Si vous utilisez l'environnement pyfinance, c'est bon pour vous!\n",
"\n",
"_____\n",
"##### Pour ceux n'utilisant pas l'environnement fourni:\n",
"\n",
"**Il est fortement recommandé d'installer Python à l'aide de la distribution Anaconda pour vous assurer que toutes les dépendances sous-jacentes (comme les bibliothèques Linear Algebra) sont synchronisées avec l'utilisation d'une installation conda. Si vous avez Anaconda, installez NumPy en vous rendant à votre terminal ou à l'invite de commande et en tapant:**\n",
" \n",
" conda install numpy\n",
" \n",
"**Si vous n'avez pas Anaconda et ne pouvez pas l'installer, veuillez vous référer à [Documentation officielle de Numpy sur les différentes instructions d'installation.](http://docs.scipy.org/doc/numpy-1.10.1/user/install.html)**\n",
"\n",
"_____"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Utiliser NumPy\n",
"\n",
"Une fois que vous avez installé NumPy, vous pouvez l'importer sous forme de bibliothèque :"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Numpy a de nombreuses fonctions et capacités intégrées. Nous ne les couvrirons pas toutes, mais nous nous concentrerons plutôt sur certains des aspects les plus importants de Numpy: vecteurs, tableaux, matrices et génération de nombres. Commençons par discuter des tableaux.\n",
"\n",
"# Tableaux Numpy\n",
"\n",
"Les tableaux NumPy sont la principale façon dont nous utiliserons Numpy tout au long du cours. Les tableaux Numpy sont essentiellement de deux types: les vecteurs et les matrices. Les vecteurs sont des tableaux 1-d et les matrices sont 2-d (mais vous devez noter qu'une matrice peut toujours avoir seulement une ligne ou une colonne).\n",
"\n",
"Commençons notre introduction en explorant comment créer des tableaux NumPy.\n",
"\n",
"## Création de tableaux NumPy\n",
"\n",
"### Depuis une liste Python\n",
"\n",
"Nous pouvons créer un tableau en convertissant directement une liste ou une liste de listes:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_list = [1,2,3]\n",
"my_list"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.array(my_list)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 2, 3], [4, 5, 6], [7, 8, 9]]"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_matrix = [[1,2,3],[4,5,6],[7,8,9]]\n",
"my_matrix"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [4, 5, 6],\n",
" [7, 8, 9]])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.array(my_matrix)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Méthodes intégrées\n",
"\n",
"Il existe de nombreuses méthodes intégrées pour générer des tableaux"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### arange\n",
"\n",
"Retourne des valeurs uniformément espacées dans un intervalle donné."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.arange(0,10)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 2, 4, 6, 8, 10])"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.arange(0,11,2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### zeros et ones\n",
"\n",
"Génère des tableaux de zéros ou de uns"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0., 0., 0.])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.zeros(3)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0.]])"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.zeros((5,5))"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., 1., 1.])"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.ones(3)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1., 1., 1.],\n",
" [ 1., 1., 1.],\n",
" [ 1., 1., 1.]])"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.ones((3,3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### linspace\n",
"Retourne les nombres uniformément espacés sur un intervalle spécifié."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0., 5., 10.])"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.linspace(0,10,3)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0. , 0.20408163, 0.40816327, 0.6122449 ,\n",
" 0.81632653, 1.02040816, 1.2244898 , 1.42857143,\n",
" 1.63265306, 1.83673469, 2.04081633, 2.24489796,\n",
" 2.44897959, 2.65306122, 2.85714286, 3.06122449,\n",
" 3.26530612, 3.46938776, 3.67346939, 3.87755102,\n",
" 4.08163265, 4.28571429, 4.48979592, 4.69387755,\n",
" 4.89795918, 5.10204082, 5.30612245, 5.51020408,\n",
" 5.71428571, 5.91836735, 6.12244898, 6.32653061,\n",
" 6.53061224, 6.73469388, 6.93877551, 7.14285714,\n",
" 7.34693878, 7.55102041, 7.75510204, 7.95918367,\n",
" 8.16326531, 8.36734694, 8.57142857, 8.7755102 ,\n",
" 8.97959184, 9.18367347, 9.3877551 , 9.59183673,\n",
" 9.79591837, 10. ])"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.linspace(0,10,50)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## eye\n",
"\n",
"Crée une matrice d'identité"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1., 0., 0., 0.],\n",
" [ 0., 1., 0., 0.],\n",
" [ 0., 0., 1., 0.],\n",
" [ 0., 0., 0., 1.]])"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.eye(4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Random \n",
"\n",
"Numpy a aussi beaucoup de façons de créer des tableaux de nombres aléatoires:\n",
"\n",
"### rand\n",
"Créez un tableau de la forme donnée et remplissez-le avec\n",
"des échantillons aléatoires provenant d'une distribution uniforme\n",
"sur ``[0, 1)``."
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.11570539, 0.35279769])"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.rand(2)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.66660768, 0.87589888, 0.12421056, 0.65074126, 0.60260888],\n",
" [ 0.70027668, 0.85572434, 0.8464595 , 0.2735416 , 0.10955384],\n",
" [ 0.0670566 , 0.83267738, 0.9082729 , 0.58249129, 0.12305748],\n",
" [ 0.27948423, 0.66422017, 0.95639833, 0.34238788, 0.9578872 ],\n",
" [ 0.72155386, 0.3035422 , 0.85249683, 0.30414307, 0.79718816]])"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.rand(5,5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### randn\n",
"\n",
"Retourne un échantillon (ou des échantillons) de la distribution \"normale standard\". Contrairement à rand qui est uniforme:"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-0.27954018, 0.90078368])"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.randn(2)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.70154515, 0.22441999, 1.33563186, 0.82872577, -0.28247509],\n",
" [ 0.64489788, 0.61815094, -0.81693168, -0.30102424, -0.29030574],\n",
" [ 0.8695976 , 0.413755 , 2.20047208, 0.17955692, -0.82159344],\n",
" [ 0.59264235, 1.29869894, -1.18870241, 0.11590888, -0.09181687],\n",
" [-0.96924265, -1.62888685, -2.05787102, -0.29705576, 0.68915542]])"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.randn(5,5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### randint\n",
"Retourne les nombres entiers aléatoires de `low` (inclus) à `high` (exclusif)."
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"44"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.randint(1,100)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([13, 64, 27, 63, 46, 68, 92, 10, 58, 24])"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.randint(1,100,10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Attributs et méthodes des tableaux\n",
"\n",
"Discutons de quelques attributs et méthodes utiles pour un tableau :"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"arr = np.arange(25)\n",
"ranarr = np.random.randint(0,50,10)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n",
" 17, 18, 19, 20, 21, 22, 23, 24])"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([10, 12, 41, 17, 49, 2, 46, 3, 19, 39])"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ranarr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reshape\n",
"Retourne un tableau contenant les mêmes données avec une nouvelle forme."
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3, 4],\n",
" [ 5, 6, 7, 8, 9],\n",
" [10, 11, 12, 13, 14],\n",
" [15, 16, 17, 18, 19],\n",
" [20, 21, 22, 23, 24]])"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr.reshape(5,5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### max, min, argmax, argmin\n",
"\n",
"Ce sont des méthodes utiles pour trouver des valeurs maximales ou minimales. Ou pour trouver l'emplacement de leur index à l'aide des méthodes argmin ou argmax"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([10, 12, 41, 17, 49, 2, 46, 3, 19, 39])"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ranarr"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"49"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ranarr.max()"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ranarr.argmax()"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ranarr.min()"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ranarr.argmin()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Shape\n",
"\n",
"Shape est un attribut des tableaux (pas une méthode):"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(25,)"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Vecteur\n",
"arr.shape"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n",
" 17, 18, 19, 20, 21, 22, 23, 24]])"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Remarquez les doubles crochets\n",
"arr.reshape(1,25)"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 25)"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr.reshape(1,25).shape"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0],\n",
" [ 1],\n",
" [ 2],\n",
" [ 3],\n",
" [ 4],\n",
" [ 5],\n",
" [ 6],\n",
" [ 7],\n",
" [ 8],\n",
" [ 9],\n",
" [10],\n",
" [11],\n",
" [12],\n",
" [13],\n",
" [14],\n",
" [15],\n",
" [16],\n",
" [17],\n",
" [18],\n",
" [19],\n",
" [20],\n",
" [21],\n",
" [22],\n",
" [23],\n",
" [24]])"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr.reshape(25,1)"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(25, 1)"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr.reshape(25,1).shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### dtype\n",
"\n",
"Vous pouvez également saisir le type de données de l'objet dans le tableau:"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('int64')"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr.dtype"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Bon travail!"
]
}
],
"metadata": {
"anaconda-cloud": {},
"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
}