You are viewing a single comment's thread from:
RE: Payment for Coding done
Machine Learning and Predictive Analysis: Advanced users may employ machine learning models to predict future flight prices based on historical data. This can be useful for creating price forecasting tools or applications.
In this script:
We start by converting the flight data into a pandas DataFrame and extracting day and month features from the date.
We split the data into training and testing sets.
We create a simple linear regression model using scikit-learn and train it on the training data.
We evaluate the model's performance on the test data using Mean Squared Error (MSE) and R-squared (R2) score.
Finally, we make a prediction for a specific date using the trained model.
This is a basic example, and in practice, you would likely need to work with larger datasets, consider additional features, and explore more advanced machine learning algorithms for better prediction accuracy. Additionally, you can implement time series forecasting techniques to handle the temporal aspect of flight price data more effectively.
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
Predict the price for a specific date
date_to_predict = pd.to_datetime("2023-09-04")
day_to_predict = date_to_predict.day
month_to_predict = date_to_predict.month
predicted_price = model.predict([[day_to_predict, month_to_predict]])
print(f'Predicted price for {date_to_predict}: ${predicted_price[0]:.2f}')
Sample flight price data (You can replace this with your actual data)
flight_data = [
{"airline": "Airline A", "route": "Route 1", "date": "2023-09-01", "price": 500},
{"airline": "Airline A", "route": "Route 1", "date": "2023-09-02", "price": 480},
{"airline": "Airline A", "route": "Route 1", "date": "2023-09-03", "price": 520},
{"airline": "Airline B", "route": "Route 1", "date": "2023-09-01", "price": 450},
{"airline": "Airline B", "route": "Route 1", "date": "2023-09-02", "price": 430},
{"airline": "Airline B", "route": "Route 1", "date": "2023-09-03", "price": 460},
# Add more flight data here...
]
Split the data into training and testing sets
X = df[['day', 'month']]
y = df['price']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Convert the flight data to a pandas DataFrame
df = pd.DataFrame(flight_data)
Feature engineering: Extract day and month from the date
df['day'] = pd.to_datetime(df['date']).dt.day
df['month'] = pd.to_datetime(df['date']).dt.month
Create and train a linear regression model
model = LinearRegression()
model.fit(X_train, y_train)
Make predictions on the test set
y_pred = model.predict(X_test)
Evaluate the model
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f'Mean Squared Error: {mse:.2f}')
print(f'R-squared (R2) Score: {r2:.2f}')