{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Algorithmic Machine Learning 🧠\n## AML-Challenge_1_Baseline 🧑🏻‍💻\n\n![alt text](https://drive.google.com/uc?export=view&id=1Uxqe7gHt6GTLjZxIXd2WAjgIvPnMkKad)\n\nProfessor : Pietro MICHIARDI 👨🏻‍🏫\n\n**Team 7️⃣ :**   \nSourish GHOSH   \nShree hari BOYALLA  \nTanmay CHAKRABORTY  \nUtkarsh TREHAN  \n","metadata":{"id":"wqNHsRT2OiC5"}},{"cell_type":"markdown","source":"## Connecting Google Drive 🔐","metadata":{"id":"oHpx_8S3Tlcq"}},{"cell_type":"code","source":"from google.colab import drive\ndrive.mount('/content/gdrive')","metadata":{"id":"O_DEbmBLsgeg","outputId":"3bf778c6-8ad3-43ff-9799-968b85b4821b"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Acquisition ⬇️","metadata":{"id":"U68EgsVYVoTd"}},{"cell_type":"code","source":"! pip install -q kaggle","metadata":{"id":"mkQrfg0pVo5z"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from google.colab import files\nfiles.upload()","metadata":{"id":"yujtjKlSVpCF","outputId":"123de372-1f07-4187-eb3c-9269a66c2668"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! mkdir ~/.kaggle","metadata":{"id":"TdWXjT_ZVpI7","outputId":"369e079b-9c20-49cc-b966-c26800a07e5f"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! cp kaggle.json ~/.kaggle/","metadata":{"id":"4mZeL-Z5VpNO"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! chmod 600 ~/.kaggle/kaggle.json","metadata":{"id":"nP_O_iGZWLlz"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Uncomment the following to test if everything's okay by running this command.\n# !kaggle datasets list","metadata":{"id":"H_rvjaxxWL5K"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!kaggle competitions download -c eurecom-aml-2021-challenge-1","metadata":{"id":"8S-JuljOWMAr","outputId":"9538208e-bbcc-4bde-9257-aac0a55e9882"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! mkdir train","metadata":{"id":"JVq3sbKBWMHU","outputId":"5a94733d-32f8-46da-c564-9de97eef9a87"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! unzip train_features.csv.zip -d train","metadata":{"id":"-0YGYpeSWMNz","outputId":"4e7827d2-e42b-4afc-df89-d87bb69ccecb"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! unzip train_targets.csv.zip -d train","metadata":{"id":"kV0lOgrgXUG6","outputId":"bd638411-3d76-42a0-95e2-f2b1dcb77e8d"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! mkdir test","metadata":{"id":"upVULr4YXHaz","outputId":"0080561d-039e-444c-f518-46e7a33f9e8a"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! unzip test_features.csv.zip -d test","metadata":{"id":"jVHmCAErXTjS","outputId":"3fe43781-ec4f-4dbf-984f-0ea221c4a554"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Wrangling 🔗","metadata":{"id":"EbkSQlXzUKo8"}},{"cell_type":"code","source":"#import the necssary libraries\nimport os\nfrom datetime import datetime\nfrom datetime import time\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib import rc \nfrom matplotlib.ticker import FuncFormatter\nimport seaborn as sns\nimport matplotlib.patches as mpatches","metadata":{"id":"et79B_ujBXLV","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_dtype = {\n    'ID'                   : int,\n    'YEAR'                 : int,  \n    'MONTH'                : int,  \n    'DAY'                  : int,  \n    'DAY_OF_WEEK'          : int,  \n    'AIRLINE'              : str, \n    'FLIGHT_NUMBER'        : str,  \n    'TAIL_NUMBER'          : str, \n    'ORIGIN_AIRPORT'       : str, \n    'DESTINATION_AIRPORT'  : str, \n    'SCHEDULED_DEPARTURE'  : str,  \n    'DEPARTURE_TIME'       : str, \n    'DEPARTURE_DELAY'      : float,\n    'TAXI_OUT'             : str, \n    'WHEELS_OFF'           : str,\n    'SCHEDULED_TIME'       : float,\n    'AIR_TIME'             : float,\n    'DISTANCE'             : int,\n    'SCHEDULED_ARRIVAL'    : str,\n    'DIVERTED'             : int,  \n    'CANCELLED'            : int,  \n    'CANCELLATION_REASON'  : str\n}\n\ny_dtype = {\n    'ID'                   : int,\n    \"ARRIVAL_DELAY\"        : float\n}\n","metadata":{"id":"_lWaaGIkxjU_","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_df = pd.read_csv(\"../input/eurecom-aml-2021-challenge-1/data/train_features.csv\", dtype=X_dtype)\ny_train_df = pd.read_csv(\"../input/eurecom-aml-2021-challenge-1/data/train_targets.csv\", dtype=y_dtype)\n\nairlines_df = pd.read_csv('../input/eurecom-aml-2021-challenge-1/data/airlines.csv').rename({'AIRLINE': 'AIRLINE_NAME'}, axis='columns')\nairports_df = pd.read_csv('../input/eurecom-aml-2021-challenge-1/data/airports.csv').rename({'AIRPORT': 'AIRPORT_NAME'}, axis='columns')","metadata":{"id":"eSTCR7JwOQpt","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def null_rows(data = None):\n    rows_with_nan = []\n    for index, row in data.iterrows():\n        is_nan_series = row.isnull()\n        if is_nan_series.any():\n            rows_with_nan.append(index)\n    return rows_with_nan","metadata":{"id":"DT_-JV0eoT3r","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def data_probing(data = None, n=5):\n    print(\"---------- Head ----------\")\n    display(data.head(n))\n    print(\"\\n---------- Shape ----------\")\n    print(\"Number of Rows: {}\\nNumber of Columns: {}\".format(data.shape[0], data.shape[1]))\n    print(\"\\n---------- Null Values ----------\")\n    print(data.isnull().sum())\n    print(\"\\n---------- Rows with null values ----------\")\n    rows_with_null = [data.iloc[[index]] for index in null_rows(data)]\n    for row in rows_with_null:\n        display(row)","metadata":{"id":"A_0OhYeglrJX","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_probing(airports_df)","metadata":{"id":"3ZgoNDB1zyZB","outputId":"83dfd97a-9b33-411e-d0f6-03fecaec9fbe","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Note:** There are NULL values present in airports_df in the column \"LATITUDE\" and \"LONGITUDE\".","metadata":{"id":"mLl0vyZ5bhUs"}},{"cell_type":"code","source":"data_probing(airlines_df)","metadata":{"id":"XniKbL0SqaBs","outputId":"823d69c7-4bc2-4bbe-bbd7-2f1415e0fdf1","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def parse_hhmm(x):\n    if x == '2400':\n        x = '0000'\n    x =  x[:-2] + ':' + x[-2:]\n    return x\n\nX_train_df.SCHEDULED_DEPARTURE = X_train_df.SCHEDULED_DEPARTURE.apply(parse_hhmm)\nX_train_df.DEPARTURE_TIME = X_train_df.DEPARTURE_TIME.apply(parse_hhmm)\nX_train_df.SCHEDULED_ARRIVAL = X_train_df.SCHEDULED_ARRIVAL.apply(parse_hhmm)","metadata":{"id":"HhbkBaoRMrnl","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_df.SCHEDULED_DEPARTURE = pd.to_datetime(X_train_df['SCHEDULED_DEPARTURE'],format= '%H:%M' ).dt.time\nX_train_df.DEPARTURE_TIME = pd.to_datetime(X_train_df['DEPARTURE_TIME'],format= '%H:%M' ).dt.time\nX_train_df.SCHEDULED_ARRIVAL = pd.to_datetime(X_train_df['SCHEDULED_ARRIVAL'],format= '%H:%M' ).dt.time","metadata":{"id":"xzekcDmhaZ3e","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_df.head(5)","metadata":{"id":"HcGpaoD-cKyQ","outputId":"197cf51d-8c22-44de-e34c-7e39ce51ab2f","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Merge feature dataframe and target dataframe for data exploration\ndf_train_merged = pd.merge(X_train_df, y_train_df, on='ID')","metadata":{"id":"gIpsL1HlaU4a","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create new columns used for data exploration\ndf_train_merged['DELAYED'] = (df_train_merged.ARRIVAL_DELAY > 0)\ndf_train_merged['DATE'] = pd.to_datetime(df_train_merged[['YEAR', 'MONTH', 'DAY']])","metadata":{"id":"_JF1zNhAaUw6","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Note:** There are NULL values present in X_train_df in the column \"CANCELLATION_REASON\". Other datasets have no NULL values in them.","metadata":{"id":"SAz8uQ9Oq8cD"}},{"cell_type":"markdown","source":"## Data Analysis 📈📉📊\n\n**Basic queries:**  \n1. How many unique origin airports?  \n2. How many unique destination airports?  \n3. How many carriers?  \n4. How many flights that have a scheduled departure time later than 18h00?  \n\n**Statistics on flight volume: this kind of statistics are helpful to reason about delays. Indeed, it is plausible to assume that \"the more flights in an airport, the higher the probability of delay\".**\n\n1. How many flights in each month of the year?  \n2. Is there any relationship between the number of flights and the days of week?  \n3. How many flights in different days of months and in different hours of days?  \n4. Which are the top 20 busiest airports (this depends on inbound and outbound traffic)?  \n5. Which are the top 20 busiest carriers?  \n\n**Statistics on the fraction of delayed flights**\n1. What is the percentage of delayed flights (over total flights) for different hours of the day?  \n2. Which hours of the day are characterized by the longest flight delay?\n3. What are the fluctuation of the percentage of delayed flights over different time granularities?  \n4. What is the percentage of delayed flights which depart from one of the top 20 busiest airports?  \n5. What is the percentage of delayed flights which belong to one of the top 20 busiest carriers?  ","metadata":{"id":"36px1LUKaUh0"}},{"cell_type":"code","source":"def unique_values(data):\n    unique_tuple = data.unique()\n    return (unique_tuple, len(unique_tuple))","metadata":{"id":"vdatjjmfaUaL","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"time_compare = time(18, 0, 0)\nprint(\"How many unique origin airports?\\nThere are {} origin airports\\n\".format(unique_values(df_train_merged['ORIGIN_AIRPORT'])[1]))\nprint(\"How many unique destination airports?\\nThere are {} destination airports\\n\".format(unique_values(df_train_merged['DESTINATION_AIRPORT'])[1]))\nprint(\"How many carriers?\\nThere are {} carriers\\n\".format(unique_values(df_train_merged['AIRLINE'])[1]))\nprint(\"How many flights that have a scheduled departure time later than 18h00?\\nThere are {} flights later then 18h00\\n\".format((X_train_df['SCHEDULED_DEPARTURE'] > time_compare).sum()))","metadata":{"id":"9CaNEN4DaURy","outputId":"b070900c-8f04-4558-d979-9d8d900e9620","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# How many flights in each month of the year?\nplt.rcParams[\"figure.figsize\"] = (15,7)\nval = df_train_merged.MONTH.value_counts().sort_index()\nx = val.index\ny = val.values\nmonths = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',\n          'Aug', 'Sept', 'Oct', 'Nov', 'Dec']\n\ndef millions(x, pos):\n    'The two args are the value and tick position'\n    return '%1.1fM' % (x*1e-6)\n\nformatter = FuncFormatter(millions)\n\nfig, ax = plt.subplots()\n\nplt.xlabel('Month')\nplt.ylabel('Number of flights')\nplt.title('Number of flights in a month')\nplt.bar(x, y, width=0.35, color=['#239CD3'])\nplt.plot(x,y, '-ok')\nplt.xticks(x, months)\nax.yaxis.set_major_formatter(formatter)\nplt.show()\nplt.close()","metadata":{"id":"3ypguAUvaTgj","outputId":"2b033974-6888-4f1f-c614-a088d46db224","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Note:** The number of flights running in a given month of the year, it can be observed that, the months of june, july and December, have high traffic, which is plausible because of the holiday seasons in those month.Infact, it is obviuos for airlines to run more flights during the holidays season, as the demand to travel is high.","metadata":{"id":"3JSYYkymcAmK"}},{"cell_type":"code","source":"# Is there any relationship between the number of flights and the days of week?\nplt.rcParams[\"figure.figsize\"] = (15,7)\nval = df_train_merged.DAY_OF_WEEK.value_counts().sort_index()\nx = val.index\ny = val.values\nplt.plot(x,y, '-o')\nplt.xlabel('Days of WEEK')\nplt.ylabel('Number of Flights')\nplt.xticks(x, ['SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY'])\nplt.title('Number of Flights in a Day Of Week')\nfor a,b in zip(x, y): \n    plt.annotate(str(b), (a,b),ha='right', va='bottom', fontsize=14)\nplt.show()","metadata":{"id":"vXRyB9v7phGo","outputId":"138caed8-3d03-429a-963d-134fc8f20496","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Note:** Considering the 1st day is sunday and so on, we can see that the number of flights increasing as days progress from sunday to Thursday as we move from one week to another. Then there is a decrease in the number of flights in the friday which is starting of the weekend, and then there is increase again on saturday.","metadata":{"id":"e7_63MVdcZrA"}},{"cell_type":"code","source":"#busiest airports by inbound and outbound traffic\nplt.rcParams[\"figure.figsize\"] = (18,10)\ntotal = df_train_merged.ORIGIN_AIRPORT.value_counts()[:20]+df_train_merged.DESTINATION_AIRPORT.value_counts()[:20]\nlabels = total.index\ntotal = total.values\n\n#getting the delayed flights\norigin_del = pd.DataFrame(df_train_merged.groupby('ORIGIN_AIRPORT').DELAYED.sum()).reset_index().sort_values('DELAYED', ascending=False)\ndest_del = pd.DataFrame(df_train_merged.groupby('DESTINATION_AIRPORT').DELAYED.sum()).reset_index().sort_values('DELAYED', ascending=False)\ntotal_del_airports = origin_del.merge(dest_del, left_on='ORIGIN_AIRPORT', right_on='DESTINATION_AIRPORT').head(20)\ntotal_del = (total_del_airports.DELAYED_x+total_del_airports.DELAYED_x).values\nsns.set_theme(style=\"whitegrid\")\nbar1 = sns.barplot(x=labels,  y=total, color='red')\n\nbar2 = sns.barplot(x=labels, y=total_del, color='black')\n\n# add legend\ntop_bar = mpatches.Patch(color='red', label='Total_flights')\nbottom_bar = mpatches.Patch(color='black', label='Delayed_flights')\n\nplt.ylabel('NUMBER OF FLIGHTS', fontsize=18)\nplt.xlabel('Airports', fontsize=18)\nplt.title('TOTAL FLIGHTS VS DELAYED FLIGHTS  of AIRLINE', fontsize=18)\nplt.legend(handles=[top_bar, bottom_bar], fontsize=18)\n# show the graph\nplt.show()","metadata":{"id":"pcsj-tRZpliX","outputId":"70a04b23-4ef3-4811-c7a0-e4e74a56ec36","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Top 20 busiest aiports by traffic(Arrival+Departure). It can also, be seen that, the airport that has highest traffic also has more flights delayed. ","metadata":{"id":"vteSMBwEthGk"}},{"cell_type":"code","source":"#pd.merge(airlines_df, train_df.AIRLINE.value_counts(), left_on='IATA', right_on=train_df.AIRLINE.index)\ncounts = pd.DataFrame(df_train_merged.AIRLINE.value_counts()).reset_index()\ndelayed_counts = pd.DataFrame(df_train_merged.groupby('AIRLINE').DELAYED.sum()).reset_index()\ndelayed_counts.columns =  ['IATA_CODE', 'DELAYED']\n\ncounts.columns = ['IATA_CODE', 'fleet']\ncounts = counts.merge(delayed_counts, on='IATA_CODE', how='inner')\ntotal = counts.merge(airlines_df, on='IATA_CODE', how='inner')\nlabels = total.AIRLINE_NAME\nt_f = total.fleet\nd_f = total.DELAYED\nsns.set_theme(style=\"whitegrid\")\nbar1 = sns.barplot(x=labels,  y=t_f, color='darkblue')\n\nbar2 = sns.barplot(x=labels, y=d_f, color='lightblue')\n\n# add legend\ntop_bar = mpatches.Patch(color='darkblue', label='Total_flights')\nbottom_bar = mpatches.Patch(color='lightblue', label='Delayed_flights')\n\nplt.xticks(rotation=-80)\nplt.ylabel('NUMBER OF FLIGHTS', fontsize=18)\nplt.xlabel('Airlines', fontsize=18)\nplt.title('TOTAL FLIGHTS VS DELAYED FLIGHTS  of AIRLINE', fontsize=18)\nplt.legend(handles=[top_bar, bottom_bar], fontsize=18)\n# show the graph\nplt.show()","metadata":{"id":"q-HPPipyrSrz","outputId":"cc95cbda-1829-4096-be96-b185d4d797d8","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The number of flights run by each carrier and the percentage of them delayed is shown. Its quite evident that, more the flights a carrier runs, there are more delays, which could be because of the fact that more flights that are run, more maintence delays and scheduling conflicts etc.","metadata":{"id":"9bUSCLiKtp3b"}},{"cell_type":"code","source":"def parse_hour(x):\n    return x.hour\n\ndf_train_merged['SCHEDULED_DEPARTURE_HOUR'] = df_train_merged.SCHEDULED_DEPARTURE.apply(parse_hour)","metadata":{"id":"O0bVYp_0Ndbb","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# What is the percentage of delayed flights (over total flights) for different hours of the day?\nval = df_train_merged.groupby(df_train_merged.SCHEDULED_DEPARTURE_HOUR)['DELAYED'].agg(\"mean\")\nx = val.index\ny = val.values\nhour_interval = ['00:00-1:00', '01:00-2:00', '02:00-3:00', '03:00-4:00', '04:00-5:00', '05:00-6:00', '06:00-7:00', '07:00-8:00', '08:00-9:00', '09:00-10:00', '10:00-11:00', '11:00-12:00', '12:00-13:00', '13:00-14:00', '14:00-15:00', '15:00-16:00', '16:00-17:00', '17:00-18:00', '18:00-19:00', '19:00-20:00', '20:00-21:00', '21:00-22:00', '22:00-23:00', '23:00-00:00' ]\n\ndef millions(x, pos):\n    'The two args are the value and tick position'\n    return '%1.1f' % (x*100)\n\nformatter = FuncFormatter(millions)\n\nfig, ax = plt.subplots()\n\nplt.xlabel('Interval')\nplt.ylabel('Percentage of delayed flights')\nplt.title('Number of flights in a given hour of a day')\nplt.bar(x, y, width=0.35, color=['#239CD3'])\nplt.plot(x,y, '-ok')\nplt.xticks(x, hour_interval, rotation=45)\nax.yaxis.set_major_formatter(formatter)\nplt.show()\nplt.close()","metadata":{"id":"hPAHmg0pS2vR","outputId":"c505efaf-821f-4aa8-d5d1-83a1f9644d7a","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The most busiest hours are in the evening, which can be seen from the trend in the graph above.","metadata":{"id":"YWqykfFjtviU"}},{"cell_type":"code","source":"def parse_dep_delay(x):\n    return abs(x)\n\ndf_train_merged['DEPARTURE_DELAY_ABS'] = df_train_merged.DEPARTURE_DELAY.apply(parse_dep_delay)","metadata":{"id":"oPTRqzWCX6U7","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Which hours of the day are characterized by the longest flight delay?\ndf_train_merged.groupby(df_train_merged.SCHEDULED_DEPARTURE_HOUR)['DEPARTURE_DELAY_ABS'].agg(\"mean\").sort_values()","metadata":{"id":"3ZAeiF-OXldF","outputId":"c8e50469-3a64-4652-ce33-aec42e342e5e","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The longest flight delay hour is 20:00 with an average delay of 19.895, it can also be observed,that this is also one of the busiest hours in a given day in terms of flight traffic.","metadata":{"id":"C03AsXKEvr2g"}},{"cell_type":"markdown","source":"# Data Preprocessing\n","metadata":{"id":"YvIaf1mSfD5x"}},{"cell_type":"code","source":"X_dtype = {\n    'ID'                   : int,\n    'YEAR'                 : int,  \n    'MONTH'                : int,  \n    'DAY'                  : int,  \n    'DAY_OF_WEEK'          : int,  \n    'AIRLINE'              : str, \n    'FLIGHT_NUMBER'        : str,  \n    'TAIL_NUMBER'          : str, \n    'ORIGIN_AIRPORT'       : str, \n    'DESTINATION_AIRPORT'  : str, \n    'SCHEDULED_DEPARTURE'  : str,  \n    'DEPARTURE_TIME'       : str, \n    'DEPARTURE_DELAY'      : float,\n    'TAXI_OUT'             : str, \n    'WHEELS_OFF'           : str,\n    'SCHEDULED_TIME'       : float,\n    'AIR_TIME'             : float,\n    'DISTANCE'             : int,\n    'SCHEDULED_ARRIVAL'    : str,\n    'DIVERTED'             : int,  \n    'CANCELLED'            : int,  \n    'CANCELLATION_REASON'  : str\n}\n\ny_dtype = {\n    'ID'                   : int,\n    \"ARRIVAL_DELAY\"        : float\n}\n\nX_train_df = pd.read_csv(\"../input/eurecom-aml-2021-challenge-1/data/train_features.csv\", dtype=X_dtype)\ny_train_df = pd.read_csv(\"../input/eurecom-aml-2021-challenge-1/data/train_targets.csv\", dtype=y_dtype)","metadata":{"id":"NmRWDDwzfNFz","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#droping certain colums\ndef drop_col(df, columns, inplace=True):\n  df.drop(columns=columns, inplace=inplace)","metadata":{"id":"atcX3pbhflLA","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns = ['ID','DIVERTED','CANCELLED','CANCELLATION_REASON', 'FLIGHT_NUMBER', 'TAIL_NUMBER', 'WHEELS_OFF']","metadata":{"id":"wDIA7hZIfmIk","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop_col(X_train_df, columns)\ndrop_col(y_train_df, ['ID'])","metadata":{"id":"mdy8s3j-fowR","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#label encoding columns with categorical values\nfrom sklearn.preprocessing import LabelEncoder\n\nencoder = LabelEncoder()\ndef encoding(df):\n  for col in df.columns:\n    if df[col].dtype == 'object':\n      df[col] = encoder.fit_transform(df[col])\n\nencoding(X_train_df)","metadata":{"id":"jTmLbxXVfsnn","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_df.head()","metadata":{"id":"0qkl-HlqE3TQ","outputId":"d5fadbe2-11dd-4ec8-a1ce-ccaf4bab1ca7","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_df.info()","metadata":{"id":"EgZFPXfquqnq","outputId":"97d2d3ea-3661-4e3c-dd80-c9fb22595908","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{"id":"NVuDYyffpNO2"}},{"cell_type":"markdown","source":"# Why we chose this model?\n\nHere in this project we chose to work with Random Forest beacuse of the following reasons:\n\n\n*   An important aspect of a data science project is the interpretability of the data. Models are generally a black-box to interpret our data. There is always a trade-off between the accuracy and interpretability of model. So we had to find the right balance between them. Here Random Forest prevails. Here trees work together to accurately represent feature importance of the decision trees.\n*   Essentially trees are weak classifiers with high bias. But even upon increasing the numbers of trees in random forest, it splits over the features randomly, and then by means of bootsrapped aggregation(bagging), it reduces the overall variance of the model. Thus it maintains the Bias-Variance tradeoff. \n*    Normalization of the data is not required as it uses the rule based approach of the decison trees.\n\n\nHere in this project, we are working on the prediction of the airline delay. Thus we wanted to potray on what factors the model decides why the certain flights are delayed. Thus using the \"Feature Imporance\" feature of the Random Forest we displayed which of the features are quintessential to its decision making.","metadata":{"id":"yZNWSApTu_rr"}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.model_selection import train_test_split\nfrom math import sqrt","metadata":{"id":"zbvQtrRdlJd3","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(X_train_df, y_train_df, test_size = 0.2, random_state=0)","metadata":{"id":"rOjr4aVglm5b","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = RandomForestRegressor(n_estimators=20, criterion='mse', bootstrap=True)\nmodel.fit(x_train, y_train)","metadata":{"id":"W7FWenZRmRT7","outputId":"e57484af-a943-4481-d29c-3c153a5b586f","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred = model.predict(x_test)","metadata":{"id":"vpZzekVbFNLQ","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.score(x_test, y_test)","metadata":{"id":"PRWapuMCWhv_","outputId":"299ab245-5889-410b-ca42-fb72ca384289","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_squared_error(y_test, Y_pred)**0.5","metadata":{"id":"24ieVvTfelOv","outputId":"aa18a674-4f30-41cf-caca-4ed9012773f8","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_feature_importance(importance,names,model_type):\n\n  #Create arrays from feature importance and feature names\n  feature_importance = np.array(importance)\n  feature_names = np.array(names)\n\n  #Create a DataFrame using a Dictionary\n  data={'feature_names':feature_names,'feature_importance':feature_importance}\n  fi_df = pd.DataFrame(data)\n\n  #Sort the DataFrame in order decreasing feature importance\n  fi_df.sort_values(by=['feature_importance'], ascending=False,inplace=True)\n\n  #Define size of bar plot\n  plt.figure(figsize=(10,8))\n  #Plot Searborn bar chart\n  sns.barplot(x=fi_df['feature_importance'], y=fi_df['feature_names'])\n  #Add chart labels\n  plt.title(model_type + 'FEATURE IMPORTANCE')\n  plt.xlabel('FEATURE IMPORTANCE')\n  plt.ylabel('FEATURE NAMES')","metadata":{"id":"qk7smqZ2svlB","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Conclusion ","metadata":{"id":"ANUq36lyu1hW"}},{"cell_type":"code","source":"plot_feature_importance(model.feature_importances_,X_train_df.columns,'RANDOM FOREST')","metadata":{"id":"6lfjj1OUpA2N","outputId":"f689e167-c17c-469c-f749-80e91236a3b7","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As we can see the most important features are \"DEPARTURE_DELAY\", \"AIR_TIME\", \"TAXI_OUT\", \"SCHEDULED_TIME\", and \"DISTANCE\". ","metadata":{"id":"IYaOt0YIuOdl"}},{"cell_type":"markdown","source":"Based on this we can say that the model comes to a decision based on these parameters, which help us say by certainty the reason why a flight might be delayed, which is our target prediction. ","metadata":{"id":"Uzf34kgjuirr"}}]}