Skip to content

goodu001/flight_data_1000000

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

flight_data_1000000

muck up data set with flight_data_1000000 and practice python

Imports

import pandas as pd — loads pandas (table/dataframe toolkit) under the alias pd.

import numpy as np — loads NumPy for fast numeric ops.

import matplotlib.pyplot as plt — plotting API from Matplotlib.

import seaborn as sns — higher-level plotting on top of Matplotlib.

import scipy.stats as stats — statistical utilities (e.g., confidence intervals).

import statsmodels.api as sm — statistical models (e.g., logistic regression).

from math import sqrt — the square-root function.

from pathlib import Path — OS-independent file paths.

1. Setup and Configuration

This first part imports all the necessary libraries for data manipulation (pandas, numpy), visualization (matplotlib, seaborn), and statistical modeling (scipy.stats, statsmodels).

It also defines two key configuration variables:

CSV_PATH: The file path to the flight data CSV.

SAMPLE_FOR_MODEL: Sets a limit of 200,000 rows for the modeling steps. This is done to ensure the models train quickly, which is useful for large datasets.

2. Data Loading

This section reads the data from the specified CSV file into a pandas DataFrame, which is the primary data structure used for analysis.

dtype={...}: This argument tells pandas the specific data type for each column in advance. This is more memory-efficient than letting pandas guess the types, especially with large files (low_memory=False also helps with this).

df.set_index("FlightID", inplace=True): Sets the FlightID column as the index of the DataFrame, which can make lookups by flight ID faster.

3. Helper Functions

Two custom functions are defined here to handle complex data cleaning tasks.

to_dt(): This function is designed to combine a date column (e.g., '2025-08-28') and a time column (e.g., '9:30') into a single, proper datetime object (e.g., '2025-08-28 09:30:00'). It cleverly handles times with single-digit hours like '1:00' by padding them to '01:00' first.

parse_uld_counts(): This is a powerful function for feature engineering. It processes the ULD_Details column, which contains text like 'AMJ×4_PMC×9'. The function parses this string and creates new columns for each ULD type (AMJ, PMC) with their corresponding counts (4, 9) as the values.

4. Data Cleaning & Feature Engineering

This is the core data preparation stage where the raw data is transformed into a usable format for analysis.

Datetime Conversion: The to_dt helper function is used to create four new datetime columns for local and UTC departure/arrival times.

Overnight Flight Correction: The code smartly identifies flights that land on the next day (where arrival time is earlier than departure time) and adds one day to the arrival date to fix this.

Feature Creation: Several new, insightful columns are created:

DurationHours: Calculates the flight duration in hours using UTC times, which avoids issues with time zones.

Route: Creates a single Route feature by combining the origin and destination (e.g., 'JFK-LAX').

IsRedEye: A boolean flag that is True for late-night or early-morning flights.

IsDelayed: The target variable for the analysis. It's a boolean flag that is True if the flight Status is 'DELAYED'.

ULD Parsing: The parse_uld_counts function is called, and only the top 8 most frequent ULD types are kept as new columns. This prevents the DataFrame from being cluttered with hundreds of rare ULD columns.

5. Descriptive Statistics & Risk Segmentation

Here, the script performs an initial exploratory analysis.

Basic Summaries: It prints the total number of rows and columns, the mean and standard deviation of Total_ULDs, and the proportion of on-time vs. delayed flights.

Risk Definition: It creates a new segment called IsRisky. A flight is defined as "risky" if it's a red-eye flight AND has a heavy ULD load (in the top 10% of all flights).

Risk Analysis: It then calculates and prints the delay rate for "risky" flights versus "non-risky" flights to see if the defined risk factors are associated with more delays.

6. Visualization & Correlation

This section creates plots to visually explore relationships in the data.

Box Plot: A box plot shows the distribution of the total number of ULDs for each type of aircraft. This helps see if certain aircraft typically carry more ULDs.

Scatter Plots: A grid of scatter plots is created to visualize the relationships between departure hour, flight duration, total ULDs, and flight delays.

Correlation: Alongside the plots, it calculates and prints the statistical correlation between IsDelayed and the other numerical features to quantify the strength of these relationships.

7. Statistical Inference & Modeling

The final part of the script uses statistical models to predict flight delays.

Confidence Interval: It calculates the 95% confidence interval for the overall proportion of delayed flights. This provides a range that likely contains the true delay rate for the entire population of flights, not just this sample.

Simple Logistic Regression: A simple predictive model is built to see if the Total_ULDs alone can predict the likelihood of a flight being delayed (IsDelayed). A bar chart is also generated to show how the delay rate changes across different ULD count groups (deciles).

Multivariate Logistic Regression: A more complex and powerful model is built. This logistic regression model uses multiple predictors (Total_ULDs, DepHourLocal, DurationHours, Aircraft, Route, and IsRedEye) to predict the probability of a delay. Categorical features like Aircraft and Route are converted into numerical dummy variables so the model can process them.

1. การตั้งค่าและกำหนดค่าเริ่มต้น (Setup and Configuration)

ส่วนแรกนี้จะทำการ import ไลบรารีที่จำเป็นทั้งหมดสำหรับการจัดการข้อมูล (pandas, numpy), การแสดงผลข้อมูลเป็นภาพ (matplotlib, seaborn), และการสร้างโมเดลทางสถิติ (scipy.stats, statsmodels)

นอกจากนี้ยังมีการกำหนดค่าตัวแปรที่สำคัญ 2 ตัว:

CSV_PATH: ที่อยู่ของไฟล์ CSV ที่เก็บข้อมูลเที่ยวบิน

SAMPLE_FOR_MODEL: กำหนดจำนวนข้อมูลสูงสุดไว้ที่ 200,000 แถวสำหรับขั้นตอนการสร้างโมเดล เพื่อให้โมเดลสามารถฝึกฝน (train) ได้อย่างรวดเร็ว ซึ่งมีประโยชน์มากเมื่อทำงานกับข้อมูลขนาดใหญ่

2. การโหลดข้อมูล (Data Loading)

ส่วนนี้จะอ่านข้อมูลจากไฟล์ CSV ที่ระบุไว้มาเก็บใน pandas DataFrame ซึ่งเป็นโครงสร้างข้อมูลหลักที่ใช้ในการวิเคราะห์

dtype={...}: อาร์กิวเมนต์นี้ใช้เพื่อบอก pandas ล่วงหน้าว่าแต่ละคอลัมน์มีชนิดข้อมูล (data type) เป็นอะไร ซึ่งจะช่วยให้ใช้หน่วยความจำได้อย่างมีประสิทธิภาพมากกว่าการให้ pandas เดาชนิดข้อมูลเอง โดยเฉพาะกับไฟล์ขนาดใหญ่

df.set_index("FlightID", inplace=True): กำหนดให้คอลัมน์ FlightID เป็นดัชนี (index) ของ DataFrame ซึ่งจะช่วยให้การค้นหาข้อมูลด้วย ID ของเที่ยวบินทำได้เร็วขึ้น

3. ฟังก์ชันตัวช่วย (Helper Functions)

มีการสร้างฟังก์ชันขึ้นมาเอง 2 ฟังก์ชันเพื่อจัดการกับงานทำความสะอาดข้อมูลที่ซับซ้อน

to_dt(): ฟังก์ชันนี้ถูกออกแบบมาเพื่อรวมคอลัมน์วันที่ (เช่น '2025-08-28') และคอลัมน์เวลา (เช่น '9:30') ให้กลายเป็น อ็อบเจกต์วันและเวลา (datetime object) ที่สมบูรณ์ (เช่น '2025-08-28 09:30:00')

parse_uld_counts(): เป็นฟังก์ชันที่ทรงพลังสำหรับ การสร้างฟีเจอร์ (feature engineering) โดยจะประมวลผลคอลัมน์ ULD_Details ซึ่งมีข้อความอย่าง 'AMJ×4_PMC×9' ฟังก์ชันนี้จะแยกวิเคราะห์ข้อความดังกล่าวและสร้างคอลัมน์ใหม่สำหรับ ULD แต่ละประเภท (AMJ, PMC) พร้อมกับจำนวนของมัน (4, 9) เป็นค่าในคอลัมน์นั้นๆ

4. การทำความสะอาดข้อมูลและการสร้างฟีเจอร์ (Data Cleaning & Feature Engineering)

นี่คือขั้นตอนหลักในการเตรียมข้อมูล โดยเป็นการแปลงข้อมูลดิบให้อยู่ในรูปแบบที่พร้อมใช้งานสำหรับการวิเคราะห์

การแปลงเป็น Datetime: ใช้ฟังก์ชัน to_dt ที่สร้างไว้เพื่อสร้างคอลัมน์วันและเวลาใหม่ 4 คอลัมน์ สำหรับเวลาออกเดินทาง/ถึงที่หมาย ทั้งในรูปแบบเวลาท้องถิ่นและเวลาสากล (UTC)

การแก้ไขเที่ยวบินข้ามคืน: โค้ดจะตรวจสอบอย่างชาญฉลาดว่าเที่ยวบินใดที่ลงจอดในวันถัดไป (กรณีที่เวลาถึงที่หมายเร็วกว่าเวลาออกเดินทาง) และบวกเพิ่ม 1 วันเข้าไปในวันที่เดินทางถึงเพื่อแก้ไขให้ถูกต้อง

การสร้างฟีเจอร์ใหม่: มีการสร้างคอลัมน์ใหม่ๆ ที่ให้ข้อมูลเชิงลึกขึ้นมาหลายคอลัมน์:

DurationHours: คำนวณระยะเวลาของเที่ยวบินเป็นหน่วยชั่วโมง โดยใช้เวลา UTC เพื่อหลีกเลี่ยงปัญหาเกี่ยวกับเขตเวลา (time zone)

Route: สร้างฟีเจอร์ Route ขึ้นมาโดยการรวมต้นทางและปลายทางเข้าด้วยกัน (เช่น 'JFK-LAX')

IsRedEye: เป็นค่าตรรกะ (boolean) ที่จะเป็น True สำหรับเที่ยวบินรอบดึกหรือเช้ามืด

IsDelayed: ตัวแปรเป้าหมาย (target variable) สำหรับการวิเคราะห์นี้ เป็นค่าตรรกะที่จะเป็น True หาก Status ของเที่ยวบินคือ 'DELAYED'

การแยกวิเคราะห์ ULD: เรียกใช้ฟังก์ชัน parse_uld_counts และเก็บไว้เฉพาะ ULD 8 ประเภทที่พบบ่อยที่สุด มาสร้างเป็นคอลัมน์ใหม่ เพื่อป้องกันไม่ให้ DataFrame มีคอลัมน์ของ ULD ที่พบน้อยเกินไปจนรก

5. สถิติเชิงพรรณนาและการแบ่งกลุ่มความเสี่ยง (Descriptive Statistics & Risk Segmentation)

ในส่วนนี้ สคริปต์จะทำการวิเคราะห์ข้อมูลเบื้องต้นเพื่อสำรวจภาพรวม

สรุปข้อมูลพื้นฐาน: พิมพ์ข้อมูลสรุป เช่น จำนวนแถวและคอลัมน์ทั้งหมด, ค่าเฉลี่ยและส่วนเบี่ยงเบนมาตรฐานของ Total_ULDs, และสัดส่วนของเที่ยวบินที่ตรงเวลาเทียบกับที่ล่าช้า

การนิยามความเสี่ยง: สร้างกลุ่มข้อมูลใหม่ที่เรียกว่า IsRisky โดยเที่ยวบินจะถูกนิยามว่า "มีความเสี่ยง" หากเป็น เที่ยวบินรอบดึก (red-eye) และมี จำนวน ULD ที่หนัก (อยู่ในกลุ่ม 10% แรกของเที่ยวบินทั้งหมด)

การวิเคราะห์ความเสี่ยง: จากนั้นจะคำนวณและเปรียบเทียบอัตราการดีเลย์ระหว่างเที่ยวบินที่ "มีความเสี่ยง" กับ "ไม่มีความเสี่ยง" เพื่อดูว่าปัจจัยความเสี่ยงที่กำหนดขึ้นนั้นสัมพันธ์กับการดีเลย์ที่เพิ่มขึ้นหรือไม่

6. การแสดงผลข้อมูลและความสัมพันธ์ (Visualization & Correlation)

ส่วนนี้จะสร้างกราฟเพื่อสำรวจความสัมพันธ์ต่างๆ ในข้อมูลด้วยสายตา

แผนภาพกล่อง (Box Plot): สร้าง Box plot เพื่อแสดงการกระจายของจำนวน ULD ทั้งหมดสำหรับเครื่องบินแต่ละประเภท ซึ่งช่วยให้เห็นว่าเครื่องบินบางประเภทมักจะบรรทุก ULD มากกว่าประเภทอื่นหรือไม่

แผนภาพการกระจาย (Scatter Plots): สร้างกลุ่มของ Scatter plot เพื่อแสดงความสัมพันธ์ระหว่างชั่วโมงที่ออกเดินทาง, ระยะเวลาบิน, จำนวน ULD ทั้งหมด และการดีเลย์ของเที่ยวบิน

สหสัมพันธ์ (Correlation): ควบคู่ไปกับการสร้างกราฟ โค้ดจะคำนวณและพิมพ์ค่า สหสัมพันธ์ ทางสถิติระหว่าง IsDelayed กับฟีเจอร์ตัวเลขอื่นๆ เพื่อวัดความแข็งแกร่งของความสัมพันธ์เหล่านี้

7. การอนุมานทางสถิติและการสร้างโมเดล (Statistical Inference & Modeling)

ส่วนสุดท้ายของสคริปต์จะใช้โมเดลทางสถิติเพื่อทำนายการดีเลย์ของเที่ยวบิน

ช่วงความเชื่อมั่น (Confidence Interval): คำนวณ ช่วงความเชื่อมั่น 95% ของสัดส่วนเที่ยวบินที่ล่าช้าทั้งหมด ซึ่งจะให้ช่วงของค่าที่น่าจะเป็นไปได้สำหรับอัตราการดีเลย์ที่แท้จริงของประชากรเที่ยวบินทั้งหมด

Simple Logistic Regression: สร้างโมเดลทำนายอย่างง่ายเพื่อดูว่า Total_ULDs เพียงอย่างเดียวสามารถทำนายโอกาสที่เที่ยวบินจะล่าช้าได้หรือไม่ นอกจากนี้ยังมีการสร้างกราฟแท่งเพื่อแสดงให้เห็นว่าอัตราการดีเลย์เปลี่ยนแปลงไปตามกลุ่มของจำนวน ULD ที่แตกต่างกันอย่างไร

Multivariate Logistic Regression: สร้างโมเดลที่ซับซ้อนและทรงพลังมากขึ้น โมเดล การถดถอยโลจิสติก (logistic regression) นี้ใช้ตัวทำนายหลายตัว (Total_ULDs, DepHourLocal, DurationHours, Aircraft, Route และ IsRedEye) เพื่อทำนายความน่าจะเป็นของการดีเลย์ โดยฟีเจอร์ที่เป็นหมวดหมู่ (categorical) เช่น Aircraft และ Route จะถูกแปลงเป็นตัวเลขในรูปแบบของ ตัวแปรดัมมี่ (dummy variables) เพื่อให้โมเดลสามารถประมวลผลได้

About

muck up data set with flight_data_1000000 and practice python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors