"## Calcul de la moyenne avec la fonction *mean* de *numpy*"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14.113000000000001"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mean = np.mean(L)\n",
"mean"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Calcul de la moyenne *à la main*"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14.113000000000001"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mean2 = np.sum(L)/len(L)\n",
"mean2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Comparaison"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mean == mean2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Écart-type\n",
"\n",
"## Calcul de l'écart-type (corrigé) avec la fonction *std* de *numpy*"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4.334094455301447"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"SD = np.std(L, ddof=1)\n",
"SD"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Calcul de l'écart-type *à la main*"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4.3340944553014475"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from math import *\n",
"\n",
"V = 0\n",
"\n",
"for i in range(len(L)):\n",
" V += (L[i]-mean2)**2\n",
"\n",
"V /= len(L)-1\n",
"\n",
"SD2 = sqrt(V)\n",
"SD2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Comparaison"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"SD == SD2"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-8.881784197001252e-16"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"SD - SD2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Bien que les deux résultats ne soient pas **strictement** égaux, la différence est particulièrement faible (correspondant peut-être à la précision machine)."