2022-2023/2nd/12_Indicateurs_statistiques/4E_etude_stat.ipynb

576 lines
10 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Statistiques\n",
"\n",
"Dans ce TP, vous travaillerez sur des séries statistiques en utilisant Python. \n",
"\n",
"Pour stocker une série de nombre en python, on utiliser les listes. Elles sont définies avec des [].\n",
"\n",
"La manipulation des listes est expliqué dans [Mémo sur les listes](#Mémo-sur-les-listes). Vous n'avez pas besoin de tout lire pour le moment par contre vous pouvez vous y reféré quand vous aurez besoin.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nombre de passagers\n",
"\n",
"On a compté le nombre de passagers dans des voitures à la sortie d'un village. Les valeurs ont été rassemblées dans la liste suivante\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"passagers = [4, 1, 4, 1, 2, 1, 5, 0, 3, 4, 0, 4, 0, 0, 4, 3, 2, 3, 5, 2, 5, 4, 1, 2, 1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Décrire l'étude statistique réalisée"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Quelle est l'effectif total de cette série?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. Calculer l'étendu de la série statistique."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. Calculer la moyenne."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5. Calculer la médiane."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nombre de kilomètre.\n",
"\n",
"Cette fois-ci, on a relevé les compteurs kilométriques de voiture sur un parking. Les valeurs ont été enregistrée dans la liste suivante"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Kilomètre au compteur\n",
"kilometres = [68806, 136429, 67417, 44743, 166108, 63686, 133615,\n",
" 97402, 131932, 60947, 166706, 194451, 103493, 71308, 69805]\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Décrire l'étude statistique réalisée"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Quelle est l'effectif total de cette série?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. Calculer l'étendu de la série statistique."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. Calculer la moyenne."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5. Calculer la médiane."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ecrire des fonctions\n",
"\n",
"Pour pouvoir rendre des lignes de code réutilisables, les programmeurs codent des `fonctions`.\n",
"\n",
"Par exemple, voici le code de la fonction `etendu` qui à partir d'une liste (`valeurs`) renvoie (`return`) l'étendu des valeurs de la liste."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def etendu(valeurs):\n",
" return max(valeurs) - min(valeurs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"L'avantage de programmer de cette façon est de pouvoir oublier la façon dont on calcule l'étendu mais de pouvoir la calculer.\n",
"\n",
"Par exemple pour calculer l'étendu du nombre de passagers, on écrira"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"etendu(passagers)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Calculer avec la fonction l'étendu des kilomètres."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Compléter la fonction suivante qui calcule la moyenne à partir d'une liste de valeurs."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def moyenne(valeurs):\n",
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. Utiliser cette fonction pour calculer la moyenne de passagers puis de kilometres."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. Compléter la fonction ci-dessous qui calcule la médiane"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def mediane(valeurs):\n",
" valeurs_triee = ...\n",
" effectif_total = ...\n",
" if ...:\n",
" return ...\n",
" else:\n",
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5. Utiliser cette fonction sur les listes passagers puis kilometres."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Mémo sur les listes\n",
"\n",
"Quelques précisions autour de la manipulation des listes avec Python"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"L = [5, 2, 1, 2, 4, 5, 5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `max`: valeur maximal de la liste"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"print(max(L))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `min`: valeur minimal de la liste "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"print(min(L))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `len`: calculer le nombre d'éléments dans la liste"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7\n"
]
}
],
"source": [
"print(len(L))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `sum`: calculer la somme des éléments de la liste"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"24\n"
]
}
],
"source": [
"print(sum(L))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `sorted`: trier les éléments de la liste"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 2, 4, 5, 5, 5]\n"
]
}
],
"source": [
"print(sorted(L))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Récupérer le i-ième élément de la liste (**/!\\ on commence à compter à partir de 0**)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n",
"1\n"
]
}
],
"source": [
"# Le premier élément de la liste\n",
"print(L[0])\n",
"# le 3e élément de la liste\n",
"print(L[2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Compter l'effectif d'une valeur"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"L.count(2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Faire une boucle sur tous les éléments de la liste"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n",
"2\n",
"1\n",
"2\n",
"4\n",
"5\n",
"5\n"
]
}
],
"source": [
"for x in L:\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}