EricoR/Indian_Airplane_Price_Prediction
0
1import streamlit as st2import pandas as pd3import numpy as np4import pickle5import json6 7def run():8 st.title('Flight Data Prediction')9 10 # Project Summary Section11 st.write('### Summary of Flight Analysis')12 st.write(13 """14 This flight data analysis covers flight volume, popular routes, and the factors that influence ticket prices.15 16 **Flight Volume and Popular Routes:**17 * **Vistara** is the airline with the highest number of flights, followed by **Air India** and **Indigo**.18 * **Delhi** and **Mumbai** are the main flight hubs, with the highest volume of flights to and from other major cities like **Bangalore**.19 * The highest number of departures occurs in the morning.20 """21 )22 st.markdown('---')23 24 st.write('### Factors Influencing Ticket Prices')25 st.write(26 """27 Flight ticket prices are not static and are influenced by several key factors:28 * **Airline Type:** Airlines can be grouped into two pricing categories:29 * **Low-Cost Carriers:** Such as **AirAsia**, **Indigo**, and **GO_FIRST**, have relatively stable and affordable prices.30 * **Full-Service Carriers:** Such as **Vistara** and **Air India**, have significantly higher prices and a more varied price range.31 * **Departure and Arrival Times:** Ticket prices vary depending on the combination of departure and arrival times.32 * **Distance Traveled:** Routes with longer distances, which require more fuel, tend to have higher ticket prices.33 * **Time of Booking:** The closer the purchase date is to the departure date, the more ticket prices tend to increase.34 """35 )36 st.markdown('---')37 38 st.write('### Model Selection and Performance')39 st.write(40 """41 To predict ticket prices, a variety of regression models were evaluated. Simple linear models like Linear Regression and Ridge Regression performed poorly due to the data's complex, non-linear nature. Advanced tree-based models, particularly **boosting models** like **XGBoost** and **LightGBM**, were far more effective at capturing these complex patterns.42 43 After comparing the models, **XGBoost** was selected as the best choice. Hyperparameter tuning was performed on the model to optimize its performance, resulting in the following final metrics:44 * **Mean R² Score: 0.9787**45 This indicates that the model can explain approximately **98%** of the variance in ticket prices, signifying a very high level of accuracy.46 * **Mean Negative MSE: -10,948,750**47 This score shows that the model has a very low average prediction error, making it a reliable tool for forecasting airline ticket prices.48 """49 )50 st.markdown('---')51 52 # Load model53 with open('src/ModelXGB.pkl', 'rb') as file_1:54 model = pickle.load(file_1)55 st.success("Model loaded successfully!")56 57 st.write('## Input Data')58 with st.form(key='data'):59 airlines = ['SpiceJet', 'AirAsia', 'Vistara', 'IndiGo', 'Akasa Air']60 flight_numbers = [f'SG-{np.random.randint(1000, 9999)}' for _ in range(5)] + \61 [f'I5-{np.random.randint(100, 999)}' for _ in range(5)] + \62 [f'UK-{np.random.randint(900, 999)}' for _ in range(5)] + \63 [f'6E-{np.random.randint(100, 999)}' for _ in range(5)] + \64 [f'QP-{np.random.randint(100, 999)}' for _ in range(5)]65 source_cities = ['Delhi', 'Mumbai', 'Bangalore', 'Kolkata', 'Chennai']66 destination_cities = ['Mumbai', 'Delhi', 'Bangalore', 'Kolkata', 'Chennai']67 departure_times = ['Early_Morning', 'Morning', 'Afternoon', 'Evening', 'Night']68 arrival_times = ['Early_Morning', 'Morning', 'Afternoon', 'Evening', 'Night']69 stops = ['zero', 'one', 'two_or_more']70 classes = ['Economy', 'Business']71 72 airline = st.selectbox('Airline', airlines)73 flight_number = st.selectbox('Flight Number', flight_numbers)74 source_city = st.selectbox('Source City', source_cities)75 destination_city = st.selectbox('Destination City', destination_cities)76 departure_time = st.selectbox('Departure Time', departure_times)77 arrival_time = st.selectbox('Arrival Time', arrival_times)78 stops = st.selectbox('Stops', stops)79 flight_class = st.selectbox('Class', classes)80 duration = st.number_input('Duration (hours)', min_value=1.5, max_value=4.0, step=0.1)81 days_left = st.number_input('Days Left for Departure', min_value=1, max_value=60)82 83 # Submit button84 submit = st.form_submit_button('Predict')85 86 if submit:87 data = {88 'airline': airline,89 'flight': flight_number,90 'source_city': source_city,91 'destination_city': destination_city,92 'departure_time': departure_time,93 'arrival_time': arrival_time,94 'stops': stops,95 'class': flight_class,96 'duration': duration,97 'days_left': days_left,98 }99 data = pd.DataFrame([data])100 st.dataframe(data)101 102 # Make prediction103 prediction = model.predict(data)104 st.write(f'Prediction: {prediction[0]}')105 106# Call the run function to execute the Streamlit app107if __name__ == "__main__":108 run()