|
@@ -0,0 +1,2067 @@
|
|
|
|
|
+import pandas as pd
|
|
|
|
|
+import numpy as np
|
|
|
|
|
+import seaborn as sns
|
|
|
|
|
+import matplotlib.pyplot as plt
|
|
|
|
|
+from scipy import stats
|
|
|
|
|
+import statsmodels.api as sm
|
|
|
|
|
+from statsmodels.formula.api import ols
|
|
|
|
|
+from scipy.stats import chi2_contingency
|
|
|
|
|
+import requests
|
|
|
|
|
+import pandas as pd
|
|
|
|
|
+from datetime import datetime, timedelta, date
|
|
|
|
|
+import json
|
|
|
|
|
+import os
|
|
|
|
|
+import statsmodels.api as sm
|
|
|
|
|
+from statsmodels.formula.api import ols
|
|
|
|
|
+from statsmodels.stats.multicomp import pairwise_tukeyhsd
|
|
|
|
|
+import statsmodels.formula.api as smf
|
|
|
|
|
+import matplotlib.patheffects as path_effects
|
|
|
|
|
+import matplotlib.dates as mdates
|
|
|
|
|
+from sklearn.ensemble import RandomForestRegressor
|
|
|
|
|
+from sklearn.model_selection import train_test_split
|
|
|
|
|
+from sklearn.metrics import mean_absolute_error, mean_squared_error
|
|
|
|
|
+
|
|
|
|
|
+pd.set_option('display.max_columns', None)
|
|
|
|
|
+dataset = 'export_des_missions_2025-06-13_100933-8546-653608357.csv'
|
|
|
|
|
+df = pd.read_csv(dataset, sep=';')
|
|
|
|
|
+df
|
|
|
|
|
+
|
|
|
|
|
+df.describe(include='all')
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+df1=df.copy()
|
|
|
|
|
+df1
|
|
|
|
|
+
|
|
|
|
|
+airports = 'airports.csv'
|
|
|
|
|
+airports = pd.read_csv(airports)
|
|
|
|
|
+airports
|
|
|
|
|
+
|
|
|
|
|
+df_iata_pays = airports[['iata_code', 'iso_country']]
|
|
|
|
|
+
|
|
|
|
|
+# Supprimer les lignes sans code IATA
|
|
|
|
|
+df_iata_pays = df_iata_pays.dropna(subset=['iata_code'])
|
|
|
|
|
+
|
|
|
|
|
+df_iata_pays
|
|
|
|
|
+
|
|
|
|
|
+# Renommer les colonnes avant la jointure pour éviter conflits
|
|
|
|
|
+df_iata_pays_origin = df_iata_pays.rename(columns={'iata_code': 'Origine', 'iso_country': 'pays_origine'})
|
|
|
|
|
+df_iata_pays_dest = df_iata_pays.rename(columns={'iata_code': 'Destination', 'iso_country': 'pays_destination'})
|
|
|
|
|
+
|
|
|
|
|
+# Merge origine
|
|
|
|
|
+df1 = df1.merge(df_iata_pays_origin, on='Origine', how='left')
|
|
|
|
|
+
|
|
|
|
|
+# Merge destination
|
|
|
|
|
+df1 = df1.merge(df_iata_pays_dest, on='Destination', how='left')
|
|
|
|
|
+
|
|
|
|
|
+df1
|
|
|
|
|
+
|
|
|
|
|
+!pip install pycountry
|
|
|
|
|
+
|
|
|
|
|
+import pycountry
|
|
|
|
|
+
|
|
|
|
|
+def iso_to_country_name(code):
|
|
|
|
|
+ try:
|
|
|
|
|
+ return pycountry.countries.get(alpha_2=code).name
|
|
|
|
|
+ except:
|
|
|
|
|
+ return code
|
|
|
|
|
+
|
|
|
|
|
+df1['Origine'] = df1['pays_origine'].apply(iso_to_country_name)
|
|
|
|
|
+df1['Destination'] = df1['pays_destination'].apply(iso_to_country_name)
|
|
|
|
|
+
|
|
|
|
|
+df1['code_pays_origine'] = df1['pays_origine']
|
|
|
|
|
+df1['code_pays_Destination'] = df1['pays_destination']
|
|
|
|
|
+
|
|
|
|
|
+df1 = df1.drop(columns=['pays_origine', 'pays_destination'])
|
|
|
|
|
+
|
|
|
|
|
+df1.head(40)
|
|
|
|
|
+
|
|
|
|
|
+API_KEY = 'NNPh5mZBBbBTNm6qnvvzI56gY1P339NZ'
|
|
|
|
|
+BASE_URL = 'https://calendarific.com/api/v2/holidays'
|
|
|
|
|
+CACHE_FILE = 'cache_feries.json'
|
|
|
|
|
+
|
|
|
|
|
+df1['date_arrivee'] = pd.to_datetime(df1['ST arrivée'], format='%d/%m/%Y %H:%M', errors='coerce').dt.date
|
|
|
|
|
+df1['date_depart'] = pd.to_datetime(df1['ST départ'], format='%d/%m/%Y %H:%M', errors='coerce').dt.date
|
|
|
|
|
+
|
|
|
|
|
+# Extraction des couples (pays, année) uniques dans origine et destination
|
|
|
|
|
+def extract_pays_annee(df1, col_pays, col_date):
|
|
|
|
|
+ subset = df1[[col_pays, col_date]].dropna()
|
|
|
|
|
+ subset['annee'] = subset[col_date].apply(lambda x: x.year)
|
|
|
|
|
+ return set(zip(subset[col_pays], subset['annee']))
|
|
|
|
|
+
|
|
|
|
|
+pays_annees_origine = extract_pays_annee(df1, 'code_pays_origine', 'date_arrivee')
|
|
|
|
|
+pays_annees_destination = extract_pays_annee(df1, 'code_pays_Destination', 'date_depart')
|
|
|
|
|
+pays_annees = pays_annees_origine.union(pays_annees_destination)
|
|
|
|
|
+
|
|
|
|
|
+# Charger le cache depuis fichier JSON
|
|
|
|
|
+if os.path.exists(CACHE_FILE):
|
|
|
|
|
+ with open(CACHE_FILE, 'r') as f:
|
|
|
|
|
+ cache_feries = json.load(f)
|
|
|
|
|
+ # Conversion des clés de string à tuple
|
|
|
|
|
+ cache_feries = {tuple(k.split(',')): v for k, v in cache_feries.items()}
|
|
|
|
|
+ print(f"Cache chargé, {len(cache_feries)} entrées")
|
|
|
|
|
+else:
|
|
|
|
|
+ cache_feries = {}
|
|
|
|
|
+ print("Pas de cache trouvé, nouvelle création")
|
|
|
|
|
+
|
|
|
|
|
+def save_cache():
|
|
|
|
|
+ with open(CACHE_FILE, 'w') as f:
|
|
|
|
|
+ json.dump({','.join(k): v for k, v in cache_feries.items()}, f, indent=2)
|
|
|
|
|
+
|
|
|
|
|
+def fetch_holidays(country_code, year):
|
|
|
|
|
+ key = (country_code, str(year))
|
|
|
|
|
+ if key in cache_feries:
|
|
|
|
|
+ return cache_feries[key]
|
|
|
|
|
+
|
|
|
|
|
+ print(f"Appel API pour {country_code} {year}...")
|
|
|
|
|
+ params = {
|
|
|
|
|
+ 'api_key': API_KEY,
|
|
|
|
|
+ 'country': country_code,
|
|
|
|
|
+ 'year': year,
|
|
|
|
|
+ 'type': 'national,religious'
|
|
|
|
|
+ }
|
|
|
|
|
+ try:
|
|
|
|
|
+ response = requests.get(BASE_URL, params=params)
|
|
|
|
|
+ response.raise_for_status()
|
|
|
|
|
+ holidays = response.json()['response']['holidays']
|
|
|
|
|
+ data = [
|
|
|
|
|
+ {
|
|
|
|
|
+ 'date': h['date']['iso'],
|
|
|
|
|
+ 'name': h['name'],
|
|
|
|
|
+ 'type': ','.join(h.get('type', []))
|
|
|
|
|
+ } for h in holidays
|
|
|
|
|
+ ]
|
|
|
|
|
+ cache_feries[key] = data
|
|
|
|
|
+ save_cache()
|
|
|
|
|
+ return data
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ print(f"Erreur API pour {country_code} {year} : {e}")
|
|
|
|
|
+ return []
|
|
|
|
|
+
|
|
|
|
|
+# Remplissage complet du cache (pour les pays/années manquants)
|
|
|
|
|
+for (pays, annee) in pays_annees:
|
|
|
|
|
+ fetch_holidays(pays, annee)
|
|
|
|
|
+
|
|
|
|
|
+def check_holiday(date, country_code):
|
|
|
|
|
+ if pd.isna(date) or pd.isna(country_code):
|
|
|
|
|
+ return False, None, None
|
|
|
|
|
+ key = (country_code, str(date.year))
|
|
|
|
|
+ holidays = cache_feries.get(key, [])
|
|
|
|
|
+ for h in holidays:
|
|
|
|
|
+ if h['date'] == date.isoformat():
|
|
|
|
|
+ return True, h['name'], h['type']
|
|
|
|
|
+ return False, None, None
|
|
|
|
|
+
|
|
|
|
|
+df1[['ferie_origine', 'fete_origine', 'type_fete_origine']] = df1.apply(
|
|
|
|
|
+ lambda row: pd.Series(check_holiday(row['date_arrivee'], row['code_pays_origine'])),
|
|
|
|
|
+ axis=1
|
|
|
|
|
+)
|
|
|
|
|
+df1[['ferie_destination', 'fete_destination', 'type_fete_destination']] = df1.apply(
|
|
|
|
|
+ lambda row: pd.Series(check_holiday(row['date_depart'], row['code_pays_Destination'])),
|
|
|
|
|
+ axis=1
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+df1.head(30)
|
|
|
|
|
+
|
|
|
|
|
+# Fonction d'alerte popup dans Jupyter
|
|
|
|
|
+def notify_jupyter(message):
|
|
|
|
|
+ from IPython.display import display, Javascript
|
|
|
|
|
+ js_code = f'alert("{message}");'
|
|
|
|
|
+ display(Javascript(js_code))
|
|
|
|
|
+
|
|
|
|
|
+def check_tomorrow_feries(df):
|
|
|
|
|
+ demain = datetime.now().date() + timedelta(days=1)
|
|
|
|
|
+
|
|
|
|
|
+ # Extraire tous les codes pays à vérifier (origine + destination)
|
|
|
|
|
+ pays_origine = df['code_pays_origine'].dropna().unique()
|
|
|
|
|
+ pays_destination = df['code_pays_Destination'].dropna().unique()
|
|
|
|
|
+ pays_a_verifier = set(pays_origine).union(pays_destination)
|
|
|
|
|
+
|
|
|
|
|
+ alerts = []
|
|
|
|
|
+ for pays in pays_a_verifier:
|
|
|
|
|
+ is_ferie, nom, type_fete = check_holiday(demain, pays)
|
|
|
|
|
+ if is_ferie:
|
|
|
|
|
+ alerts.append(f"{demain.isoformat()} - {pays} : {nom} ({type_fete})")
|
|
|
|
|
+
|
|
|
|
|
+ if alerts:
|
|
|
|
|
+ message = "Jour(s) férié(s) demain :\n" + "\n".join(alerts)
|
|
|
|
|
+ print(message)
|
|
|
|
|
+ notify_jupyter(message)
|
|
|
|
|
+ else:
|
|
|
|
|
+ print(f"Aucun jour férié demain ({demain.isoformat()}) pour les pays dans le dataframe.")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+check_tomorrow_feries(df1)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+#planifier tache : cron job(linux) / task scheduler(windows)
|
|
|
|
|
+def check_holiday_for_date(check_date, country_code):
|
|
|
|
|
+ if not check_date or not country_code:
|
|
|
|
|
+ return False, None, None
|
|
|
|
|
+ key = (country_code, str(check_date.year))
|
|
|
|
|
+ holidays = cache_feries.get(key, [])
|
|
|
|
|
+ if not holidays:
|
|
|
|
|
+ holidays = fetch_holidays(country_code, check_date.year)
|
|
|
|
|
+ for h in holidays:
|
|
|
|
|
+ if h['date'] == check_date.isoformat():
|
|
|
|
|
+ return True, h['name'], h['type']
|
|
|
|
|
+ return False, None, None
|
|
|
|
|
+
|
|
|
|
|
+date_test = date(2025, 6, 6)
|
|
|
|
|
+pays_origine = 'DE'
|
|
|
|
|
+pays_destination = 'TN'
|
|
|
|
|
+
|
|
|
|
|
+ferie_origine, nom_origine, type_origine = check_holiday_for_date(date_test, pays_origine)
|
|
|
|
|
+
|
|
|
|
|
+ferie_destination, nom_destination, type_destination = check_holiday_for_date(date_test, pays_destination)
|
|
|
|
|
+
|
|
|
|
|
+print(f"Date : {date_test}")
|
|
|
|
|
+print(f"Pays origine ({pays_origine}) : {'Férié' if ferie_origine else 'Non férié'} - {nom_origine} ({type_origine})")
|
|
|
|
|
+print(f"Pays destination ({pays_destination}) : {'Férié' if ferie_destination else 'Non férié'} - {nom_destination} ({type_destination})")
|
|
|
|
|
+
|
|
|
|
|
+# Nombre de PMR par Origine
|
|
|
|
|
+pmr_par_origine = df1.groupby('Origine').size().reset_index(name='Nombre_PMR')
|
|
|
|
|
+print(pmr_par_origine)
|
|
|
|
|
+
|
|
|
|
|
+pmr_par_origine_sorted = pmr_par_origine.sort_values(by='Nombre_PMR', ascending=False)
|
|
|
|
|
+
|
|
|
|
|
+data_to_plot = pmr_par_origine_sorted.copy()
|
|
|
|
|
+
|
|
|
|
|
+# barplot
|
|
|
|
|
+plt.figure(figsize=(max(20, len(data_to_plot) * 0.25), 10)) # ajuster automatiquement la largeur
|
|
|
|
|
+
|
|
|
|
|
+sns.barplot(data=data_to_plot, x='Origine', y='Nombre_PMR', palette='Blues_d')
|
|
|
|
|
+
|
|
|
|
|
+plt.title("Nombre de PMR par origine", fontsize=18)
|
|
|
|
|
+plt.xlabel("origine", fontsize=14)
|
|
|
|
|
+plt.ylabel("Nombre de PMR", fontsize=14)
|
|
|
|
|
+plt.xticks(rotation=90, fontsize=9)
|
|
|
|
|
+plt.yticks(fontsize=12)
|
|
|
|
|
+plt.grid(axis='y', linestyle='--', alpha=0.7)
|
|
|
|
|
+
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# Nombre de PMR par Origine
|
|
|
|
|
+pmr_par_destination = df1.groupby('Destination').size().reset_index(name='Nombre_PMR')
|
|
|
|
|
+print(pmr_par_destination)
|
|
|
|
|
+
|
|
|
|
|
+pmr_par_destination_sorted = pmr_par_destination.sort_values(by='Nombre_PMR', ascending=False)
|
|
|
|
|
+
|
|
|
|
|
+data_to_plot = pmr_par_destination_sorted.copy()
|
|
|
|
|
+
|
|
|
|
|
+# barplot
|
|
|
|
|
+plt.figure(figsize=(max(20, len(data_to_plot) * 0.25), 10))
|
|
|
|
|
+
|
|
|
|
|
+sns.barplot(data=data_to_plot, x='Destination', y='Nombre_PMR', palette='Blues_d')
|
|
|
|
|
+
|
|
|
|
|
+plt.title("Nombre de PMR par Destination", fontsize=18)
|
|
|
|
|
+plt.xlabel("Destination", fontsize=14)
|
|
|
|
|
+plt.ylabel("Nombre de PMR", fontsize=14)
|
|
|
|
|
+plt.xticks(rotation=90, fontsize=9)
|
|
|
|
|
+plt.yticks(fontsize=12)
|
|
|
|
|
+plt.grid(axis='y', linestyle='--', alpha=0.7)
|
|
|
|
|
+
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# test ANOVA (Analyse de la Variance)
|
|
|
|
|
+
|
|
|
|
|
+# une ligne = un pmr
|
|
|
|
|
+df1['PMR'] = 1
|
|
|
|
|
+
|
|
|
|
|
+# Tester si l'Origine est significative
|
|
|
|
|
+model_origine = ols('PMR ~ C(Origine)', data=df1).fit()
|
|
|
|
|
+anova_origine = sm.stats.anova_lm(model_origine, typ=2)
|
|
|
|
|
+print("ANOVA pour Origine :")
|
|
|
|
|
+print(anova_origine)
|
|
|
|
|
+
|
|
|
|
|
+# Tester si la Destination est significative
|
|
|
|
|
+model_destination = ols('PMR ~ C(Destination)', data=df1).fit()
|
|
|
|
|
+anova_destination = sm.stats.anova_lm(model_destination, typ=2)
|
|
|
|
|
+print("\nANOVA pour Destination :")
|
|
|
|
|
+print(anova_destination)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# tester la dépendance entre Origine et Destination
|
|
|
|
|
+
|
|
|
|
|
+contingency_table = pd.crosstab(df1['Origine'], df1['Destination'])
|
|
|
|
|
+
|
|
|
|
|
+# Test du Chi²
|
|
|
|
|
+chi2, p, dof, expected = chi2_contingency(contingency_table)
|
|
|
|
|
+
|
|
|
|
|
+print("Chi² :", chi2)
|
|
|
|
|
+print("Degrés de liberté :", dof)
|
|
|
|
|
+print("P-value :", p)
|
|
|
|
|
+
|
|
|
|
|
+import seaborn as sns
|
|
|
|
|
+import matplotlib.pyplot as plt
|
|
|
|
|
+
|
|
|
|
|
+plt.figure(figsize=(20, 10))
|
|
|
|
|
+sns.heatmap(contingency_table, cmap='Blues')
|
|
|
|
|
+plt.title('Heatmap Origine vs Destination')
|
|
|
|
|
+plt.xlabel('Destination')
|
|
|
|
|
+plt.ylabel('Origine')
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+df1['Origine'] = df1['Origine'].fillna('').str.strip()
|
|
|
|
|
+df1['Destination'] = df1['Destination'].fillna('').str.strip()
|
|
|
|
|
+
|
|
|
|
|
+# Détection des cas
|
|
|
|
|
+entree_et_reste = df1[(df1['Origine'] != '') & (df1['Destination'] == '')]
|
|
|
|
|
+sortie_seulement = df1[(df1['Origine'] == '') & (df1['Destination'] != '')]
|
|
|
|
|
+escale = df1[(df1['Origine'] != '') & (df1['Destination'] != '')]
|
|
|
|
|
+
|
|
|
|
|
+# Labels et valeurs
|
|
|
|
|
+labels = ['Entrée et restée en France', 'Sortie uniquement de France', 'Escale']
|
|
|
|
|
+counts = [len(entree_et_reste), len(sortie_seulement), len(escale)]
|
|
|
|
|
+total = len(df1)
|
|
|
|
|
+percentages = [round(c / total * 100, 2) for c in counts]
|
|
|
|
|
+
|
|
|
|
|
+# graphe
|
|
|
|
|
+plt.figure(figsize=(9, 6))
|
|
|
|
|
+bars = plt.bar(labels, counts, color=['#b0e0e6', '#add8e6', '#aec6cf'])
|
|
|
|
|
+plt.title('Répartition des passagers via la France')
|
|
|
|
|
+plt.ylabel('Nombre de passagers')
|
|
|
|
|
+plt.xticks(rotation=15)
|
|
|
|
|
+plt.grid(axis='y', linestyle='', alpha=0.7)
|
|
|
|
|
+
|
|
|
|
|
+# Ajout des valeurs + %
|
|
|
|
|
+for bar, count, perc in zip(bars, counts, percentages):
|
|
|
|
|
+ plt.text(bar.get_x() + bar.get_width()/2,
|
|
|
|
|
+ bar.get_height() + total*0.001, # un peu au-dessus
|
|
|
|
|
+ f'{count} ({perc}%)',
|
|
|
|
|
+ ha='center', va='bottom', fontsize=10, fontweight='bold')
|
|
|
|
|
+
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# Création de la variable 'org_dest'
|
|
|
|
|
+def type_passage(row):
|
|
|
|
|
+ if row['Origine'] != '' and row['Destination'] == '':
|
|
|
|
|
+ return '0' # entrée en France
|
|
|
|
|
+ elif row['Origine'] == '' and row['Destination'] != '':
|
|
|
|
|
+ return '1' # sortie de France
|
|
|
|
|
+ elif row['Origine'].upper() != 'FRANCE' and row['Destination'].upper() != 'FRANCE':
|
|
|
|
|
+ return '2' #escaleF
|
|
|
|
|
+
|
|
|
|
|
+df1['org_dest'] = df1.apply(type_passage, axis=1)
|
|
|
|
|
+df1 = df1.drop(columns=['PMR'])
|
|
|
|
|
+
|
|
|
|
|
+df1.head(40)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+contingency_table = df1['org_dest'].value_counts().sort_index()
|
|
|
|
|
+
|
|
|
|
|
+print(contingency_table)
|
|
|
|
|
+
|
|
|
|
|
+table = pd.DataFrame({'PMR_count': contingency_table})
|
|
|
|
|
+chi2, p, dof, expected = chi2_contingency([table['PMR_count'], expected := [table['PMR_count'].sum() / 3] * 3])
|
|
|
|
|
+
|
|
|
|
|
+print(f"\nChi2 = {chi2:.2f}, p-value = {p:.4f}")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# Conversion en datetime avec dayfirst=True (car format jour/mois/année)
|
|
|
|
|
+df1['ST arrivée'] = pd.to_datetime(df1['ST arrivée'], dayfirst=True)
|
|
|
|
|
+df1['ST départ'] = pd.to_datetime(df1['ST départ'], dayfirst=True)
|
|
|
|
|
+
|
|
|
|
|
+# Extraction jour de la semaine (0=lundi, 6=dimanche)
|
|
|
|
|
+df1['jour_semaine_entree'] = df1['ST arrivée'].dt.dayofweek
|
|
|
|
|
+df1['jour_semaine_depart'] = df1['ST départ'].dt.dayofweek
|
|
|
|
|
+
|
|
|
|
|
+# Format HH:MM
|
|
|
|
|
+df1['heure_entree'] = df1['ST arrivée'].dt.strftime('%H:%M')
|
|
|
|
|
+df1['heure_depart'] = df1['ST départ'].dt.strftime('%H:%M')
|
|
|
|
|
+
|
|
|
|
|
+# Mois : Janvier, Février, etc.
|
|
|
|
|
+df1['mois_entree'] = df1['ST arrivée'].dt.month_name()
|
|
|
|
|
+df1['mois_depart'] = df1['ST départ'].dt.month_name()
|
|
|
|
|
+
|
|
|
|
|
+df1.head()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# Mapper les numéros de jours (0 à 6) en noms
|
|
|
|
|
+jours = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi', 'Dimanche']
|
|
|
|
|
+df1['jour_nom_entree'] = df1['jour_semaine_entree'].map(dict(enumerate(jours)))
|
|
|
|
|
+df1['jour_nom_depart'] = df1['jour_semaine_depart'].map(dict(enumerate(jours)))
|
|
|
|
|
+
|
|
|
|
|
+def countplot_avec_valeurs(data, colonne, ordre, titre, xlabel, ylabel):
|
|
|
|
|
+ plt.figure(figsize=(10, 4))
|
|
|
|
|
+ ax = sns.countplot(data=data, x=colonne, order=ordre)
|
|
|
|
|
+ plt.title(titre)
|
|
|
|
|
+ plt.xlabel(xlabel)
|
|
|
|
|
+ plt.ylabel(ylabel)
|
|
|
|
|
+ plt.xticks(rotation=45)
|
|
|
|
|
+
|
|
|
|
|
+ # Ajouter les valeurs au-dessus des barres
|
|
|
|
|
+ for p in ax.patches:
|
|
|
|
|
+ hauteur = p.get_height()
|
|
|
|
|
+ ax.text(p.get_x() + p.get_width() / 2., hauteur + 3, # +3 pour décoller un peu du sommet
|
|
|
|
|
+ int(hauteur), ha='center', fontsize=10)
|
|
|
|
|
+
|
|
|
|
|
+ plt.tight_layout()
|
|
|
|
|
+ plt.show()
|
|
|
|
|
+
|
|
|
|
|
+countplot_avec_valeurs(df1, 'jour_nom_entree', jours,
|
|
|
|
|
+ 'Répartition des entrées par jour de la semaine',
|
|
|
|
|
+ 'Jour de la semaine', 'Nombre d\'entrées')
|
|
|
|
|
+
|
|
|
|
|
+countplot_avec_valeurs(df1, 'jour_nom_depart', jours,
|
|
|
|
|
+ 'Répartition des départs par jour de la semaine',
|
|
|
|
|
+ 'Jour de la semaine', 'Nombre de départs')
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+mois_ordre = ['January', 'February', 'March', 'April', 'May', 'June',
|
|
|
|
|
+ 'July', 'August', 'September', 'October', 'November', 'December']
|
|
|
|
|
+
|
|
|
|
|
+# Filtrer les mois présents dans la colonne 'mois_entree' et 'mois_depart'
|
|
|
|
|
+mois_ordre_entree = [m for m in mois_ordre if m in df1['mois_entree'].unique()]
|
|
|
|
|
+mois_ordre_depart = [m for m in mois_ordre if m in df1['mois_depart'].unique()]
|
|
|
|
|
+
|
|
|
|
|
+def countplot_avec_valeurs(data, colonne, ordre, titre, xlabel, ylabel):
|
|
|
|
|
+ plt.figure(figsize=(10, 4))
|
|
|
|
|
+ ax = sns.countplot(data=data, x=colonne, order=ordre)
|
|
|
|
|
+ plt.title(titre)
|
|
|
|
|
+ plt.xlabel(xlabel)
|
|
|
|
|
+ plt.ylabel(ylabel)
|
|
|
|
|
+ plt.xticks(rotation=45)
|
|
|
|
|
+
|
|
|
|
|
+ for p in ax.patches:
|
|
|
|
|
+ hauteur = p.get_height()
|
|
|
|
|
+ ax.text(p.get_x() + p.get_width() / 2., hauteur + 3,
|
|
|
|
|
+ int(hauteur), ha='center', fontsize=10)
|
|
|
|
|
+
|
|
|
|
|
+ plt.tight_layout()
|
|
|
|
|
+ plt.show()
|
|
|
|
|
+
|
|
|
|
|
+# Répartition par mois d'entrée
|
|
|
|
|
+countplot_avec_valeurs(df1, 'mois_entree', mois_ordre_entree,
|
|
|
|
|
+ 'Répartition des entrées par mois',
|
|
|
|
|
+ 'Mois', 'Nombre d\'entrées')
|
|
|
|
|
+
|
|
|
|
|
+# Répartition par mois de départ
|
|
|
|
|
+countplot_avec_valeurs(df1, 'mois_depart', mois_ordre_depart,
|
|
|
|
|
+ 'Répartition des départs par mois',
|
|
|
|
|
+ 'Mois', 'Nombre de départs')
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+API_URL = "https://iata-and-icao-codes.p.rapidapi.com/airlines"
|
|
|
|
|
+HEADERS = {
|
|
|
|
|
+ "x-rapidapi-key": "9bd1fe1013mshfef01143800e2cbp18a39ejsnfa4b363d6392",
|
|
|
|
|
+ "x-rapidapi-host": "iata-and-icao-codes.p.rapidapi.com"
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+response = requests.get(API_URL, headers=HEADERS)
|
|
|
|
|
+
|
|
|
|
|
+if response.status_code == 200:
|
|
|
|
|
+ data = response.json()
|
|
|
|
|
+ airlines_df = pd.DataFrame(data)
|
|
|
|
|
+
|
|
|
|
|
+ # Renommer les colonnes pour faciliter la fusion
|
|
|
|
|
+ airlines_arr = airlines_df.rename(columns={'iata_code': 'Code Cie arrivée', 'name': 'nom_compagnie_arr'})
|
|
|
|
|
+ airlines_dest = airlines_df.rename(columns={'iata_code': 'Code Cie départ', 'name': 'nom_compagnie_dest'})
|
|
|
|
|
+
|
|
|
|
|
+ df1 = df1.merge(airlines_arr[['Code Cie arrivée', 'nom_compagnie_arr']], on='Code Cie arrivée', how='left')
|
|
|
|
|
+
|
|
|
|
|
+ df1 = df1.merge(airlines_dest[['Code Cie départ', 'nom_compagnie_dest']], on='Code Cie départ', how='left')
|
|
|
|
|
+
|
|
|
|
|
+ # Dictionnaire de corrections manuelles si certaines compagnies manquent dans l'API
|
|
|
|
|
+ corrections = {
|
|
|
|
|
+ 'EZY': 'easyJet',
|
|
|
|
|
+ 'EJU': 'easyJet Europe',
|
|
|
|
|
+ 'VF': 'Volotea',
|
|
|
|
|
+ 'ENT': 'Enter Air',
|
|
|
|
|
+ 'N0': 'Norwegian Air Sweden'
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for code, name in corrections.items():
|
|
|
|
|
+ df1.loc[df1['Code Cie arrivée'] == code, 'nom_compagnie_arr'] = name
|
|
|
|
|
+ df1.loc[df1['Code Cie départ'] == code, 'nom_compagnie_dest'] = name
|
|
|
|
|
+
|
|
|
|
|
+ # Formatage final : Nom (Code)
|
|
|
|
|
+ df1['nom_compagnie_arr'] = df1.apply(
|
|
|
|
|
+ lambda row: f"{row['nom_compagnie_arr']} ({row['Code Cie arrivée']})"
|
|
|
|
|
+ if pd.notna(row['nom_compagnie_arr']) else row['Code Cie arrivée'],
|
|
|
|
|
+ axis=1
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ df1['nom_compagnie_dest'] = df1.apply(
|
|
|
|
|
+ lambda row: f"{row['nom_compagnie_dest']} ({row['Code Cie départ']})"
|
|
|
|
|
+ if pd.notna(row['nom_compagnie_dest']) else row['Code Cie départ'],
|
|
|
|
|
+ axis=1
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ df1[['Code Cie arrivée', 'nom_compagnie_arr', 'Code Cie départ', 'nom_compagnie_dest']].head(40)
|
|
|
|
|
+
|
|
|
|
|
+else:
|
|
|
|
|
+ print(f"Erreur API: code {response.status_code}")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# Associer chaque vol à sa compagnie
|
|
|
|
|
+compagnie_par_vol_arr = df1.groupby(['Num Vol arrivée'])['nom_compagnie_arr'].first()
|
|
|
|
|
+compagnie_par_vol_dep = df1.groupby(['Num Vol départ'])['nom_compagnie_dest'].first()
|
|
|
|
|
+
|
|
|
|
|
+# Compter le nombre de vols uniques par compagnie (arrivée et départ)
|
|
|
|
|
+nb_vols_uniques_arr = compagnie_par_vol_arr.value_counts().sort_values(ascending=False)
|
|
|
|
|
+nb_vols_uniques_dep = compagnie_par_vol_dep.value_counts().sort_values(ascending=False)
|
|
|
|
|
+
|
|
|
|
|
+fig, axes = plt.subplots(1, 2, figsize=(18, 6))
|
|
|
|
|
+
|
|
|
|
|
+# ARRIVÉE
|
|
|
|
|
+sns.barplot(x=nb_vols_uniques_arr.index, y=nb_vols_uniques_arr.values,
|
|
|
|
|
+ palette="Blues_d", ax=axes[0])
|
|
|
|
|
+axes[0].set_title("Vols uniques par compagnie (arrivée)")
|
|
|
|
|
+axes[0].set_xlabel("Compagnie")
|
|
|
|
|
+axes[0].set_ylabel("Nombre de vols uniques")
|
|
|
|
|
+axes[0].tick_params(axis='x', rotation=90)
|
|
|
|
|
+for i, v in enumerate(nb_vols_uniques_arr.values):
|
|
|
|
|
+ axes[0].text(i, v + 0.5, str(v), ha='center', fontsize=8)
|
|
|
|
|
+
|
|
|
|
|
+# DÉPART
|
|
|
|
|
+sns.barplot(x=nb_vols_uniques_dep.index, y=nb_vols_uniques_dep.values,
|
|
|
|
|
+ palette="Greens_d", ax=axes[1])
|
|
|
|
|
+axes[1].set_title("Vols uniques par compagnie (départ)")
|
|
|
|
|
+axes[1].set_xlabel("Compagnie")
|
|
|
|
|
+axes[1].set_ylabel("Nombre de vols uniques")
|
|
|
|
|
+axes[1].tick_params(axis='x', rotation=90)
|
|
|
|
|
+for i, v in enumerate(nb_vols_uniques_dep.values):
|
|
|
|
|
+ axes[1].text(i, v + 0.5, str(v), ha='center', fontsize=8)
|
|
|
|
|
+
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# Comptage des compagnies à l’arrivée
|
|
|
|
|
+compte_arr = df1['nom_compagnie_arr'].value_counts()
|
|
|
|
|
+
|
|
|
|
|
+# Comptage des compagnies au départ
|
|
|
|
|
+compte_dep = df1['nom_compagnie_dest'].value_counts()
|
|
|
|
|
+
|
|
|
|
|
+def ajouter_valeurs_barres(ax, valeurs):
|
|
|
|
|
+ for i, v in enumerate(valeurs):
|
|
|
|
|
+ ax.text(i, v + max(valeurs)*0.01, str(v), ha='center', va='bottom', fontsize=9)
|
|
|
|
|
+
|
|
|
|
|
+# ARRIVÉE
|
|
|
|
|
+plt.figure(figsize=(15, 6))
|
|
|
|
|
+ax1 = sns.barplot(x=compte_arr.index, y=compte_arr.values, palette='Blues_d')
|
|
|
|
|
+plt.title("Nombre de PMR par compagnie aérienne à l’arrivée")
|
|
|
|
|
+plt.xlabel("Compagnie")
|
|
|
|
|
+plt.ylabel("Nombre total PMR par compagnie")
|
|
|
|
|
+plt.xticks(rotation=90)
|
|
|
|
|
+ajouter_valeurs_barres(ax1, compte_arr.values)
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+# DÉPART
|
|
|
|
|
+plt.figure(figsize=(15, 6))
|
|
|
|
|
+ax2 = sns.barplot(x=compte_dep.index, y=compte_dep.values, palette='Greens_d')
|
|
|
|
|
+plt.title("Nombre de PMR par compagnie aérienne départ")
|
|
|
|
|
+plt.xlabel("Compagnie")
|
|
|
|
|
+plt.ylabel("Nombre Total PMR")
|
|
|
|
|
+plt.xticks(rotation=90)
|
|
|
|
|
+ajouter_valeurs_barres(ax2, compte_dep.values)
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# Nombre de PMR par vol arrivée
|
|
|
|
|
+pmr_par_vol_arrivee = df1.groupby('Num Vol arrivée').size().sort_values(ascending=False)
|
|
|
|
|
+
|
|
|
|
|
+# Compagnie associée à chaque vol arrivée
|
|
|
|
|
+compagnie_par_vol_arrivee = df1.groupby('Num Vol arrivée')['nom_compagnie_arr'].first()
|
|
|
|
|
+
|
|
|
|
|
+df_vols_arrivee = pd.DataFrame({
|
|
|
|
|
+ 'nombre_pmr': pmr_par_vol_arrivee,
|
|
|
|
|
+ 'compagnie': compagnie_par_vol_arrivee
|
|
|
|
|
+}).dropna() # <-- Important pour éviter NaN
|
|
|
|
|
+
|
|
|
|
|
+# Moyenne PMR par vol par compagnie arrivée
|
|
|
|
|
+moyenne_pmr_par_compagnie_arrivee = (
|
|
|
|
|
+ df_vols_arrivee.groupby('compagnie')['nombre_pmr']
|
|
|
|
|
+ .mean()
|
|
|
|
|
+ .sort_values(ascending=False)
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+# Nombre de PMR par vol arrivée
|
|
|
|
|
+
|
|
|
|
|
+plt.figure(figsize=(18, 6))
|
|
|
|
|
+sns.barplot(x=pmr_par_vol_arrivee.index.astype(str), y=pmr_par_vol_arrivee.values, palette='Blues_d')
|
|
|
|
|
+plt.title("Nombre de PMR par vol arrivée")
|
|
|
|
|
+plt.xlabel("Vol arrivée (CodeCie_NumVol)")
|
|
|
|
|
+plt.ylabel("Nombre de PMR")
|
|
|
|
|
+plt.xticks(rotation=90)
|
|
|
|
|
+
|
|
|
|
|
+for i, v in enumerate(pmr_par_vol_arrivee.values):
|
|
|
|
|
+ if pd.notna(v) and pd.notnull(v): # Vérification
|
|
|
|
|
+ plt.text(i, v + 0.5, str(int(v)), ha='center', fontsize=8)
|
|
|
|
|
+
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+# Moyenne PMR par vol par compagnie arrivée
|
|
|
|
|
+
|
|
|
|
|
+plt.figure(figsize=(15, 6))
|
|
|
|
|
+sns.barplot(x=moyenne_pmr_par_compagnie_arrivee.index, y=moyenne_pmr_par_compagnie_arrivee.values, palette='coolwarm')
|
|
|
|
|
+plt.title("Moyenne de PMR par vol par compagnie (arrivée)")
|
|
|
|
|
+plt.xlabel("Compagnie")
|
|
|
|
|
+plt.ylabel("Moyenne PMR par vol")
|
|
|
|
|
+plt.xticks(rotation=90)
|
|
|
|
|
+
|
|
|
|
|
+for i, v in enumerate(moyenne_pmr_par_compagnie_arrivee.values):
|
|
|
|
|
+ if pd.notna(v) and pd.notnull(v): # Vérification
|
|
|
|
|
+ plt.text(i, v + 0.05, f"{v:.2f}", ha='center', fontsize=10)
|
|
|
|
|
+
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# Nombre de PMR par vol départ
|
|
|
|
|
+pmr_par_vol_depart = df1.groupby([ 'Num Vol départ']).size().sort_values(ascending=False)
|
|
|
|
|
+
|
|
|
|
|
+compagnie_par_vol_depart = df1.groupby([ 'Num Vol départ'])['nom_compagnie_dest'].first()
|
|
|
|
|
+
|
|
|
|
|
+df_vols_depart = pd.DataFrame({
|
|
|
|
|
+ 'nombre_pmr': pmr_par_vol_depart,
|
|
|
|
|
+ 'compagnie': compagnie_par_vol_depart
|
|
|
|
|
+})
|
|
|
|
|
+
|
|
|
|
|
+# Calcul moyenne PMR par vol par compagnie départ
|
|
|
|
|
+moyenne_pmr_par_compagnie_depart = df_vols_depart.groupby('compagnie')['nombre_pmr'].mean().sort_values(ascending=False)
|
|
|
|
|
+
|
|
|
|
|
+# Nombre PMR par vol départ
|
|
|
|
|
+
|
|
|
|
|
+plt.figure(figsize=(18, 6))
|
|
|
|
|
+sns.barplot(x=pmr_par_vol_depart.index, y=pmr_par_vol_depart.values, palette='Greens_d')
|
|
|
|
|
+plt.title("Nombre de PMR par vol départ")
|
|
|
|
|
+plt.xlabel("Vol départ (CodeCie_NumVol)")
|
|
|
|
|
+plt.ylabel("Nombre de PMR")
|
|
|
|
|
+plt.xticks(rotation=90)
|
|
|
|
|
+
|
|
|
|
|
+for i, v in enumerate(pmr_par_vol_depart.values):
|
|
|
|
|
+ plt.text(i, v + 0.5, str(v), ha='center', fontsize=8)
|
|
|
|
|
+
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+# Moyenne PMR par vol par compagnie départ
|
|
|
|
|
+
|
|
|
|
|
+plt.figure(figsize=(15, 6))
|
|
|
|
|
+sns.barplot(x=moyenne_pmr_par_compagnie_depart.index, y=moyenne_pmr_par_compagnie_depart.values, palette='coolwarm')
|
|
|
|
|
+plt.title("Moyenne de PMR par vol par compagnie (départ)")
|
|
|
|
|
+plt.xlabel("Compagnie")
|
|
|
|
|
+plt.ylabel("Moyenne PMR par vol")
|
|
|
|
|
+plt.xticks(rotation=90)
|
|
|
|
|
+
|
|
|
|
|
+for i, v in enumerate(moyenne_pmr_par_compagnie_depart.values):
|
|
|
|
|
+ plt.text(i, v + 0.05, f"{v:.2f}", ha='center', fontsize=10)
|
|
|
|
|
+
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# Calcul PMR par vol arrivée
|
|
|
|
|
+pmr_par_vol_arrivee = df1.groupby('Num Vol arrivée').size().reset_index(name='nombre_pmr')
|
|
|
|
|
+compagnie_arr = df1.groupby('Num Vol arrivée')['nom_compagnie_arr'].first().reset_index()
|
|
|
|
|
+df_vols_arrivee = pmr_par_vol_arrivee.merge(compagnie_arr, on='Num Vol arrivée')
|
|
|
|
|
+
|
|
|
|
|
+# Calcul PMR par vol départ
|
|
|
|
|
+pmr_par_vol_depart = df1.groupby('Num Vol départ').size().reset_index(name='nombre_pmr')
|
|
|
|
|
+compagnie_dep = df1.groupby('Num Vol départ')['nom_compagnie_dest'].first().reset_index()
|
|
|
|
|
+df_vols_depart = pmr_par_vol_depart.merge(compagnie_dep, on='Num Vol départ')
|
|
|
|
|
+
|
|
|
|
|
+# ANOVA arrivée
|
|
|
|
|
+modele_arr = ols('nombre_pmr ~ C(nom_compagnie_arr)', data=df_vols_arrivee).fit()
|
|
|
|
|
+anova_arr = sm.stats.anova_lm(modele_arr, typ=2)
|
|
|
|
|
+print("ANOVA - nombre PMR par compagnie (arrivée) :")
|
|
|
|
|
+print(anova_arr)
|
|
|
|
|
+
|
|
|
|
|
+# Tukey HSD arrivée
|
|
|
|
|
+tukey_arr = pairwise_tukeyhsd(endog=df_vols_arrivee['nombre_pmr'],
|
|
|
|
|
+ groups=df_vols_arrivee['nom_compagnie_arr'],
|
|
|
|
|
+ alpha=0.05)
|
|
|
|
|
+print(tukey_arr.summary())
|
|
|
|
|
+tukey_arr.plot_simultaneous()
|
|
|
|
|
+plt.title('Tukey HSD - PMR par compagnie (arrivée)')
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+# --- ANOVA départ ---
|
|
|
|
|
+modele_dep = ols('nombre_pmr ~ C(nom_compagnie_dest)', data=df_vols_depart).fit()
|
|
|
|
|
+anova_dep = sm.stats.anova_lm(modele_dep, typ=2)
|
|
|
|
|
+print("ANOVA - nombre PMR par compagnie (départ) :")
|
|
|
|
|
+print(anova_dep)
|
|
|
|
|
+
|
|
|
|
|
+# Tukey HSD départ
|
|
|
|
|
+tukey_dep = pairwise_tukeyhsd(endog=df_vols_depart['nombre_pmr'],
|
|
|
|
|
+ groups=df_vols_depart['nom_compagnie_dest'],
|
|
|
|
|
+ alpha=0.05)
|
|
|
|
|
+print(tukey_dep.summary())
|
|
|
|
|
+tukey_dep.plot_simultaneous()
|
|
|
|
|
+plt.title('Tukey HSD - PMR par compagnie (départ)')
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# Calcul du nombre de PMR par compagnie à l'arrivée
|
|
|
|
|
+pmr_arr = df1['nom_compagnie_arr'].value_counts(dropna=False).reset_index()
|
|
|
|
|
+pmr_arr.columns = ['compagnie', 'total_pmr']
|
|
|
|
|
+
|
|
|
|
|
+# Calcul de la distribution cumulée (en excluant NaN dans le calcul)
|
|
|
|
|
+pmr_arr_nonan = pmr_arr[pmr_arr['compagnie'].notna()].copy()
|
|
|
|
|
+pmr_arr_nonan['cumsum'] = pmr_arr_nonan['total_pmr'].cumsum() / pmr_arr_nonan['total_pmr'].sum()
|
|
|
|
|
+
|
|
|
|
|
+# Identifier les compagnies représentant 80% des PMR
|
|
|
|
|
+top_80_arr = pmr_arr_nonan[pmr_arr_nonan['cumsum'] <= 0.8]['compagnie'].tolist()
|
|
|
|
|
+
|
|
|
|
|
+# Créer une nouvelle colonne dans df1 avec regroupement
|
|
|
|
|
+def simplify_compagnie(x):
|
|
|
|
|
+ if pd.isna(x):
|
|
|
|
|
+ return x # garder NaN tel quel
|
|
|
|
|
+ elif x in top_80_arr:
|
|
|
|
|
+ return x
|
|
|
|
|
+ else:
|
|
|
|
|
+ return 'Other'
|
|
|
|
|
+
|
|
|
|
|
+df1['compagnie_arr_simplifiée'] = df1['nom_compagnie_arr'].apply(simplify_compagnie)
|
|
|
|
|
+
|
|
|
|
|
+# Recompter les PMR par compagnie simplifiée (y compris NaN)
|
|
|
|
|
+pmr_arr_grouped = df1['compagnie_arr_simplifiée'].value_counts(dropna=False).reset_index()
|
|
|
|
|
+pmr_arr_grouped.columns = ['compagnie', 'total_pmr']
|
|
|
|
|
+
|
|
|
|
|
+# Nettoyer pour le graphique : enlever NaN
|
|
|
|
|
+pmr_arr_grouped_clean = pmr_arr_grouped[pmr_arr_grouped['compagnie'].notna()]
|
|
|
|
|
+
|
|
|
|
|
+# Garder seulement le top 80, exclure 'Other'
|
|
|
|
|
+pmr_arr_top = pmr_arr_grouped_clean[pmr_arr_grouped_clean['compagnie'] != 'Other']
|
|
|
|
|
+
|
|
|
|
|
+# Tracer la courbe avec uniquement le top 80, sans 'Other'
|
|
|
|
|
+plt.figure(figsize=(12, 6))
|
|
|
|
|
+plt.bar(pmr_arr_top['compagnie'], pmr_arr_top['total_pmr'], color='steelblue')
|
|
|
|
|
+plt.title("Top compagnies représentant 80% des PMR à l'arrivée")
|
|
|
|
|
+plt.xlabel("Compagnie aérienne")
|
|
|
|
|
+plt.ylabel("Nombre de PMR")
|
|
|
|
|
+plt.xticks(rotation=90)
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# Calcul du nombre de PMR par compagnie à la destination (en comptant aussi les NaN)
|
|
|
|
|
+pmr_dest = df1['nom_compagnie_dest'].value_counts(dropna=False).reset_index()
|
|
|
|
|
+pmr_dest.columns = ['compagnie', 'total_pmr']
|
|
|
|
|
+
|
|
|
|
|
+# Calcul de la distribution cumulée (sur les compagnies non NaN)
|
|
|
|
|
+pmr_dest_nonan = pmr_dest[pmr_dest['compagnie'].notna()]
|
|
|
|
|
+pmr_dest_nonan.loc[:, 'cumsum'] = pmr_dest_nonan['total_pmr'].cumsum() / pmr_dest_nonan['total_pmr'].sum()
|
|
|
|
|
+
|
|
|
|
|
+# Identifier les compagnies représentant 80% des PMR à la destination
|
|
|
|
|
+top_80_dest = pmr_dest_nonan[pmr_dest_nonan['cumsum'] <= 0.8]['compagnie'].tolist()
|
|
|
|
|
+
|
|
|
|
|
+# Fonction pour simplifier la colonne des compagnies à la destination
|
|
|
|
|
+def simplify_compagnie_dest(x):
|
|
|
|
|
+ if pd.isna(x):
|
|
|
|
|
+ return x # garder NaN tel quel
|
|
|
|
|
+ elif x in top_80_dest:
|
|
|
|
|
+ return x
|
|
|
|
|
+ else:
|
|
|
|
|
+ return 'Other'
|
|
|
|
|
+
|
|
|
|
|
+df1['compagnie_dest_simplifiée'] = df1['nom_compagnie_dest'].apply(simplify_compagnie_dest)
|
|
|
|
|
+
|
|
|
|
|
+# Recompter les PMR par compagnie simplifiée à la destination (avec NaN)
|
|
|
|
|
+pmr_dest_grouped = df1['compagnie_dest_simplifiée'].value_counts(dropna=False).reset_index()
|
|
|
|
|
+pmr_dest_grouped.columns = ['compagnie', 'total_pmr']
|
|
|
|
|
+
|
|
|
|
|
+# Filtrer pour le graphique : garder seulement le top 80, exclure 'Other' et NaN
|
|
|
|
|
+pmr_dest_top = pmr_dest_grouped[
|
|
|
|
|
+ (pmr_dest_grouped['compagnie'] != 'Other') &
|
|
|
|
|
+ (pmr_dest_grouped['compagnie'].notna())
|
|
|
|
|
+]
|
|
|
|
|
+
|
|
|
|
|
+# Tracer la courbe avec uniquement le top 80, sans 'Other'
|
|
|
|
|
+plt.figure(figsize=(12, 6))
|
|
|
|
|
+plt.bar(pmr_dest_top['compagnie'], pmr_dest_top['total_pmr'], color='steelblue')
|
|
|
|
|
+plt.title("Top compagnies représentant 80% des PMR à la destination")
|
|
|
|
|
+plt.xlabel("Compagnie aérienne")
|
|
|
|
|
+plt.ylabel("Nombre de PMR")
|
|
|
|
|
+plt.xticks(rotation=90)
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+df1
|
|
|
|
|
+
|
|
|
|
|
+def time_to_minutes(t):
|
|
|
|
|
+ try:
|
|
|
|
|
+ if pd.isna(t):
|
|
|
|
|
+ return np.nan
|
|
|
|
|
+ h, m = map(int, t.split(':'))
|
|
|
|
|
+ return h * 60 + m
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ return np.nan
|
|
|
|
|
+
|
|
|
|
|
+# encodage cyclique (sin/cos)
|
|
|
|
|
+def encode_cyclic_period(period_idx, total_periods=58):
|
|
|
|
|
+ if np.isnan(period_idx):
|
|
|
|
|
+ return np.nan, np.nan
|
|
|
|
|
+ angle = 2 * np.pi * period_idx / total_periods
|
|
|
|
|
+ return np.sin(angle), np.cos(angle)
|
|
|
|
|
+
|
|
|
|
|
+# Conversion heures vers minutes
|
|
|
|
|
+df1['minutes_entree'] = df1['heure_entree'].apply(time_to_minutes)
|
|
|
|
|
+df1['minutes_depart'] = df1['heure_depart'].apply(time_to_minutes)
|
|
|
|
|
+
|
|
|
|
|
+# Calcul des périodes entières (58 périodes de 24.83 minutes environ)
|
|
|
|
|
+periode_duration = 1440 / 58 # ≈ 24.8276 min
|
|
|
|
|
+df1['periode_entree'] = df1['minutes_entree'].apply(
|
|
|
|
|
+ lambda x: int(x // periode_duration) if not np.isnan(x) else np.nan)
|
|
|
|
|
+df1['periode_depart'] = df1['minutes_depart'].apply(
|
|
|
|
|
+ lambda x: int(x // periode_duration) if not np.isnan(x) else np.nan)
|
|
|
|
|
+
|
|
|
|
|
+# Encodage cyclique
|
|
|
|
|
+df1[['periode_entree_sin', 'periode_entree_cos']] = df1['periode_entree'].apply(
|
|
|
|
|
+ lambda x: pd.Series(encode_cyclic_period(x)))
|
|
|
|
|
+df1[['periode_depart_sin', 'periode_depart_cos']] = df1['periode_depart'].apply(
|
|
|
|
|
+ lambda x: pd.Series(encode_cyclic_period(x)))
|
|
|
|
|
+
|
|
|
|
|
+df1
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+plt.figure(figsize=(8, 8))
|
|
|
|
|
+plt.title("Visualisation encodage cyclique des heures (arrivée et départ)")
|
|
|
|
|
+
|
|
|
|
|
+# Scatter points
|
|
|
|
|
+plt.scatter(df1['periode_entree_cos'], df1['periode_entree_sin'], color='blue', label='Heure Arrivée', alpha=0.6)
|
|
|
|
|
+plt.scatter(df1['periode_depart_cos'], df1['periode_depart_sin'], color='red', label='Heure Départ', alpha=0.6)
|
|
|
|
|
+
|
|
|
|
|
+# Circle
|
|
|
|
|
+circle = plt.Circle((0, 0), 1, color='grey', fill=False, linestyle='--')
|
|
|
|
|
+plt.gca().add_artist(circle)
|
|
|
|
|
+
|
|
|
|
|
+def annotated_text(x, y, text, color):
|
|
|
|
|
+ txt = plt.text(x, y, text, fontsize=10, color=color, fontweight='bold')
|
|
|
|
|
+ txt.set_path_effects([path_effects.Stroke(linewidth=2, foreground='white'), path_effects.Normal()])
|
|
|
|
|
+
|
|
|
|
|
+# periodes Unique entrée
|
|
|
|
|
+unique_entree_periods = df1['periode_entree'].dropna().unique()
|
|
|
|
|
+for periode in unique_entree_periods:
|
|
|
|
|
+ periode = int(periode)
|
|
|
|
|
+ minutes = periode * 25
|
|
|
|
|
+ h = minutes // 60
|
|
|
|
|
+ m = minutes % 60
|
|
|
|
|
+ # Calculate cos and sin for the label position (radius a bit > 1 for visibility)
|
|
|
|
|
+ angle = 2 * np.pi * (periode / 58)
|
|
|
|
|
+ x = 1.1 * np.cos(angle)
|
|
|
|
|
+ y = 1.1 * np.sin(angle)
|
|
|
|
|
+ annotated_text(x, y, f"{h:02d}h{m:02d}", 'blue')
|
|
|
|
|
+
|
|
|
|
|
+# periodes Unique départ
|
|
|
|
|
+unique_depart_periods = df1['periode_depart'].dropna().unique()
|
|
|
|
|
+for periode in unique_depart_periods:
|
|
|
|
|
+ periode = int(periode)
|
|
|
|
|
+ minutes = periode * 25
|
|
|
|
|
+ h = minutes // 60
|
|
|
|
|
+ m = minutes % 60
|
|
|
|
|
+ angle = 2 * np.pi * (periode / 58)
|
|
|
|
|
+ x = 1.2 * np.cos(angle) # Slightly farther out to not overlap entrée labels
|
|
|
|
|
+ y = 1.2 * np.sin(angle)
|
|
|
|
|
+ annotated_text(x, y, f"{h:02d}h{m:02d}", 'red')
|
|
|
|
|
+
|
|
|
|
|
+plt.xlabel('Cosinus')
|
|
|
|
|
+plt.ylabel('Sinus')
|
|
|
|
|
+plt.legend()
|
|
|
|
|
+plt.grid(True)
|
|
|
|
|
+plt.axis('equal')
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+entree_counts = df1['periode_entree'].dropna().astype(int).value_counts().sort_index()
|
|
|
|
|
+depart_counts = df1['periode_depart'].dropna().astype(int).value_counts().sort_index()
|
|
|
|
|
+
|
|
|
|
|
+# Fusionner dans un même index
|
|
|
|
|
+all_indices = sorted(set(entree_counts.index).union(depart_counts.index))
|
|
|
|
|
+entree = [entree_counts.get(i, 0) for i in all_indices]
|
|
|
|
|
+depart = [depart_counts.get(i, 0) for i in all_indices]
|
|
|
|
|
+
|
|
|
|
|
+# Créer les labels combinés (indice + heure)
|
|
|
|
|
+def indice_to_hour_str(indice):
|
|
|
|
|
+ total_minutes = indice * 25
|
|
|
|
|
+ h = total_minutes // 60
|
|
|
|
|
+ m = total_minutes % 60
|
|
|
|
|
+ return f"{indice:02d} ({h:02d}h{m:02d})"
|
|
|
|
|
+
|
|
|
|
|
+horaire_labels = [indice_to_hour_str(i) for i in all_indices]
|
|
|
|
|
+
|
|
|
|
|
+# Positions des barres
|
|
|
|
|
+x = np.arange(len(all_indices))
|
|
|
|
|
+width = 0.4
|
|
|
|
|
+
|
|
|
|
|
+# Barplot
|
|
|
|
|
+plt.figure(figsize=(14, 6))
|
|
|
|
|
+plt.bar(x - width/2, entree, width=width, label='Arrivée', color='blue')
|
|
|
|
|
+plt.bar(x + width/2, depart, width=width, label='Départ', color='red')
|
|
|
|
|
+
|
|
|
|
|
+plt.xlabel("Indice de période (tranche de 25 minutes)")
|
|
|
|
|
+plt.ylabel("Nombre de PMR")
|
|
|
|
|
+plt.title("Nombre d'arrivées et de départs par tranche horaire")
|
|
|
|
|
+plt.xticks(x, horaire_labels, rotation=90)
|
|
|
|
|
+plt.legend()
|
|
|
|
|
+plt.grid(True, axis='y', linestyle='--', alpha=0.7)
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# Extraire le jour de la semaine pour arrivée et départ
|
|
|
|
|
+df1['jour_semaine_arrivee'] = df1['ST arrivée'].dt.day_name()
|
|
|
|
|
+df1['jour_semaine_depart'] = df1['ST départ'].dt.day_name()
|
|
|
|
|
+
|
|
|
|
|
+# Compter le nombre de PMR par jour de la semaine (arrivée)
|
|
|
|
|
+pmr_arrivee_sem = df1.groupby('jour_semaine_arrivee').size().reindex(
|
|
|
|
|
+ ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+# Compter le nombre de PMR par jour de la semaine (départ)
|
|
|
|
|
+pmr_depart_sem = df1.groupby('jour_semaine_depart').size().reindex(
|
|
|
|
|
+ ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+# Créer un DataFrame pour faciliter le tracé
|
|
|
|
|
+df_sem = pd.DataFrame({
|
|
|
|
|
+ 'Arrivées': pmr_arrivee_sem,
|
|
|
|
|
+ 'Départs': pmr_depart_sem
|
|
|
|
|
+})
|
|
|
|
|
+
|
|
|
|
|
+df_sem.plot(kind='bar', figsize=(10,5), color=['skyblue', 'salmon'])
|
|
|
|
|
+
|
|
|
|
|
+plt.title("Nombre total de PMR par jour de la semaine (Arrivées vs Départs)")
|
|
|
|
|
+plt.ylabel("Nombre de PMR")
|
|
|
|
|
+plt.xlabel("Jour de la semaine")
|
|
|
|
|
+plt.xticks(rotation=45)
|
|
|
|
|
+plt.grid(axis='y')
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# Grouper par date uniquement
|
|
|
|
|
+pmr_par_jour_arrivee = df1.groupby(df1['ST arrivée'].dt.date).size()
|
|
|
|
|
+pmr_par_jour_depart = df1.groupby(df1['ST départ'].dt.date).size()
|
|
|
|
|
+
|
|
|
|
|
+# Tracer les courbes
|
|
|
|
|
+plt.figure(figsize=(12, 6))
|
|
|
|
|
+plt.plot(pmr_par_jour_arrivee.index, pmr_par_jour_arrivee.values, marker='o', linestyle='-', color='blue', label='Arrivées')
|
|
|
|
|
+plt.plot(pmr_par_jour_depart.index, pmr_par_jour_depart.values, marker='o', linestyle='-', color='red', label='Départs')
|
|
|
|
|
+
|
|
|
|
|
+plt.title("Évolution du nombre de PMR par jour (Arrivées vs Départs)")
|
|
|
|
|
+plt.xlabel("Date")
|
|
|
|
|
+plt.ylabel("Nombre de PMR")
|
|
|
|
|
+plt.legend()
|
|
|
|
|
+plt.grid(True)
|
|
|
|
|
+
|
|
|
|
|
+# Rendre l'axe X plus clair avec format date
|
|
|
|
|
+ax = plt.gca()
|
|
|
|
|
+ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
|
|
|
|
|
+plt.xticks(rotation=45)
|
|
|
|
|
+
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+df1['ST arrivée arrondie'] = df1['ST arrivée'].dt.floor('25min')
|
|
|
|
|
+df1['ST départ arrondie'] = df1['ST départ'].dt.floor('25min')
|
|
|
|
|
+
|
|
|
|
|
+pmr_par_25min_arrivee = df1.groupby('ST arrivée arrondie').size()
|
|
|
|
|
+pmr_par_25min_depart = df1.groupby('ST départ arrondie').size()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+plt.figure(figsize=(14, 6))
|
|
|
|
|
+plt.plot(pmr_par_25min_arrivee.index, pmr_par_25min_arrivee.values, label='Arrivées', marker='o')
|
|
|
|
|
+plt.plot(pmr_par_25min_depart.index, pmr_par_25min_depart.values, label='Départs', marker='o')
|
|
|
|
|
+
|
|
|
|
|
+plt.title("Nombre de PMR par tranche de 25 minutes")
|
|
|
|
|
+plt.xlabel("Date et heure")
|
|
|
|
|
+plt.ylabel("Nombre de PMR")
|
|
|
|
|
+
|
|
|
|
|
+ax = plt.gca()
|
|
|
|
|
+ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M'))
|
|
|
|
|
+ax.xaxis.set_major_locator(mdates.HourLocator(interval=1)) # tick every 1 hour
|
|
|
|
|
+
|
|
|
|
|
+plt.xticks(rotation=45)
|
|
|
|
|
+plt.grid(True)
|
|
|
|
|
+plt.legend()
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+df1 = df1.drop(columns=['jour_nom_entree', 'jour_nom_depart', 'minutes_entree' ,'minutes_depart' ,'periode_entree', 'periode_depart','jour_semaine_arrivee','ST arrivée arrondie', 'ST départ arrondie'])
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+df1
|
|
|
|
|
+
|
|
|
|
|
+colonnes_details = [
|
|
|
|
|
+ 'Detail SORTI', 'Detail MEET-RPI', 'Detail ELB', 'Detail MEETR-A',
|
|
|
|
|
+ 'Detail MEETL-A', 'Detail MEET-LIEU', 'Detail MEET-BNQ', 'Detail MEET-ACC',
|
|
|
|
|
+ 'Detail PEMB', 'Detail MEET-EMB', 'Detail MEETR-D', 'Detail MEETL-D', 'Detail MEET-COR'
|
|
|
|
|
+]
|
|
|
|
|
+
|
|
|
|
|
+for col in colonnes_details:
|
|
|
|
|
+ print(f"\n--- {col} ---")
|
|
|
|
|
+ print(df1[col].value_counts(dropna=False)) # dropna=False pour aussi voir les NaN
|
|
|
|
|
+ print(df1[col].value_counts(dropna=False, normalize=True).round(3) * 100) # pourcentage
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# nouvelle colonne binaire : 1 si "NO SHOW", 0 sinon
|
|
|
|
|
+df1['no_show'] = df1[['Detail SORTI', 'Detail MEETR-A', 'Detail MEETL-A','Detail MEET-ACC']].apply(
|
|
|
|
|
+ lambda row: 'NO SHOW' in row.values, axis=1).astype(int)
|
|
|
|
|
+
|
|
|
|
|
+# compagnie à l'arrivée
|
|
|
|
|
+no_show_by_company = df1.groupby('nom_compagnie_arr')['no_show'].agg(['count', 'sum'])
|
|
|
|
|
+no_show_by_company['taux_no_show'] = no_show_by_company['sum'] / no_show_by_company['count']
|
|
|
|
|
+no_show_by_company = no_show_by_company.sort_values('taux_no_show', ascending=False)
|
|
|
|
|
+
|
|
|
|
|
+plt.figure(figsize=(12, 6))
|
|
|
|
|
+plt.bar(no_show_by_company.index[:20], no_show_by_company['taux_no_show'][:20], color='tomato')
|
|
|
|
|
+plt.xticks(rotation=90)
|
|
|
|
|
+plt.title("Taux de NO SHOW par compagnie (Top 20)")
|
|
|
|
|
+plt.ylabel("Taux de NO SHOW")
|
|
|
|
|
+plt.xlabel("Compagnie aérienne")
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# Table de contingence entre compagnie et no_show
|
|
|
|
|
+contingency_table = pd.crosstab(df1['nom_compagnie_arr'], df1['no_show'])
|
|
|
|
|
+chi2, p, dof, expected = chi2_contingency(contingency_table)
|
|
|
|
|
+print(f"Chi² statistic : {chi2:.2f}")
|
|
|
|
|
+print(f"Degrees of freedom : {dof}")
|
|
|
|
|
+print(f"P-value : {p:.5f}")
|
|
|
|
|
+
|
|
|
|
|
+if p < 0.05:
|
|
|
|
|
+ print("✅ Il existe une relation significative entre la compagnie aérienne et le NO SHOW (on rejette H0).")
|
|
|
|
|
+else:
|
|
|
|
|
+ print("❌ Aucune relation significative détectée (on ne peut pas rejeter H0).")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# nouvelle colonne binaire : 1 si "NO SHOW" dans l'une des colonnes sélectionnées, 0 sinon
|
|
|
|
|
+df1['no_show'] = df1[['Detail SORTI', 'Detail MEETR-A', 'Detail MEETL-A', 'Detail MEET-ACC']].apply(
|
|
|
|
|
+ lambda row: 'NO SHOW' in row.values, axis=1).astype(int)
|
|
|
|
|
+
|
|
|
|
|
+# compagnie à la destination
|
|
|
|
|
+no_show_by_company_dest = df1.groupby('nom_compagnie_dest')['no_show'].agg(['count', 'sum'])
|
|
|
|
|
+no_show_by_company_dest['taux_no_show'] = no_show_by_company_dest['sum'] / no_show_by_company_dest['count']
|
|
|
|
|
+no_show_by_company_dest = no_show_by_company_dest.sort_values('taux_no_show', ascending=False)
|
|
|
|
|
+
|
|
|
|
|
+plt.figure(figsize=(12, 6))
|
|
|
|
|
+plt.bar(no_show_by_company_dest.index[:20], no_show_by_company_dest['taux_no_show'][:20], color='tomato')
|
|
|
|
|
+plt.xticks(rotation=90)
|
|
|
|
|
+plt.title("Taux de NO SHOW par compagnie à la destination (Top 20)")
|
|
|
|
|
+plt.ylabel("Taux de NO SHOW")
|
|
|
|
|
+plt.xlabel("Compagnie aérienne")
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+signal_counts = df['Statut signalement arrivée'].value_counts()
|
|
|
|
|
+
|
|
|
|
|
+plt.figure(figsize=(10,6))
|
|
|
|
|
+ax = signal_counts.plot(kind='bar', color='skyblue')
|
|
|
|
|
+plt.title("Répartition des demandes d'assistance par délai de signalement")
|
|
|
|
|
+plt.ylabel("Nombre de demandes")
|
|
|
|
|
+plt.xlabel("Statut signalement arrivée")
|
|
|
|
|
+plt.xticks(rotation=45, ha='right')
|
|
|
|
|
+
|
|
|
|
|
+# Ajouter les nombres au-dessus des barres
|
|
|
|
|
+for p in ax.patches:
|
|
|
|
|
+ height = p.get_height()
|
|
|
|
|
+ ax.text(p.get_x() + p.get_width() / 2, height + 5, # 5 = décalage vertical au-dessus
|
|
|
|
|
+ f'{int(height)}', ha='center')
|
|
|
|
|
+
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+df1['Statut signalement arrivée'] = df['Statut signalement arrivée'].fillna('Non précisé')
|
|
|
|
|
+
|
|
|
|
|
+# Grouper par signalement et calculer le taux moyen de no-show
|
|
|
|
|
+no_show_by_signal = df1.groupby('Statut signalement arrivée')['no_show'].mean().sort_values()
|
|
|
|
|
+
|
|
|
|
|
+plt.figure(figsize=(10, 6))
|
|
|
|
|
+no_show_by_signal.plot(kind='bar', color='salmon')
|
|
|
|
|
+plt.ylabel("Taux de no-show")
|
|
|
|
|
+plt.title("Taux de no-show selon le moment du signalement de l’assistance")
|
|
|
|
|
+plt.xticks(rotation=45)
|
|
|
|
|
+plt.grid(axis='y')
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+print(df1['Statut signalement arrivée'].unique())
|
|
|
|
|
+
|
|
|
|
|
+# Nettoyage des valeurs
|
|
|
|
|
+df1['Statut signalement arrivée'] = df1['Statut signalement arrivée'].replace({
|
|
|
|
|
+ '<1h': '<6h',
|
|
|
|
|
+ '<2h': '<6h',
|
|
|
|
|
+ '<6h': '<6h',
|
|
|
|
|
+ 'N.A': 'Non précisé',
|
|
|
|
|
+ np.nan: 'Non précisé'
|
|
|
|
|
+})
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+from pandas.api.types import CategoricalDtype
|
|
|
|
|
+
|
|
|
|
|
+ordre = CategoricalDtype(
|
|
|
|
|
+ categories=['<6h', '<12h', '<24h', '<36h', '>36h'],
|
|
|
|
|
+ ordered=True
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+df1['Statut signalement arrivée'] = df1['Statut signalement arrivée'].astype(ordre)
|
|
|
|
|
+df1['Signalement_encoded'] = df1['Statut signalement arrivée'].cat.codes
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+df1
|
|
|
|
|
+
|
|
|
|
|
+# Trier par nombre de passagers
|
|
|
|
|
+passagers_par_vol_date = df1.groupby(['Code Cie arrivée', 'ST arrivée']).size().reset_index(name='nb_passagers')
|
|
|
|
|
+top_vols = passagers_par_vol_date.sort_values(by='nb_passagers', ascending=False).head(10)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+plt.figure(figsize=(12, 6))
|
|
|
|
|
+bars = plt.barh(
|
|
|
|
|
+ top_vols['Code Cie arrivée'] + ' - ' + top_vols['ST arrivée'].astype(str),
|
|
|
|
|
+ top_vols['nb_passagers'],
|
|
|
|
|
+ color='skyblue'
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+plt.xlabel("Nombre de passagers")
|
|
|
|
|
+plt.title("Top 10 des vols avec le plus de passagers")
|
|
|
|
|
+
|
|
|
|
|
+for bar in bars:
|
|
|
|
|
+ width = bar.get_width()
|
|
|
|
|
+ plt.text(width + 1, bar.get_y() + bar.get_height()/2, str(int(width)), va='center')
|
|
|
|
|
+
|
|
|
|
|
+plt.gca().invert_yaxis()
|
|
|
|
|
+plt.tight_layout()
|
|
|
|
|
+plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+df1['ST arrivée'] = pd.to_datetime(df1['ST arrivée'], errors='coerce')
|
|
|
|
|
+
|
|
|
|
|
+df1['tranche_25min_arr'] = df1['ST arrivée'].dt.floor('25min')
|
|
|
|
|
+
|
|
|
|
|
+pmr_arrivee_25min = df1.groupby(['tranche_25min_arr']).size().reset_index(name='pmr_total')
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+df1['ST départ'] = pd.to_datetime(df1['ST départ'], errors='coerce')
|
|
|
|
|
+
|
|
|
|
|
+# Arrondir à la tranche de 25 minutes
|
|
|
|
|
+df1['tranche_25min_depart'] = df1['ST départ'].dt.floor('25min')
|
|
|
|
|
+
|
|
|
|
|
+# Grouper par tranche et compter les PMR
|
|
|
|
|
+pmr_depart_25min = df1.groupby(['tranche_25min_depart']).size().reset_index(name='pmr_total')
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+pmr_arrivee_25min = pmr_arrivee_25min.rename(columns={'tranche_25min_arr': 'tranche_25min'})
|
|
|
|
|
+pmr_depart_25min = pmr_depart_25min.rename(columns={'tranche_25min_depart': 'tranche_25min'})
|
|
|
|
|
+
|
|
|
|
|
+pmr_merged = pd.merge(
|
|
|
|
|
+ pmr_arrivee_25min,
|
|
|
|
|
+ pmr_depart_25min,
|
|
|
|
|
+ on='tranche_25min',
|
|
|
|
|
+ how='outer',
|
|
|
|
|
+ suffixes=('_arrivee', '_depart')
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+# Remplacer les NaN par 0
|
|
|
|
|
+pmr_merged['pmr_total_arrivee'] = pmr_merged['pmr_total_arrivee'].fillna(0).astype(int)
|
|
|
|
|
+pmr_merged['pmr_total_depart'] = pmr_merged['pmr_total_depart'].fillna(0).astype(int)
|
|
|
|
|
+
|
|
|
|
|
+pmr_merged['total'] = pmr_merged['pmr_total_arrivee'] + pmr_merged['pmr_total_depart']
|
|
|
|
|
+
|
|
|
|
|
+pmr_merged = pmr_merged.sort_values('tranche_25min').reset_index(drop=True)
|
|
|
|
|
+
|
|
|
|
|
+pmr_merged[['tranche_25min', 'pmr_total_arrivee', 'pmr_total_depart', 'total']]
|
|
|
|
|
+pmr_merged = pmr_merged.rename(columns={'total': 'pmr_total'})
|
|
|
|
|
+pmr_merged
|
|
|
|
|
+
|
|
|
|
|
+#df1=df1[['org_dest', 'ST arrivée', 'ST départ','mois_entree' ,'mois_depart','periode_entree_sin', 'periode_entree_cos' ,'periode_depart_sin', 'periode_depart_cos','no_show','compagnie_arr_encoded','compagnie_dest_encoded']]
|
|
|
|
|
+df1
|
|
|
|
|
+
|
|
|
|
|
+df1['jour_semaine_entree'] = df1['ST arrivée'].dt.weekday
|
|
|
|
|
+df1['jour_semaine_entree_sin'] = np.sin(2 * np.pi * df1['jour_semaine_entree'] / 7)
|
|
|
|
|
+df1['jour_semaine_entree_cos'] = np.cos(2 * np.pi * df1['jour_semaine_entree'] / 7)
|
|
|
|
|
+
|
|
|
|
|
+df1['jour_semaine_depart'] = df1['ST départ'].dt.weekday
|
|
|
|
|
+df1['jour_semaine_depart_sin'] = np.sin(2 * np.pi * df1['jour_semaine_depart'] / 7)
|
|
|
|
|
+df1['jour_semaine_depart_cos'] = np.cos(2 * np.pi * df1['jour_semaine_depart'] / 7)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+print(df1[pd.to_numeric(df1['mois_entree'], errors='coerce').isna()]['mois_entree'].unique())
|
|
|
|
|
+print(df1[pd.to_numeric(df1['mois_depart'], errors='coerce').isna()]['mois_depart'].unique())
|
|
|
|
|
+
|
|
|
|
|
+mois_map = {
|
|
|
|
|
+ 'janvier': 1, 'février': 2, 'mars': 3, 'avril': 4,
|
|
|
|
|
+ 'mai': 5, 'juin': 6, 'juillet': 7, 'août': 8,
|
|
|
|
|
+ 'septembre': 9, 'octobre': 10, 'novembre': 11, 'décembre': 12
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+df1['mois_entree'] = df1['mois_entree'].str.lower().str.strip().map(mois_map)
|
|
|
|
|
+df1['mois_depart'] = df1['mois_depart'].str.lower().str.strip().map(mois_map)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+df1
|
|
|
|
|
+
|
|
|
|
|
+df1['mois_entree_sin'] = np.sin(2 * np.pi * df1['mois_entree'] / 12)
|
|
|
|
|
+df1['mois_entree_cos'] = np.cos(2 * np.pi * df1['mois_entree'] / 12)
|
|
|
|
|
+
|
|
|
|
|
+df1['mois_depart_sin'] = np.sin(2 * np.pi * df1['mois_depart'] / 12)
|
|
|
|
|
+df1['mois_depart_cos'] = np.cos(2 * np.pi * df1['mois_depart'] / 12)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+#df1 = df1.drop(columns=['mois_entree', 'mois_depart' ,'ST arrivée','ST départ','mois_entree','mois_depart','jour_semaine_entree', 'jour_semaine_depart','mois_entree_sin', 'mois_entree_cos' ,'mois_depart_sin' ,'mois_depart_cos'], errors='ignore')
|
|
|
|
|
+df1
|
|
|
|
|
+
|
|
|
|
|
+df1['tranche_arrivee'] = df1['ST arrivée'].dt.floor('25T')
|
|
|
|
|
+df1['tranche_depart'] = df1['ST départ'].dt.floor('25T')
|
|
|
|
|
+
|
|
|
|
|
+# Nombre de vols uniques par compagnie à l’arrivée par tranche
|
|
|
|
|
+arrivee_grouped = (
|
|
|
|
|
+ df1.groupby(['tranche_arrivee', 'compagnie_arr_simplifiée'])['Code Cie arrivée']
|
|
|
|
|
+ .nunique()
|
|
|
|
|
+ .unstack(fill_value=0)
|
|
|
|
|
+ .reset_index()
|
|
|
|
|
+)
|
|
|
|
|
+arrivee_grouped.rename(columns={'tranche_arrivee': 'tranche_25min'}, inplace=True)
|
|
|
|
|
+arrivee_grouped.columns = ['tranche_25min'] + [f"arrivee_{col}" for col in arrivee_grouped.columns[1:]]
|
|
|
|
|
+
|
|
|
|
|
+# Nombre de vols uniques par compagnie au départ par tranche
|
|
|
|
|
+depart_grouped = (
|
|
|
|
|
+ df1.groupby(['tranche_depart', 'compagnie_dest_simplifiée'])['Code Cie départ']
|
|
|
|
|
+ .nunique()
|
|
|
|
|
+ .unstack(fill_value=0)
|
|
|
|
|
+ .reset_index()
|
|
|
|
|
+)
|
|
|
|
|
+depart_grouped.rename(columns={'tranche_depart': 'tranche_25min'}, inplace=True)
|
|
|
|
|
+depart_grouped.columns = ['tranche_25min'] + [f"depart_{col}" for col in depart_grouped.columns[1:]]
|
|
|
|
|
+
|
|
|
|
|
+pmr_merged['tranche_25min'] = pd.to_datetime(pmr_merged['tranche_25min'])
|
|
|
|
|
+
|
|
|
|
|
+# Fusion avec les données arrivées et départs
|
|
|
|
|
+df_final = pmr_merged.merge(arrivee_grouped, on='tranche_25min', how='left')
|
|
|
|
|
+df_final = df_final.merge(depart_grouped, on='tranche_25min', how='left')
|
|
|
|
|
+df_final.fillna(0, inplace=True)
|
|
|
|
|
+
|
|
|
|
|
+# Nombre total de vols uniques (arrivée + départ) par tranche
|
|
|
|
|
+arrivee_total = (
|
|
|
|
|
+ df1.groupby('tranche_arrivee')['Code Cie arrivée']
|
|
|
|
|
+ .nunique()
|
|
|
|
|
+ .reset_index(name='nb_vols_arrivee')
|
|
|
|
|
+ .rename(columns={'tranche_arrivee': 'tranche_25min'})
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+depart_total = (
|
|
|
|
|
+ df1.groupby('tranche_depart')['Code Cie départ']
|
|
|
|
|
+ .nunique()
|
|
|
|
|
+ .reset_index(name='nb_vols_depart')
|
|
|
|
|
+ .rename(columns={'tranche_depart': 'tranche_25min'})
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+vols_total = pd.merge(arrivee_total, depart_total, on='tranche_25min', how='outer').fillna(0)
|
|
|
|
|
+vols_total['nb_vols_total'] = vols_total['nb_vols_arrivee'] + vols_total['nb_vols_depart']
|
|
|
|
|
+
|
|
|
|
|
+# Fusion avec le total des vols
|
|
|
|
|
+df_final = df_final.merge(vols_total[['tranche_25min', 'nb_vols_total']], on='tranche_25min', how='left')
|
|
|
|
|
+df_final['nb_vols_total'] = df_final['nb_vols_total'].fillna(0).astype(int)
|
|
|
|
|
+
|
|
|
|
|
+df_final['jourSemaine'] = df_final['tranche_25min'].dt.dayofweek
|
|
|
|
|
+df_final['is_weekend'] = df_final['jourSemaine'].isin([5,6]).astype(int)
|
|
|
|
|
+df_final = df_final.drop(columns=['pmr_total_arrivee', 'pmr_total_depart'], errors='ignore')
|
|
|
|
|
+
|
|
|
|
|
+cols = df_final.columns.tolist()
|
|
|
|
|
+for col in ['tranche_25min', 'nb_vols_total', 'pmr_total', 'jourSemaine', 'is_weekend']:
|
|
|
|
|
+ if col in cols:
|
|
|
|
|
+ cols.remove(col)
|
|
|
|
|
+
|
|
|
|
|
+new_cols_order = ['tranche_25min', 'jourSemaine', 'is_weekend', 'nb_vols_total'] + cols + ['pmr_total']
|
|
|
|
|
+
|
|
|
|
|
+df_final = df_final[new_cols_order]
|
|
|
|
|
+
|
|
|
|
|
+df_final.head(10)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+df1['tranche_arrivee'] = df1['ST arrivée'].dt.floor('25T')
|
|
|
|
|
+df1['tranche_depart'] = df1['ST départ'].dt.floor('25T')
|
|
|
|
|
+
|
|
|
|
|
+# Nombre de vols uniques par compagnie à l’arrivée par tranche
|
|
|
|
|
+arrivee_grouped = (
|
|
|
|
|
+ df1.groupby(['tranche_arrivee', 'compagnie_arr_simplifiée'])['Num Vol arrivée']
|
|
|
|
|
+ .nunique()
|
|
|
|
|
+ .unstack(fill_value=0)
|
|
|
|
|
+ .reset_index()
|
|
|
|
|
+)
|
|
|
|
|
+arrivee_grouped.rename(columns={'tranche_arrivee': 'tranche_25min'}, inplace=True)
|
|
|
|
|
+arrivee_grouped.columns = ['tranche_25min'] + [f"arrivee_{col}" for col in arrivee_grouped.columns[1:]]
|
|
|
|
|
+
|
|
|
|
|
+# Nombre de vols uniques par compagnie au départ par tranche
|
|
|
|
|
+depart_grouped = (
|
|
|
|
|
+ df1.groupby(['tranche_depart', 'compagnie_dest_simplifiée'])['Num Vol départ']
|
|
|
|
|
+ .nunique()
|
|
|
|
|
+ .unstack(fill_value=0)
|
|
|
|
|
+ .reset_index()
|
|
|
|
|
+)
|
|
|
|
|
+depart_grouped.rename(columns={'tranche_depart': 'tranche_25min'}, inplace=True)
|
|
|
|
|
+depart_grouped.columns = ['tranche_25min'] + [f"depart_{col}" for col in depart_grouped.columns[1:]]
|
|
|
|
|
+
|
|
|
|
|
+# PMR à l’arrivée par tranche et compagnie (nombre d'occurrences)
|
|
|
|
|
+pmr_arrivee = (
|
|
|
|
|
+ df1.groupby(['tranche_arrivee', 'compagnie_arr_simplifiée'])
|
|
|
|
|
+ .size()
|
|
|
|
|
+ .unstack(fill_value=0)
|
|
|
|
|
+ .reset_index()
|
|
|
|
|
+)
|
|
|
|
|
+pmr_arrivee.rename(columns={'tranche_arrivee': 'tranche_25min'}, inplace=True)
|
|
|
|
|
+pmr_arrivee.columns = ['tranche_25min'] + [f"pmr_arrivee_{col}" for col in pmr_arrivee.columns[1:]]
|
|
|
|
|
+
|
|
|
|
|
+# PMR au départ par tranche et compagnie
|
|
|
|
|
+pmr_depart = (
|
|
|
|
|
+ df1.groupby(['tranche_depart', 'compagnie_dest_simplifiée'])
|
|
|
|
|
+ .size()
|
|
|
|
|
+ .unstack(fill_value=0)
|
|
|
|
|
+ .reset_index()
|
|
|
|
|
+)
|
|
|
|
|
+pmr_depart.rename(columns={'tranche_depart': 'tranche_25min'}, inplace=True)
|
|
|
|
|
+pmr_depart.columns = ['tranche_25min'] + [f"pmr_depart_{col}" for col in pmr_depart.columns[1:]]
|
|
|
|
|
+
|
|
|
|
|
+pmr_merged = pd.merge(pmr_arrivee, pmr_depart, on='tranche_25min', how='outer').fillna(0)
|
|
|
|
|
+pmr_merged['tranche_25min'] = pd.to_datetime(pmr_merged['tranche_25min'])
|
|
|
|
|
+
|
|
|
|
|
+df_final = pmr_merged.merge(arrivee_grouped, on='tranche_25min', how='left')
|
|
|
|
|
+df_final = df_final.merge(depart_grouped, on='tranche_25min', how='left')
|
|
|
|
|
+df_final.fillna(0, inplace=True)
|
|
|
|
|
+
|
|
|
|
|
+arrivee_total = (
|
|
|
|
|
+ df1.groupby('tranche_arrivee')['Num Vol arrivée']
|
|
|
|
|
+ .nunique()
|
|
|
|
|
+ .reset_index(name='nb_vols_arrivee')
|
|
|
|
|
+ .rename(columns={'tranche_arrivee': 'tranche_25min'})
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+depart_total = (
|
|
|
|
|
+ df1.groupby('tranche_depart')['Num Vol départ']
|
|
|
|
|
+ .nunique()
|
|
|
|
|
+ .reset_index(name='nb_vols_depart')
|
|
|
|
|
+ .rename(columns={'tranche_depart': 'tranche_25min'})
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+vols_total = pd.merge(arrivee_total, depart_total, on='tranche_25min', how='outer').fillna(0)
|
|
|
|
|
+vols_total['nb_vols_total'] = vols_total['nb_vols_arrivee'] + vols_total['nb_vols_depart']
|
|
|
|
|
+
|
|
|
|
|
+df_final = df_final.merge(vols_total[['tranche_25min', 'nb_vols_total']], on='tranche_25min', how='left')
|
|
|
|
|
+df_final['nb_vols_total'] = df_final['nb_vols_total'].fillna(0).astype(int)
|
|
|
|
|
+
|
|
|
|
|
+pmr_cols = [col for col in df_final.columns if col.startswith('pmr_arrivee_') or col.startswith('pmr_depart_')]
|
|
|
|
|
+df_final['pmr_total'] = df_final[pmr_cols].sum(axis=1)
|
|
|
|
|
+
|
|
|
|
|
+df_final['jourSemaine'] = df_final['tranche_25min'].dt.dayofweek
|
|
|
|
|
+
|
|
|
|
|
+df_final = df_final.drop(columns=['pmr_total_arrivee', 'pmr_total_depart'], errors='ignore')
|
|
|
|
|
+
|
|
|
|
|
+cols = df_final.columns.tolist()
|
|
|
|
|
+for col in ['tranche_25min', 'nb_vols_total', 'pmr_total', 'jourSemaine']:
|
|
|
|
|
+ if col in cols:
|
|
|
|
|
+ cols.remove(col)
|
|
|
|
|
+
|
|
|
|
|
+new_cols_order = ['tranche_25min', 'jourSemaine', 'nb_vols_total'] + cols + ['pmr_total']
|
|
|
|
|
+df_final = df_final[new_cols_order]
|
|
|
|
|
+
|
|
|
|
|
+df_final.head(10)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+import requests
|
|
|
|
|
+import pandas as pd
|
|
|
|
|
+
|
|
|
|
|
+API_KEY = 'NNPh5mZBBbBTNm6qnvvzI56gY1P339NZ'
|
|
|
|
|
+BASE_URL = 'https://calendarific.com/api/v2/holidays'
|
|
|
|
|
+
|
|
|
|
|
+def get_holidays_for_year_with_names(year, country='FR'):
|
|
|
|
|
+ params = {
|
|
|
|
|
+ 'api_key': API_KEY,
|
|
|
|
|
+ 'country': country,
|
|
|
|
|
+ 'year': year
|
|
|
|
|
+ }
|
|
|
|
|
+ response = requests.get(BASE_URL, params=params)
|
|
|
|
|
+ data = response.json()
|
|
|
|
|
+ holidays = {}
|
|
|
|
|
+ if data['meta']['code'] == 200:
|
|
|
|
|
+ for holiday in data['response']['holidays']:
|
|
|
|
|
+ date_iso = holiday['date']['iso'] # format yyyy-mm-dd
|
|
|
|
|
+ name = holiday['name']
|
|
|
|
|
+ holidays[date_iso] = name
|
|
|
|
|
+ else:
|
|
|
|
|
+ print(f"Erreur API pour l'année {year}:", data['meta'].get('error_detail', 'Erreur inconnue'))
|
|
|
|
|
+ return holidays
|
|
|
|
|
+
|
|
|
|
|
+# Préparation des tranches 25 minutes dans df1 (exemple avec tes colonnes)
|
|
|
|
|
+df1['tranche_arrivee'] = df1['ST arrivée'].dt.floor('25T')
|
|
|
|
|
+df1['tranche_depart'] = df1['ST départ'].dt.floor('25T')
|
|
|
|
|
+
|
|
|
|
|
+# Nombre de vols uniques par compagnie à l’arrivée par tranche
|
|
|
|
|
+arrivee_grouped = (
|
|
|
|
|
+ df1.groupby(['tranche_arrivee', 'compagnie_arr_simplifiée'])['Num Vol arrivée']
|
|
|
|
|
+ .nunique()
|
|
|
|
|
+ .unstack(fill_value=0)
|
|
|
|
|
+ .reset_index()
|
|
|
|
|
+)
|
|
|
|
|
+arrivee_grouped.rename(columns={'tranche_arrivee': 'tranche_25min'}, inplace=True)
|
|
|
|
|
+arrivee_grouped.columns = ['tranche_25min'] + [f"arrivee_{col}" for col in arrivee_grouped.columns[1:]]
|
|
|
|
|
+
|
|
|
|
|
+# Nombre de vols uniques par compagnie au départ par tranche
|
|
|
|
|
+depart_grouped = (
|
|
|
|
|
+ df1.groupby(['tranche_depart', 'compagnie_dest_simplifiée'])['Num Vol départ']
|
|
|
|
|
+ .nunique()
|
|
|
|
|
+ .unstack(fill_value=0)
|
|
|
|
|
+ .reset_index()
|
|
|
|
|
+)
|
|
|
|
|
+depart_grouped.rename(columns={'tranche_depart': 'tranche_25min'}, inplace=True)
|
|
|
|
|
+depart_grouped.columns = ['tranche_25min'] + [f"depart_{col}" for col in depart_grouped.columns[1:]]
|
|
|
|
|
+
|
|
|
|
|
+# PMR à l’arrivée par tranche et compagnie
|
|
|
|
|
+pmr_arrivee = (
|
|
|
|
|
+ df1.groupby(['tranche_arrivee', 'compagnie_arr_simplifiée'])
|
|
|
|
|
+ .size()
|
|
|
|
|
+ .unstack(fill_value=0)
|
|
|
|
|
+ .reset_index()
|
|
|
|
|
+)
|
|
|
|
|
+pmr_arrivee.rename(columns={'tranche_arrivee': 'tranche_25min'}, inplace=True)
|
|
|
|
|
+pmr_arrivee.columns = ['tranche_25min'] + [f"pmr_arrivee_{col}" for col in pmr_arrivee.columns[1:]]
|
|
|
|
|
+
|
|
|
|
|
+# PMR au départ par tranche et compagnie
|
|
|
|
|
+pmr_depart = (
|
|
|
|
|
+ df1.groupby(['tranche_depart', 'compagnie_dest_simplifiée'])
|
|
|
|
|
+ .size()
|
|
|
|
|
+ .unstack(fill_value=0)
|
|
|
|
|
+ .reset_index()
|
|
|
|
|
+)
|
|
|
|
|
+pmr_depart.rename(columns={'tranche_depart': 'tranche_25min'}, inplace=True)
|
|
|
|
|
+pmr_depart.columns = ['tranche_25min'] + [f"pmr_depart_{col}" for col in pmr_depart.columns[1:]]
|
|
|
|
|
+
|
|
|
|
|
+# Fusion des PMR arrivee et depart
|
|
|
|
|
+pmr_merged = pd.merge(pmr_arrivee, pmr_depart, on='tranche_25min', how='outer').fillna(0)
|
|
|
|
|
+pmr_merged['tranche_25min'] = pd.to_datetime(pmr_merged['tranche_25min'])
|
|
|
|
|
+
|
|
|
|
|
+# Fusion des données vols et pmr
|
|
|
|
|
+df_final = pmr_merged.merge(arrivee_grouped, on='tranche_25min', how='left')
|
|
|
|
|
+df_final = df_final.merge(depart_grouped, on='tranche_25min', how='left')
|
|
|
|
|
+df_final.fillna(0, inplace=True)
|
|
|
|
|
+
|
|
|
|
|
+# Calcul du nombre total de vols par tranche
|
|
|
|
|
+arrivee_total = (
|
|
|
|
|
+ df1.groupby('tranche_arrivee')['Num Vol arrivée']
|
|
|
|
|
+ .nunique()
|
|
|
|
|
+ .reset_index(name='nb_vols_arrivee')
|
|
|
|
|
+ .rename(columns={'tranche_arrivee': 'tranche_25min'})
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+depart_total = (
|
|
|
|
|
+ df1.groupby('tranche_depart')['Num Vol départ']
|
|
|
|
|
+ .nunique()
|
|
|
|
|
+ .reset_index(name='nb_vols_depart')
|
|
|
|
|
+ .rename(columns={'tranche_depart': 'tranche_25min'})
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+vols_total = pd.merge(arrivee_total, depart_total, on='tranche_25min', how='outer').fillna(0)
|
|
|
|
|
+vols_total['nb_vols_total'] = vols_total['nb_vols_arrivee'] + vols_total['nb_vols_depart']
|
|
|
|
|
+
|
|
|
|
|
+df_final = df_final.merge(vols_total[['tranche_25min', 'nb_vols_total']], on='tranche_25min', how='left')
|
|
|
|
|
+df_final['nb_vols_total'] = df_final['nb_vols_total'].fillna(0).astype(int)
|
|
|
|
|
+
|
|
|
|
|
+pmr_cols = [col for col in df_final.columns if col.startswith('pmr_arrivee_') or col.startswith('pmr_depart_')]
|
|
|
|
|
+df_final['pmr_total'] = df_final[pmr_cols].sum(axis=1)
|
|
|
|
|
+
|
|
|
|
|
+df_final['jourSemaine'] = df_final['tranche_25min'].dt.dayofweek
|
|
|
|
|
+
|
|
|
|
|
+cols = df_final.columns.tolist()
|
|
|
|
|
+for col in ['tranche_25min', 'nb_vols_total', 'pmr_total', 'jourSemaine']:
|
|
|
|
|
+ if col in cols:
|
|
|
|
|
+ cols.remove(col)
|
|
|
|
|
+new_cols_order = ['tranche_25min', 'jourSemaine', 'nb_vols_total'] + cols + ['pmr_total']
|
|
|
|
|
+df_final = df_final[new_cols_order]
|
|
|
|
|
+
|
|
|
|
|
+# holiday_name
|
|
|
|
|
+
|
|
|
|
|
+years = df_final['tranche_25min'].dt.year.unique()
|
|
|
|
|
+
|
|
|
|
|
+all_holidays = {}
|
|
|
|
|
+for year in years:
|
|
|
|
|
+ holidays_for_year = get_holidays_for_year_with_names(year, country='FR')
|
|
|
|
|
+ all_holidays.update(holidays_for_year)
|
|
|
|
|
+
|
|
|
|
|
+df_final['date_str'] = df_final['tranche_25min'].dt.strftime('%Y-%m-%d')
|
|
|
|
|
+
|
|
|
|
|
+df_final['holiday_name'] = df_final['date_str'].map(all_holidays).fillna('')
|
|
|
|
|
+
|
|
|
|
|
+df_final.drop(columns=['date_str'], inplace=True)
|
|
|
|
|
+
|
|
|
|
|
+df_final.head(10)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+df_final
|
|
|
|
|
+
|
|
|
|
|
+df_final['minutes'] = df_final['tranche_25min'].dt.hour * 60 + df_final['tranche_25min'].dt.minute
|
|
|
|
|
+df_final['tranche_num'] = df_final['minutes'] // 25
|
|
|
|
|
+max_tranche = 24 * 60 // 25
|
|
|
|
|
+
|
|
|
|
|
+df_final['tranche_sin'] = np.sin(2 * np.pi * df_final['tranche_num'] / max_tranche)
|
|
|
|
|
+df_final['tranche_cos'] = np.cos(2 * np.pi * df_final['tranche_num'] / max_tranche)
|
|
|
|
|
+
|
|
|
|
|
+max_jour = 7
|
|
|
|
|
+df_final['jour_sin'] = np.sin(2 * np.pi * df_final['jourSemaine'] / max_jour)
|
|
|
|
|
+df_final['jour_cos'] = np.cos(2 * np.pi * df_final['jourSemaine'] / max_jour)
|
|
|
|
|
+
|
|
|
|
|
+# Supprime les colonnes originales si nécessaire
|
|
|
|
|
+df_final = df_final.drop(columns=['tranche_25min', 'jourSemaine'])
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+df_final
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+X = df_final.drop(columns=['pmr_total'])
|
|
|
|
|
+y = df_final['pmr_total']
|
|
|
|
|
+
|
|
|
|
|
+X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
|
|
|
|
+
|
|
|
|
|
+rf = RandomForestRegressor(n_estimators=100, random_state=42)
|
|
|
|
|
+
|
|
|
|
|
+rf.fit(X_train, y_train)
|
|
|
|
|
+
|
|
|
|
|
+y_pred = rf.predict(X_test)
|
|
|
|
|
+
|
|
|
|
|
+mae = mean_absolute_error(y_test, y_pred)
|
|
|
|
|
+rmse = mean_squared_error(y_test, y_pred, squared=False)
|
|
|
|
|
+
|
|
|
|
|
+print(f"MAE: {mae:.2f}")
|
|
|
|
|
+print(f"RMSE: {rmse:.2f}")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ordered_columns = [
|
|
|
|
|
+ "nb_vols_total",
|
|
|
|
|
+ "pmr_arrivee_Aer Lingus (EI)",
|
|
|
|
|
+ "pmr_arrivee_Air Algerie (AH)",
|
|
|
|
|
+ "pmr_arrivee_Air Canada (AC)",
|
|
|
|
|
+ "pmr_arrivee_Air China (CA)",
|
|
|
|
|
+ "pmr_arrivee_Air France (AF)",
|
|
|
|
|
+ "pmr_arrivee_Air India (AI)",
|
|
|
|
|
+ "pmr_arrivee_Air Senegal (HC)",
|
|
|
|
|
+ "pmr_arrivee_Air Tahiti Nui (TN)",
|
|
|
|
|
+ "pmr_arrivee_American Airlines (AA)",
|
|
|
|
|
+ "pmr_arrivee_Asiana Airlines (OZ)",
|
|
|
|
|
+ "pmr_arrivee_Avianca (AV)",
|
|
|
|
|
+ "pmr_arrivee_Brussels Airlines (SN)",
|
|
|
|
|
+ "pmr_arrivee_Cathay Pacific (CX)",
|
|
|
|
|
+ "pmr_arrivee_EL AL (LY)",
|
|
|
|
|
+ "pmr_arrivee_Emirates (EK)",
|
|
|
|
|
+ "pmr_arrivee_Etihad Airways (EY)",
|
|
|
|
|
+ "pmr_arrivee_LATAM Chile (LA)",
|
|
|
|
|
+ "pmr_arrivee_Lufthansa (LH)",
|
|
|
|
|
+ "pmr_arrivee_Norwegian Air Sweden (N0)",
|
|
|
|
|
+ "pmr_arrivee_Nouvelair (BJ)",
|
|
|
|
|
+ "pmr_arrivee_Other",
|
|
|
|
|
+ "pmr_arrivee_PIA (PK)",
|
|
|
|
|
+ "pmr_arrivee_Qatar Airways (QR)",
|
|
|
|
|
+ "pmr_arrivee_SAS Scandinavian (SK)",
|
|
|
|
|
+ "pmr_arrivee_Singapore Airlines (SQ)",
|
|
|
|
|
+ "pmr_arrivee_SunExpress (XQ)",
|
|
|
|
|
+ "pmr_arrivee_Turkish Airlines (TK)",
|
|
|
|
|
+ "pmr_arrivee_United Airlines (UA)",
|
|
|
|
|
+ "pmr_arrivee_Volotea (VF)",
|
|
|
|
|
+ "pmr_arrivee_easyJet (EZY)",
|
|
|
|
|
+ "pmr_arrivee_easyJet Europe (EJU)",
|
|
|
|
|
+ "pmr_depart_Air Algerie (AH)",
|
|
|
|
|
+ "pmr_depart_Air Canada (AC)",
|
|
|
|
|
+ "pmr_depart_Air France (AF)",
|
|
|
|
|
+ "pmr_depart_Air India (AI)",
|
|
|
|
|
+ "pmr_depart_Air Senegal (HC)",
|
|
|
|
|
+ "pmr_depart_Alitalia (AZ)",
|
|
|
|
|
+ "pmr_depart_American Airlines (AA)",
|
|
|
|
|
+ "pmr_depart_Avianca (AV)",
|
|
|
|
|
+ "pmr_depart_British Airways (BA)",
|
|
|
|
|
+ "pmr_depart_Brussels Airlines (SN)",
|
|
|
|
|
+ "pmr_depart_EL AL (LY)",
|
|
|
|
|
+ "pmr_depart_Emirates (EK)",
|
|
|
|
|
+ "pmr_depart_Ethiopian Airlines (ET)",
|
|
|
|
|
+ "pmr_depart_Jet2.com (LS)",
|
|
|
|
|
+ "pmr_depart_Lufthansa (LH)",
|
|
|
|
|
+ "pmr_depart_Nouvelair (BJ)",
|
|
|
|
|
+ "pmr_depart_Other",
|
|
|
|
|
+ "pmr_depart_Qatar Airways (QR)",
|
|
|
|
|
+ "pmr_depart_Royal Air Maroc (AT)",
|
|
|
|
|
+ "pmr_depart_SunExpress (XQ)",
|
|
|
|
|
+ "pmr_depart_Turkish Airlines (TK)",
|
|
|
|
|
+ "pmr_depart_United Airlines (UA)",
|
|
|
|
|
+ "pmr_depart_Volotea (VF)",
|
|
|
|
|
+ "pmr_depart_easyJet (EZY)",
|
|
|
|
|
+ "pmr_depart_easyJet Europe (EJU)",
|
|
|
|
|
+ "arrivee_Aer Lingus (EI)",
|
|
|
|
|
+ "arrivee_Air Algerie (AH)",
|
|
|
|
|
+ "arrivee_Air Canada (AC)",
|
|
|
|
|
+ "arrivee_Air China (CA)",
|
|
|
|
|
+ "arrivee_Air France (AF)",
|
|
|
|
|
+ "arrivee_Air India (AI)",
|
|
|
|
|
+ "arrivee_Air Senegal (HC)",
|
|
|
|
|
+ "arrivee_Air Tahiti Nui (TN)",
|
|
|
|
|
+ "arrivee_American Airlines (AA)",
|
|
|
|
|
+ "arrivee_Asiana Airlines (OZ)",
|
|
|
|
|
+ "arrivee_Avianca (AV)",
|
|
|
|
|
+ "arrivee_Brussels Airlines (SN)",
|
|
|
|
|
+ "arrivee_Cathay Pacific (CX)",
|
|
|
|
|
+ "arrivee_EL AL (LY)",
|
|
|
|
|
+ "arrivee_Emirates (EK)",
|
|
|
|
|
+ "arrivee_Etihad Airways (EY)",
|
|
|
|
|
+ "arrivee_LATAM Chile (LA)",
|
|
|
|
|
+ "arrivee_Lufthansa (LH)",
|
|
|
|
|
+ "arrivee_Norwegian Air Sweden (N0)",
|
|
|
|
|
+ "arrivee_Nouvelair (BJ)",
|
|
|
|
|
+ "arrivee_Other",
|
|
|
|
|
+ "arrivee_PIA (PK)",
|
|
|
|
|
+ "arrivee_Qatar Airways (QR)",
|
|
|
|
|
+ "arrivee_SAS Scandinavian (SK)",
|
|
|
|
|
+ "arrivee_Singapore Airlines (SQ)",
|
|
|
|
|
+ "arrivee_SunExpress (XQ)",
|
|
|
|
|
+ "arrivee_Turkish Airlines (TK)",
|
|
|
|
|
+ "arrivee_United Airlines (UA)",
|
|
|
|
|
+ "arrivee_Volotea (VF)",
|
|
|
|
|
+ "arrivee_easyJet (EZY)",
|
|
|
|
|
+ "arrivee_easyJet Europe (EJU)",
|
|
|
|
|
+ "depart_Air Algerie (AH)",
|
|
|
|
|
+ "depart_Air Canada (AC)",
|
|
|
|
|
+ "depart_Air France (AF)",
|
|
|
|
|
+ "depart_Air India (AI)",
|
|
|
|
|
+ "depart_Air Senegal (HC)",
|
|
|
|
|
+ "depart_Alitalia (AZ)",
|
|
|
|
|
+ "depart_American Airlines (AA)",
|
|
|
|
|
+ "depart_Avianca (AV)",
|
|
|
|
|
+ "depart_British Airways (BA)",
|
|
|
|
|
+ "depart_Brussels Airlines (SN)",
|
|
|
|
|
+ "depart_EL AL (LY)",
|
|
|
|
|
+ "depart_Emirates (EK)",
|
|
|
|
|
+ "depart_Ethiopian Airlines (ET)",
|
|
|
|
|
+ "depart_Jet2.com (LS)",
|
|
|
|
|
+ "depart_Lufthansa (LH)",
|
|
|
|
|
+ "depart_Nouvelair (BJ)",
|
|
|
|
|
+ "depart_Other",
|
|
|
|
|
+ "depart_Qatar Airways (QR)",
|
|
|
|
|
+ "depart_Royal Air Maroc (AT)",
|
|
|
|
|
+ "depart_SunExpress (XQ)",
|
|
|
|
|
+ "depart_Turkish Airlines (TK)",
|
|
|
|
|
+ "depart_United Airlines (UA)",
|
|
|
|
|
+ "depart_Volotea (VF)",
|
|
|
|
|
+ "depart_easyJet (EZY)",
|
|
|
|
|
+ "depart_easyJet Europe (EJU)",
|
|
|
|
|
+ "minutes",
|
|
|
|
|
+ "tranche_num",
|
|
|
|
|
+ "tranche_sin",
|
|
|
|
|
+ "tranche_cos",
|
|
|
|
|
+ "jour_sin",
|
|
|
|
|
+ "jour_cos"
|
|
|
|
|
+]
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def generate_pmr(size):
|
|
|
|
|
+ return np.random.poisson(lam=2, size=size)
|
|
|
|
|
+
|
|
|
|
|
+n_samples = 1000
|
|
|
|
|
+
|
|
|
|
|
+pmr_arrivee_cols = [c for c in ordered_columns if c.startswith("pmr_arrivee_")]
|
|
|
|
|
+pmr_depart_cols = [c for c in ordered_columns if c.startswith("pmr_depart_")]
|
|
|
|
|
+arrivee_cols = [c for c in ordered_columns if c.startswith("arrivee_")]
|
|
|
|
|
+depart_cols = [c for c in ordered_columns if c.startswith("depart_")]
|
|
|
|
|
+
|
|
|
|
|
+data = {}
|
|
|
|
|
+
|
|
|
|
|
+data["nb_vols_total"] = np.random.randint(0, 50, size=n_samples)
|
|
|
|
|
+
|
|
|
|
|
+for col in pmr_arrivee_cols:
|
|
|
|
|
+ data[col] = generate_pmr(n_samples)
|
|
|
|
|
+
|
|
|
|
|
+for col in pmr_depart_cols:
|
|
|
|
|
+ data[col] = generate_pmr(n_samples)
|
|
|
|
|
+
|
|
|
|
|
+for col in arrivee_cols + depart_cols:
|
|
|
|
|
+ # Binary 0/1 for arrival/departure presence flags
|
|
|
|
|
+ data[col] = np.random.choice([0, 1], size=n_samples)
|
|
|
|
|
+
|
|
|
|
|
+data["minutes"] = np.random.randint(0, 1440, size=n_samples)
|
|
|
|
|
+
|
|
|
|
|
+data["tranche_num"] = data["minutes"] // 25
|
|
|
|
|
+
|
|
|
|
|
+data["tranche_sin"] = np.sin(2 * np.pi * data["tranche_num"] / 96)
|
|
|
|
|
+data["tranche_cos"] = np.cos(2 * np.pi * data["tranche_num"] / 96)
|
|
|
|
|
+
|
|
|
|
|
+jour_num = np.random.randint(0, 7, size=n_samples)
|
|
|
|
|
+
|
|
|
|
|
+data["jour_sin"] = np.sin(2 * np.pi * jour_num / 7)
|
|
|
|
|
+data["jour_cos"] = np.cos(2 * np.pi * jour_num / 7)
|
|
|
|
|
+
|
|
|
|
|
+import pandas as pd
|
|
|
|
|
+df = pd.DataFrame(data)
|
|
|
|
|
+
|
|
|
|
|
+df = df[ordered_columns]
|
|
|
|
|
+
|
|
|
|
|
+pmr_pred = rf.predict(df)
|
|
|
|
|
+
|
|
|
|
|
+for tranche, pred in zip(data['tranche_num'], pmr_pred):
|
|
|
|
|
+ start_minute = tranche * 25
|
|
|
|
|
+ end_minute = (tranche + 1) * 25 - 1
|
|
|
|
|
+ start_hour, start_min = divmod(start_minute, 60)
|
|
|
|
|
+ end_hour, end_min = divmod(end_minute, 60)
|
|
|
|
|
+ print(f"Tranche {tranche} ({start_hour:02d}:{start_min:02d} - {end_hour:02d}:{end_min:02d}): Prédiction PMR = {pred:.2f}")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+import requests
|
|
|
|
|
+import pandas as pd
|
|
|
|
|
+import numpy as np
|
|
|
|
|
+
|
|
|
|
|
+API_KEY = "OaVTBoRzf6lvHiOkYr8UFhNg06CGGdB6"
|
|
|
|
|
+
|
|
|
|
|
+# Fonction qui récupère la liste des jours fériés d'une année pour un pays
|
|
|
|
|
+def get_holidays_for_year_country(year, country_code):
|
|
|
|
|
+ url = "https://calendarific.com/api/v2/holidays"
|
|
|
|
|
+ params = {
|
|
|
|
|
+ 'api_key': API_KEY,
|
|
|
|
|
+ 'country': country_code,
|
|
|
|
|
+ 'year': year,
|
|
|
|
|
+ 'type': 'national,religious' # tu peux ajuster le type si besoin
|
|
|
|
|
+ }
|
|
|
|
|
+ response = requests.get(url, params=params)
|
|
|
|
|
+ if response.status_code != 200:
|
|
|
|
|
+ print(f"Erreur API pour {year} - {country_code}: {response.status_code}")
|
|
|
|
|
+ return set()
|
|
|
|
|
+ holidays = response.json().get('response', {}).get('holidays', [])
|
|
|
|
|
+ # Extraire les dates ISO (YYYY-MM-DD) dans un set pour accès rapide
|
|
|
|
|
+ holiday_dates = set(hol['date']['iso'] for hol in holidays)
|
|
|
|
|
+ return holiday_dates
|
|
|
|
|
+
|
|
|
|
|
+df['ST arrivée'] = pd.to_datetime(df['ST arrivée'], errors='coerce')
|
|
|
|
|
+
|
|
|
|
|
+df['year_arrivee'] = df['ST arrivée'].dt.year
|
|
|
|
|
+unique_year_country = df[['year_arrivee', 'code_pays_origine']].dropna().drop_duplicates()
|
|
|
|
|
+
|
|
|
|
|
+# Dictionnaire pour stocker les jours fériés par (année, pays)
|
|
|
|
|
+holidays_map = {}
|
|
|
|
|
+
|
|
|
|
|
+for idx, row in unique_year_country.iterrows():
|
|
|
|
|
+ year = int(row['year_arrivee'])
|
|
|
|
|
+ country = row['code_pays_origine']
|
|
|
|
|
+ holidays_map[(year, country)] = get_holidays_for_year_country(year, country)
|
|
|
|
|
+
|
|
|
|
|
+# Fonction pour vérifier si la date est un jour férié via le mapping
|
|
|
|
|
+def is_holiday_mapped(row):
|
|
|
|
|
+ if pd.isnull(row['ST arrivée']) or pd.isnull(row['code_pays_origine']):
|
|
|
|
|
+ return False
|
|
|
|
|
+ key = (row['ST arrivée'].year, row['code_pays_origine'])
|
|
|
|
|
+ date_str = row['ST arrivée'].strftime('%Y-%m-%d')
|
|
|
|
|
+ return date_str in holidays_map.get(key, set())
|
|
|
|
|
+
|
|
|
|
|
+df['jour_ferie_arrivee'] = df.apply(is_holiday_mapped, axis=1)
|
|
|
|
|
+
|
|
|
|
|
+df.drop(columns=['year_arrivee'], inplace=True)
|
|
|
|
|
+
|
|
|
|
|
+def encode_cyclique_time(dt_series):
|
|
|
|
|
+ minutes = dt_series.dt.hour.fillna(0) * 60 + dt_series.dt.minute.fillna(0)
|
|
|
|
|
+ minutes_in_day = 24 * 60
|
|
|
|
|
+ sin_time = np.sin(2 * np.pi * minutes / minutes_in_day)
|
|
|
|
|
+ cos_time = np.cos(2 * np.pi * minutes / minutes_in_day)
|
|
|
|
|
+ return sin_time, cos_time
|
|
|
|
|
+
|
|
|
|
|
+df['sin_ST_arrivee'], df['cos_ST_arrivee'] = encode_cyclique_time(df['ST arrivée'])
|
|
|
|
|
+df['jour_sem_arrivee'] = df['ST arrivée'].dt.dayofweek
|
|
|
|
|
+
|
|
|
|
|
+features_pmr = [
|
|
|
|
|
+ 'Num Vol arrivée',
|
|
|
|
|
+ 'Code Cie arrivée',
|
|
|
|
|
+ 'code_pays_origine',
|
|
|
|
|
+ 'Origine',
|
|
|
|
|
+ 'jour_sem_arrivee',
|
|
|
|
|
+ 'sin_ST_arrivee', 'cos_ST_arrivee',
|
|
|
|
|
+ 'jour_ferie_arrivee'
|
|
|
|
|
+]
|
|
|
|
|
+
|
|
|
|
|
+df_volArr = df[features_pmr].dropna().copy()
|
|
|
|
|
+
|
|
|
|
|
+df_volArr.head(30)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+!pip install lightgbm
|
|
|
|
|
+
|
|
|
|
|
+pip install catboost
|
|
|
|
|
+
|
|
|
|
|
+import pandas as pd
|
|
|
|
|
+import numpy as np
|
|
|
|
|
+import requests
|
|
|
|
|
+from sklearn.model_selection import train_test_split
|
|
|
|
|
+from sklearn.metrics import mean_squared_error, r2_score
|
|
|
|
|
+from lightgbm import LGBMRegressor
|
|
|
|
|
+from catboost import CatBoostRegressor, Pool
|
|
|
|
|
+from sklearn.metrics import confusion_matrix, accuracy_score, ConfusionMatrixDisplay
|
|
|
|
|
+import matplotlib.pyplot as plt
|
|
|
|
|
+
|
|
|
|
|
+API_KEY = "OaVTBoRzf6lvHiOkYr8UFhNg06CGGdB6"
|
|
|
|
|
+
|
|
|
|
|
+def get_holidays_for_year_country(year, country_code):
|
|
|
|
|
+ url = "https://calendarific.com/api/v2/holidays"
|
|
|
|
|
+ params = {
|
|
|
|
|
+ 'api_key': API_KEY,
|
|
|
|
|
+ 'country': country_code,
|
|
|
|
|
+ 'year': year,
|
|
|
|
|
+ 'type': 'national,religious'
|
|
|
|
|
+ }
|
|
|
|
|
+ try:
|
|
|
|
|
+ response = requests.get(url, params=params, timeout=10)
|
|
|
|
|
+ response.raise_for_status()
|
|
|
|
|
+ except requests.RequestException as e:
|
|
|
|
|
+ print(f"Erreur API pour {year} - {country_code}: {e}")
|
|
|
|
|
+ return set()
|
|
|
|
|
+
|
|
|
|
|
+ holidays = response.json().get('response', {}).get('holidays', [])
|
|
|
|
|
+ return set(hol['date']['iso'] for hol in holidays if 'date' in hol and 'iso' in hol['date'])
|
|
|
|
|
+
|
|
|
|
|
+def encode_cyclique_time(dt_series):
|
|
|
|
|
+ hours = dt_series.dt.hour.fillna(0)
|
|
|
|
|
+ minutes = dt_series.dt.minute.fillna(0)
|
|
|
|
|
+ total_minutes = hours * 60 + minutes
|
|
|
|
|
+ minutes_in_day = 24 * 60
|
|
|
|
|
+ sin_time = np.sin(2 * np.pi * total_minutes / minutes_in_day)
|
|
|
|
|
+ cos_time = np.cos(2 * np.pi * total_minutes / minutes_in_day)
|
|
|
|
|
+ return sin_time, cos_time
|
|
|
|
|
+
|
|
|
|
|
+def prepare_data():
|
|
|
|
|
+ local_csv_path = "export_des_missions_2025-06-13_100933-8546-653608357.csv"
|
|
|
|
|
+ cols_to_keep = ['Num Vol arrivée', 'Num Vol départ', 'Code Cie arrivée', 'Code Cie départ',
|
|
|
|
|
+ 'Origine', 'Destination', 'ST départ', 'ST arrivée']
|
|
|
|
|
+ df = pd.read_csv(local_csv_path, sep=';')
|
|
|
|
|
+ df = df[cols_to_keep].copy()
|
|
|
|
|
+
|
|
|
|
|
+ df['ST arrivée'] = pd.to_datetime(df['ST arrivée'], errors='coerce')
|
|
|
|
|
+ df['ST départ'] = pd.to_datetime(df['ST départ'], errors='coerce')
|
|
|
|
|
+
|
|
|
|
|
+ airports = pd.read_csv('airports.csv')
|
|
|
|
|
+ df_iata_pays = airports[['iata_code', 'iso_country']].dropna(subset=['iata_code'])
|
|
|
|
|
+
|
|
|
|
|
+ df_iata_pays_origin = df_iata_pays.rename(columns={'iata_code': 'Origine', 'iso_country': 'code_pays_origine'})
|
|
|
|
|
+ df_iata_pays_dest = df_iata_pays.rename(columns={'iata_code': 'Destination', 'iso_country': 'code_pays_destination'})
|
|
|
|
|
+
|
|
|
|
|
+ df = df.merge(df_iata_pays_origin, on='Origine', how='left')
|
|
|
|
|
+ df = df.merge(df_iata_pays_dest, on='Destination', how='left')
|
|
|
|
|
+
|
|
|
|
|
+ pmr_depart = df.groupby('Num Vol départ').size().reset_index(name='pmr_total_depart')
|
|
|
|
|
+ pmr_arrivee = df.groupby('Num Vol arrivée').size().reset_index(name='pmr_total_arrivee')
|
|
|
|
|
+
|
|
|
|
|
+ df_depart = df[['Num Vol départ', 'Code Cie départ', 'Origine', 'Destination', 'ST départ', 'code_pays_destination']].copy()
|
|
|
|
|
+ df_arrivee = df[['Num Vol arrivée', 'Code Cie arrivée', 'Origine', 'Destination', 'ST arrivée', 'code_pays_origine']].copy()
|
|
|
|
|
+
|
|
|
|
|
+ df_depart = df_depart.merge(pmr_depart, on='Num Vol départ', how='left')
|
|
|
|
|
+ df_arrivee = df_arrivee.merge(pmr_arrivee, on='Num Vol arrivée', how='left')
|
|
|
|
|
+
|
|
|
|
|
+ df_depart.rename(columns={
|
|
|
|
|
+ 'Num Vol départ': 'Num Vol',
|
|
|
|
|
+ 'Code Cie départ': 'Code Cie',
|
|
|
|
|
+ 'ST départ': 'ST',
|
|
|
|
|
+ 'code_pays_destination': 'code_pays',
|
|
|
|
|
+ 'pmr_total_depart': 'pmr total'
|
|
|
|
|
+ }, inplace=True)
|
|
|
|
|
+
|
|
|
|
|
+ df_arrivee.rename(columns={
|
|
|
|
|
+ 'Num Vol arrivée': 'Num Vol',
|
|
|
|
|
+ 'Code Cie arrivée': 'Code Cie',
|
|
|
|
|
+ 'ST arrivée': 'ST',
|
|
|
|
|
+ 'code_pays_origine': 'code_pays',
|
|
|
|
|
+ 'pmr_total_arrivee': 'pmr total'
|
|
|
|
|
+ }, inplace=True)
|
|
|
|
|
+
|
|
|
|
|
+ df_depart['type_vol'] = 'depart'
|
|
|
|
|
+ df_arrivee['type_vol'] = 'arrivee'
|
|
|
|
|
+
|
|
|
|
|
+ df_depart = df_depart[df_depart['Num Vol'].notna() & (df_depart['Num Vol'] != '')]
|
|
|
|
|
+ df_arrivee = df_arrivee[df_arrivee['Num Vol'].notna() & (df_arrivee['Num Vol'] != '')]
|
|
|
|
|
+
|
|
|
|
|
+ df_vols = pd.concat([df_depart, df_arrivee], ignore_index=True)
|
|
|
|
|
+ df_vols = df_vols.drop_duplicates(subset=['Num Vol'])
|
|
|
|
|
+
|
|
|
|
|
+ df_vols['Trajet'] = df_vols.apply(
|
|
|
|
|
+ lambda row: row['Destination'] if row['type_vol'] == 'depart' else row['Origine'],
|
|
|
|
|
+ axis=1
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ df_vols['ST'] = pd.to_datetime(df_vols['ST'], errors='coerce')
|
|
|
|
|
+ df_vols['Origine'] = df_vols['Origine'].fillna('')
|
|
|
|
|
+ df_vols['Destination'] = df_vols['Destination'].fillna('')
|
|
|
|
|
+ df_vols['code_pays'] = df_vols['code_pays'].fillna('')
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ # Compter le nombre de vols par jour
|
|
|
|
|
+ frequence_vol_par_jour = df_vols.groupby(['Num Vol']).size().rename('frequence_vol_par_jour')
|
|
|
|
|
+ df_vols = df_vols.merge(frequence_vol_par_jour, on='Num Vol', how='left')
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ unique_year_country = df_vols[['ST', 'code_pays']].dropna()
|
|
|
|
|
+ unique_year_country['year'] = unique_year_country['ST'].dt.year
|
|
|
|
|
+ unique_year_country = unique_year_country[['year', 'code_pays']].drop_duplicates()
|
|
|
|
|
+
|
|
|
|
|
+ holidays_map = {}
|
|
|
|
|
+ for _, row in unique_year_country.iterrows():
|
|
|
|
|
+ key = (int(row['year']), row['code_pays'])
|
|
|
|
|
+ if key not in holidays_map:
|
|
|
|
|
+ holidays_map[key] = get_holidays_for_year_country(*key)
|
|
|
|
|
+
|
|
|
|
|
+ def is_holiday(row):
|
|
|
|
|
+ if pd.isnull(row['ST']) or pd.isnull(row['code_pays']):
|
|
|
|
|
+ return False
|
|
|
|
|
+ key = (row['ST'].year, row['code_pays'])
|
|
|
|
|
+ date_str = row['ST'].strftime('%Y-%m-%d')
|
|
|
|
|
+ return date_str in holidays_map.get(key, set())
|
|
|
|
|
+
|
|
|
|
|
+ df_vols['jour_ferie'] = df_vols.apply(is_holiday, axis=1)
|
|
|
|
|
+ df_vols['sin_ST'], df_vols['cos_ST'] = encode_cyclique_time(df_vols['ST'])
|
|
|
|
|
+ df_vols['jour_sem'] = df_vols['ST'].dt.dayofweek.fillna(-1).astype(int)
|
|
|
|
|
+
|
|
|
|
|
+ final_cols = [
|
|
|
|
|
+ 'Num Vol',
|
|
|
|
|
+ 'Code Cie',
|
|
|
|
|
+ 'Trajet',
|
|
|
|
|
+ 'jour_sem',
|
|
|
|
|
+ 'sin_ST',
|
|
|
|
|
+ 'cos_ST',
|
|
|
|
|
+ 'jour_ferie',
|
|
|
|
|
+ 'frequence_vol_par_jour',
|
|
|
|
|
+ 'pmr total'
|
|
|
|
|
+ ]
|
|
|
|
|
+
|
|
|
|
|
+ df_vols_final = df_vols[final_cols].copy()
|
|
|
|
|
+
|
|
|
|
|
+ df_vols_final.to_csv('vols.csv', index=False)
|
|
|
|
|
+ print("Données enrichies sauvegardées dans vols.csv")
|
|
|
|
|
+
|
|
|
|
|
+def train_and_evaluate():
|
|
|
|
|
+ df = pd.read_csv('vols.csv')
|
|
|
|
|
+
|
|
|
|
|
+ X = df.drop(columns=["pmr total", "Num Vol"])
|
|
|
|
|
+ y = df["pmr total"]
|
|
|
|
|
+
|
|
|
|
|
+ categorical_cols = X.select_dtypes(include='object').columns.tolist()
|
|
|
|
|
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
|
|
|
|
+
|
|
|
|
|
+ train_pool = Pool(X_train, y_train, cat_features=categorical_cols)
|
|
|
|
|
+ test_pool = Pool(X_test, y_test, cat_features=categorical_cols)
|
|
|
|
|
+
|
|
|
|
|
+ model = CatBoostRegressor(
|
|
|
|
|
+ iterations=1000,
|
|
|
|
|
+ learning_rate=0.05,
|
|
|
|
|
+ depth=6,
|
|
|
|
|
+ loss_function='RMSE',
|
|
|
|
|
+ eval_metric='RMSE',
|
|
|
|
|
+ random_seed=42,
|
|
|
|
|
+ verbose=100,
|
|
|
|
|
+ early_stopping_rounds=50
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ model.fit(train_pool, eval_set=test_pool, use_best_model=True)
|
|
|
|
|
+
|
|
|
|
|
+ y_pred = model.predict(X_test)
|
|
|
|
|
+
|
|
|
|
|
+ mse = mean_squared_error(y_test, y_pred)
|
|
|
|
|
+ r2 = r2_score(y_test, y_pred)
|
|
|
|
|
+
|
|
|
|
|
+ print(f"Mean Squared Error (MSE): {mse:.2f}")
|
|
|
|
|
+ print(f"R^2 Score: {r2:.2f}")
|
|
|
|
|
+
|
|
|
|
|
+ df_result = X_test.copy()
|
|
|
|
|
+ df_result['y_true'] = y_test
|
|
|
|
|
+ df_result['y_pred'] = y_pred
|
|
|
|
|
+ print(df_result.head(10))
|
|
|
|
|
+
|
|
|
|
|
+ total_y_true = y_test.sum()
|
|
|
|
|
+ print(f"Somme totale de y_true : {total_y_true}")
|
|
|
|
|
+ total_y_pred = y_pred.sum()
|
|
|
|
|
+ print(f"Somme totale de y_pred : {total_y_pred}")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ y_test_class = y_test.round().astype(int)
|
|
|
|
|
+ y_pred_class = np.round(y_pred).astype(int)
|
|
|
|
|
+
|
|
|
|
|
+ cm = confusion_matrix(y_test_class, y_pred_class)
|
|
|
|
|
+ acc = accuracy_score(y_test_class, y_pred_class)
|
|
|
|
|
+
|
|
|
|
|
+ print(f"\nAccuracy (rounded predictions): {acc:.2f}")
|
|
|
|
|
+
|
|
|
|
|
+ disp = ConfusionMatrixDisplay(confusion_matrix=cm)
|
|
|
|
|
+ disp.plot(cmap='Blues')
|
|
|
|
|
+ plt.title('Confusion Matrix (rounded values)')
|
|
|
|
|
+ plt.show()
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == "__main__":
|
|
|
|
|
+ prepare_data()
|
|
|
|
|
+ train_and_evaluate()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+import pandas as pd
|
|
|
|
|
+from sklearn.linear_model import Ridge
|
|
|
|
|
+from sklearn.metrics import mean_squared_error, r2_score
|
|
|
|
|
+from sklearn.model_selection import train_test_split
|
|
|
|
|
+
|
|
|
|
|
+# Charger les données
|
|
|
|
|
+df = pd.read_csv("vols.csv")
|
|
|
|
|
+
|
|
|
|
|
+y = df["pmr total"]
|
|
|
|
|
+X = df.drop(columns=["pmr total"])
|
|
|
|
|
+
|
|
|
|
|
+X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
|
|
|
|
+
|
|
|
|
|
+# Encodage par la moyenne cible (Target Encoding)
|
|
|
|
|
+X_train_enc = X_train.copy()
|
|
|
|
|
+X_test_enc = X_test.copy()
|
|
|
|
|
+
|
|
|
|
|
+for col in X.select_dtypes(include=["object", "category"]).columns:
|
|
|
|
|
+ means = X_train_enc.join(y_train)[[col, "pmr total"]].groupby(col)["pmr total"].mean()
|
|
|
|
|
+ X_train_enc[col] = X_train_enc[col].map(means)
|
|
|
|
|
+ X_test_enc[col] = X_test_enc[col].map(means)
|
|
|
|
|
+
|
|
|
|
|
+ # Remplacer les valeurs inconnues (non vues dans le train) par la moyenne globale
|
|
|
|
|
+ global_mean = y_train.mean()
|
|
|
|
|
+ X_test_enc[col] = X_test_enc[col].fillna(global_mean)
|
|
|
|
|
+
|
|
|
|
|
+# Modèle Ridge
|
|
|
|
|
+model = Ridge(alpha=1.0)
|
|
|
|
|
+model.fit(X_train_enc, y_train)
|
|
|
|
|
+
|
|
|
|
|
+# Prédictions
|
|
|
|
|
+y_pred = model.predict(X_test_enc)
|
|
|
|
|
+
|
|
|
|
|
+# Évaluation
|
|
|
|
|
+mse = mean_squared_error(y_test, y_pred)
|
|
|
|
|
+r2 = r2_score(y_test, y_pred)
|
|
|
|
|
+
|
|
|
|
|
+print(f"Mean Squared Error (MSE): {mse:.2f}")
|
|
|
|
|
+print(f"R² Score: {r2:.2f}")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+with open("AE.ipynb", "r", encoding="utf-8") as f:
|
|
|
|
|
+ notebook = json.load(f)
|
|
|
|
|
+
|
|
|
|
|
+with open("code.py", "w", encoding="utf-8") as f:
|
|
|
|
|
+ for cell in notebook["cells"]:
|
|
|
|
|
+ if cell["cell_type"] == "code":
|
|
|
|
|
+ f.write("".join(cell["source"]))
|
|
|
|
|
+ f.write("\n\n")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|