Skip to content

Instantly share code, notes, and snippets.

@mkcor
Created June 9, 2015 23:03
Show Gist options
  • Save mkcor/c87d0dc7617b4f94fab7 to your computer and use it in GitHub Desktop.
Save mkcor/c87d0dc7617b4f94fab7 to your computer and use it in GitHub Desktop.
Pamphlet contre les boucles `for`...
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Parfois, je chiale contre les boucles `for` avec mes amis. Voici en quelques lignes ce que j'entends par l'approche *vectorielle* et par l'approche *fonctionnelle*. Vivent les langages de haut niveau!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Approche vectorielle"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Mettons que vos données se trouvent dans un `numpy.array`."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"values = np.random.rand(10, 4) * 100"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 2.77847808, 72.85771536, 56.29995592, 14.90389083],\n",
" [ 97.12365121, 93.88874185, 93.68289159, 17.01559433],\n",
" [ 12.10831173, 14.18559097, 68.77712231, 48.21193988],\n",
" [ 34.17506404, 70.05497028, 53.72394929, 80.99896088],\n",
" [ 20.74168948, 31.17384832, 16.33856878, 25.8684615 ],\n",
" [ 38.70880503, 53.14364983, 12.98471181, 90.38483846],\n",
" [ 99.80423837, 61.0587404 , 59.21589663, 10.58816294],\n",
" [ 88.59167853, 84.12369445, 38.28889267, 81.06017106],\n",
" [ 99.70838527, 43.40254982, 77.40168185, 49.24173179],\n",
" [ 10.32727555, 11.57119777, 2.56799993, 37.6388576 ]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"values"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Et qu'elles doivent subir un certain traitement. C'est direct:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.35518744, -0.56553708, -0.24615564, 0.72018743],\n",
" [ 0.26260508, -0.35137352, -0.53532123, -0.96557141],\n",
" [-0.44220793, 0.99882779, -0.33152174, -0.88572342],\n",
" [ 0.37319936, 0.80751793, -0.31159788, -0.63069472],\n",
" [ 0.94881392, -0.23972076, -0.58963392, 0.6711213 ],\n",
" [ 0.84666862, 0.26038919, 0.40624526, 0.66039788],\n",
" [-0.66442677, -0.97960376, 0.45677258, -0.91814961],\n",
" [ 0.58678623, 0.64368764, 0.55617889, -0.58204224],\n",
" [-0.73290328, -0.54781257, 0.90785289, -0.854065 ],\n",
" [-0.78487699, -0.83885307, 0.54265328, -0.06021779]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.sin(values)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Approche fonctionnelle"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Les `pandas.DataFrame`, c'est ma structure de données préférée! Ils peuvent tout manger."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"df = pd.DataFrame(values, columns=['exp1', 'exp2', 'exp3', 'exp4'])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>exp1</th>\n",
" <th>exp2</th>\n",
" <th>exp3</th>\n",
" <th>exp4</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2.778478</td>\n",
" <td>72.857715</td>\n",
" <td>56.299956</td>\n",
" <td>14.903891</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>97.123651</td>\n",
" <td>93.888742</td>\n",
" <td>93.682892</td>\n",
" <td>17.015594</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>12.108312</td>\n",
" <td>14.185591</td>\n",
" <td>68.777122</td>\n",
" <td>48.211940</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>34.175064</td>\n",
" <td>70.054970</td>\n",
" <td>53.723949</td>\n",
" <td>80.998961</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>20.741689</td>\n",
" <td>31.173848</td>\n",
" <td>16.338569</td>\n",
" <td>25.868462</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>38.708805</td>\n",
" <td>53.143650</td>\n",
" <td>12.984712</td>\n",
" <td>90.384838</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>99.804238</td>\n",
" <td>61.058740</td>\n",
" <td>59.215897</td>\n",
" <td>10.588163</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>88.591679</td>\n",
" <td>84.123694</td>\n",
" <td>38.288893</td>\n",
" <td>81.060171</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>99.708385</td>\n",
" <td>43.402550</td>\n",
" <td>77.401682</td>\n",
" <td>49.241732</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>10.327276</td>\n",
" <td>11.571198</td>\n",
" <td>2.568000</td>\n",
" <td>37.638858</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" exp1 exp2 exp3 exp4\n",
"0 2.778478 72.857715 56.299956 14.903891\n",
"1 97.123651 93.888742 93.682892 17.015594\n",
"2 12.108312 14.185591 68.777122 48.211940\n",
"3 34.175064 70.054970 53.723949 80.998961\n",
"4 20.741689 31.173848 16.338569 25.868462\n",
"5 38.708805 53.143650 12.984712 90.384838\n",
"6 99.804238 61.058740 59.215897 10.588163\n",
"7 88.591679 84.123694 38.288893 81.060171\n",
"8 99.708385 43.402550 77.401682 49.241732\n",
"9 10.327276 11.571198 2.568000 37.638858"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Appliquons le même traitement (d'accord, c'est un peu artificiel dans ce cas, mais c'est juste pour illustrer le concept):"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>exp1</th>\n",
" <th>exp2</th>\n",
" <th>exp3</th>\n",
" <th>exp4</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.355187</td>\n",
" <td>-0.565537</td>\n",
" <td>-0.246156</td>\n",
" <td>0.720187</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.262605</td>\n",
" <td>-0.351374</td>\n",
" <td>-0.535321</td>\n",
" <td>-0.965571</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>-0.442208</td>\n",
" <td>0.998828</td>\n",
" <td>-0.331522</td>\n",
" <td>-0.885723</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.373199</td>\n",
" <td>0.807518</td>\n",
" <td>-0.311598</td>\n",
" <td>-0.630695</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.948814</td>\n",
" <td>-0.239721</td>\n",
" <td>-0.589634</td>\n",
" <td>0.671121</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>0.846669</td>\n",
" <td>0.260389</td>\n",
" <td>0.406245</td>\n",
" <td>0.660398</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>-0.664427</td>\n",
" <td>-0.979604</td>\n",
" <td>0.456773</td>\n",
" <td>-0.918150</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>0.586786</td>\n",
" <td>0.643688</td>\n",
" <td>0.556179</td>\n",
" <td>-0.582042</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>-0.732903</td>\n",
" <td>-0.547813</td>\n",
" <td>0.907853</td>\n",
" <td>-0.854065</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>-0.784877</td>\n",
" <td>-0.838853</td>\n",
" <td>0.542653</td>\n",
" <td>-0.060218</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" exp1 exp2 exp3 exp4\n",
"0 0.355187 -0.565537 -0.246156 0.720187\n",
"1 0.262605 -0.351374 -0.535321 -0.965571\n",
"2 -0.442208 0.998828 -0.331522 -0.885723\n",
"3 0.373199 0.807518 -0.311598 -0.630695\n",
"4 0.948814 -0.239721 -0.589634 0.671121\n",
"5 0.846669 0.260389 0.406245 0.660398\n",
"6 -0.664427 -0.979604 0.456773 -0.918150\n",
"7 0.586786 0.643688 0.556179 -0.582042\n",
"8 -0.732903 -0.547813 0.907853 -0.854065\n",
"9 -0.784877 -0.838853 0.542653 -0.060218"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.apply(lambda x: np.sin(x))"
]
}
],
"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.4.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment