{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": 67, "metadata": { "id": "KGftS__cd1wP" }, "outputs": [], "source": [ "from mpl_toolkits.mplot3d import Axes3D\n", "from sklearn.preprocessing import StandardScaler\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import os\n", "import pandas as pd" ] }, { "cell_type": "code", "source": [ "def plotPerColumnDistribution(df, nGraphShown, nGraphPerRow):\n", " nunique = df.nunique()\n", " df = df[[col for col in df if nunique[col] > 1 and nunique [col] < 50]]\n", " nRow, nCol = df.shape\n", " columnNames = list(df)\n", " # Calculate nGraphRow before using it in the calculation\n", " nGraphRow = (nCol + nGraphPerRow - 1) // nGraphPerRow # Use // for integer division to ensure an integer result\n", " plt.figure(num = None, figsize = (6 * nGraphPerRow, 8 * nGraphRow), dpi = 80, facecolor = 'w', edgecolor = 'k')\n", " for i in range(min(nCol, nGraphShown)):\n", " plt.subplot(nGraphRow, nGraphPerRow, i + 1)\n", " columnDf = df.iloc[:, i]\n", " if (not np.issubdtype(type(columnDf.iloc[0]), np.number)):\n", " valueCounts = columnDf.value_counts()\n", " valueCounts.plot.bar()\n", " else:\n", " columnDf.hist()\n", " plt.ylabel('counts')\n", " plt.xticks(rotation = 90)\n", " plt.title(f'{columnNames[i]} (column {i})')\n", " plt.tight_layout(pad = 1.0, w_pad = 1.0, h_pad = 1.0)\n", " plt.show()" ], "metadata": { "id": "8e-f3-j8eFFY" }, "execution_count": 68, "outputs": [] }, { "cell_type": "code", "source": [ "def plotCorrelationMatrix(df, graphWidth):\n", " filename = df.dataframeName\n", " # Drop columns with any NaN values\n", " # df = df.dropna(axis='columns') # Commented out to potentially improve the result\n", "\n", " # Drop rows with missing values in any column\n", " # Drop rows with missing values in any column\n", " df = df.dropna()\n", "\n", " # Select only numeric columns\n", " df = df.select_dtypes(include=[np.number])\n", "\n", " df = df[[col for col in df if df[col].nunique() > 1]]\n", " if df.shape[1] < 2:\n", " print(f'No correlation plots shown: The number of non-NaN or constant columns ({df.shape[1]}) is less than 2')\n", " return\n", " corr = df.corr()\n", " plt.figure(num=None, figsize=(graphWidth, graphWidth), dpi=80, facecolor='w', edgecolor='k')\n", " corrMat = plt.matshow(corr, fignum=2)\n", " plt.xticks(range(len(corr.columns)), corr.columns, rotation=90)\n", " plt.yticks(range(len(corr.columns)), corr.columns)\n", " plt.gca().xaxis.tick_bottom()\n", " plt.colorbar(corrMat)\n", " plt.title(f'Correlation Matrix for {filename}', fontsize=15)\n", " plt.show()" ], "metadata": { "id": "COTtxITQgIPc" }, "execution_count": 69, "outputs": [] }, { "cell_type": "code", "source": [ "def plotScatterMatrix(df, plotSize, textSize):\n", " df = df.select_dtypes(include=[np.number])\n", " df = df.dropna(axis='columns') # Changed 'columns' to axis='columns'\n", " df = df[[col for col in df if df[col].nunique() > 1]]\n", " columnNames = list(df)\n", " if len(columnNames) > 10:\n", " columnsNames = columnNames[:10]\n", " df = df[columnNames]\n", " ax = pd.plotting.scatter_matrix(df, alpha=0.75, figsize=[plotSize, plotSize], diagonal=\"kde\")\n", " # Calculate the correlation matrix directly\n", " corrs = df.corr()\n", " for i, j in zip(*plt.np.triu_indices_from(ax, k = 1)):\n", " # Access correlation values using .iloc\n", " ax[i, j].annotate('Corr. coef = %.3f' % corrs.iloc[i, j], (0.8, 0.2), xycoords='axes fraction', ha='center', va='center', size=textSize)\n", " plt.suptitle('Scatter and Density Plot')\n", " plt.show()" ], "metadata": { "id": "DMfXEWWVhViK" }, "execution_count": 70, "outputs": [] }, { "cell_type": "code", "source": [ "nRowsRead = 1000\n", "df1 = pd.read_csv('/content/air_traffic_data.csv', delimiter=',', nrows= nRowsRead)\n", "df1.dataframeName = 'air_traffic_data.csv'\n", "nRow, nCol = df1.shape\n", "print(f'There are {nRow} rows and {nCol} columns')" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ErgTeXE6iaJb", "outputId": "be6adef1-d70a-4ccd-fbb5-c3ce40f63b41" }, "execution_count": 71, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "There are 1000 rows and 16 columns\n" ] } ] }, { "cell_type": "code", "source": [ "\n", "df1.head(5)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 382 }, "id": "FHo8RqO7elj7", "outputId": "a239e470-e859-4623-de69-ac5c76e46ea0" }, "execution_count": 72, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " Activity Period Operating Airline Operating Airline IATA Code \\\n", "0 200507 ATA Airlines TZ \n", "1 200507 ATA Airlines TZ \n", "2 200507 ATA Airlines TZ \n", "3 200507 Air Canada AC \n", "4 200507 Air Canada AC \n", "\n", " Published Airline Published Airline IATA Code GEO Summary GEO Region \\\n", "0 ATA Airlines TZ Domestic US \n", "1 ATA Airlines TZ Domestic US \n", "2 ATA Airlines TZ Domestic US \n", "3 Air Canada AC International Canada \n", "4 Air Canada AC International Canada \n", "\n", " Activity Type Code Price Category Code Terminal Boarding Area \\\n", "0 Deplaned Low Fare Terminal 1 B \n", "1 Enplaned Low Fare Terminal 1 B \n", "2 Thru / Transit Low Fare Terminal 1 B \n", "3 Deplaned Other Terminal 1 B \n", "4 Enplaned Other Terminal 1 B \n", "\n", " Passenger Count Adjusted Activity Type Code Adjusted Passenger Count \\\n", "0 27271 Deplaned 27271 \n", "1 29131 Enplaned 29131 \n", "2 5415 Thru / Transit * 2 10830 \n", "3 35156 Deplaned 35156 \n", "4 34090 Enplaned 34090 \n", "\n", " Year Month \n", "0 2005 July \n", "1 2005 July \n", "2 2005 July \n", "3 2005 July \n", "4 2005 July " ], "text/html": [ "\n", "
| \n", " | Activity Period | \n", "Operating Airline | \n", "Operating Airline IATA Code | \n", "Published Airline | \n", "Published Airline IATA Code | \n", "GEO Summary | \n", "GEO Region | \n", "Activity Type Code | \n", "Price Category Code | \n", "Terminal | \n", "Boarding Area | \n", "Passenger Count | \n", "Adjusted Activity Type Code | \n", "Adjusted Passenger Count | \n", "Year | \n", "Month | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "200507 | \n", "ATA Airlines | \n", "TZ | \n", "ATA Airlines | \n", "TZ | \n", "Domestic | \n", "US | \n", "Deplaned | \n", "Low Fare | \n", "Terminal 1 | \n", "B | \n", "27271 | \n", "Deplaned | \n", "27271 | \n", "2005 | \n", "July | \n", "
| 1 | \n", "200507 | \n", "ATA Airlines | \n", "TZ | \n", "ATA Airlines | \n", "TZ | \n", "Domestic | \n", "US | \n", "Enplaned | \n", "Low Fare | \n", "Terminal 1 | \n", "B | \n", "29131 | \n", "Enplaned | \n", "29131 | \n", "2005 | \n", "July | \n", "
| 2 | \n", "200507 | \n", "ATA Airlines | \n", "TZ | \n", "ATA Airlines | \n", "TZ | \n", "Domestic | \n", "US | \n", "Thru / Transit | \n", "Low Fare | \n", "Terminal 1 | \n", "B | \n", "5415 | \n", "Thru / Transit * 2 | \n", "10830 | \n", "2005 | \n", "July | \n", "
| 3 | \n", "200507 | \n", "Air Canada | \n", "AC | \n", "Air Canada | \n", "AC | \n", "International | \n", "Canada | \n", "Deplaned | \n", "Other | \n", "Terminal 1 | \n", "B | \n", "35156 | \n", "Deplaned | \n", "35156 | \n", "2005 | \n", "July | \n", "
| 4 | \n", "200507 | \n", "Air Canada | \n", "AC | \n", "Air Canada | \n", "AC | \n", "International | \n", "Canada | \n", "Enplaned | \n", "Other | \n", "Terminal 1 | \n", "B | \n", "34090 | \n", "Enplaned | \n", "34090 | \n", "2005 | \n", "July | \n", "