-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS_Project_Backend.Rmd
156 lines (125 loc) · 5.16 KB
/
DS_Project_Backend.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
---
title: "DS_Project_Backend"
output: html_document
date: "2023-04-19"
---
```{r, include = TRUE, warning = FALSE, message = FALSE}
library(tidyverse)
library(modelr)
library(lubridate)
library(dplyr)
library(glmnet)
library(caret)
library(pROC)
library(caTools)
library(randomForest)
flight_new = read_csv("Flights Price Prediction Dataset\\Cleaned_dataset.csv", show_col_types = FALSE)
#head(flight_new)
unique(flight_new$Total_stops)
flight_new["Total_stops"][flight_new["Total_stops"]== "non-stop"] <- '0'
flight_new["Total_stops"][flight_new["Total_stops"]== "1-stop"] <- '1'
flight_new["Total_stops"][flight_new["Total_stops"]== "2+-stop"] <- '2'
flight_new$Total_stops = as.numeric(flight_new$Total_stops)
#head(flight_new)
```
##Model Fitting: Lasso Model to new data
```{r, warning = FALSE, message = FALSE, fig.width = 10, fig.height = 3}
#Defining predictor and response variables
head(flight_new)
y = flight_new$Fare
x = model.matrix( ~ ., data =(select(flight_new,-Fare, -Flight_code, -Date_of_journey, -Duration_in_hours)))
#Splitting data
index = sample(nrow(x), floor(0.8 * nrow(x)))
x_train = x[index, ]
y_train = y[index]
x_test = x[-index, ]
y_test = y[-index]
#Fitting the model
model = cv.glmnet(x_train,y_train,alpha =1,folds = 5)
optimal_lambda = model$lambda.min
optimal_model = glmnet(x_train,y_train,alpha =1,lambda = optimal_lambda)
summary(optimal_model)
#Extracting model coefficients
coef(optimal_model)
```
```{r, warning = FALSE, message = FALSE, fig.width = 10, fig.height = 3}
#Extracting pmodel predictions
predicted = predict(optimal_model, x_test)
#Evaluation metrics
auc(y_test, predicted)
postResample(y_test, predicted)
saveRDS(optimal_model, file = "model_file.rds")
```
```{r, warning = FALSE, message = FALSE, fig.width = 10, fig.height = 3}
head(flight_new)
y = flight_new$Fare
x = model.matrix( ~ ., data =(select(flight_new,-Fare, -Flight_code, -Date_of_journey, -Duration_in_hours)))
#Splitting data
index = sample(nrow(x), floor(0.8 * nrow(x)))
x_train = x[index, ]
y_train = y[index]
x_test = x[-index, ]
y_test = y[-index]
head(x_train)
flightPricePredict <- function(Airline, Source, Destination, Flight_date, Days_left, Class, Total_stops, Arrival, Departure){
flight_new = read_csv("Flights Price Prediction Dataset\\Cleaned_dataset.csv", show_col_types = FALSE)
unique(flight_new$Total_stops)
flight_new["Total_stops"][flight_new["Total_stops"]== "non-stop"] <- '0'
flight_new["Total_stops"][flight_new["Total_stops"]== "1-stop"] <- '1'
flight_new["Total_stops"][flight_new["Total_stops"]== "2+-stop"] <- '2'
flight_new$Total_stops = as.numeric(flight_new$Total_stops)
model <- readRDS("model_file_Random_Forest.rds")
Journey_day = strftime(Flight_date,"%A")
flight_inpt = data.frame(Journey_day, Airline, Class, Source, Departure, Total_stops, Arrival, Destination, Days_left)
inp = model.matrix( ~ ., data =rbind(flight_inpt, select(flight_new,-Fare, -Flight_code, -Date_of_journey, -Duration_in_hours)))
temp = predict(model, newdata=inp)
prd = as.numeric(temp[1])
for (i in Days_left:1) {
flight_inpt = data.frame(Journey_day, Airline, Class, Source, Departure, Total_stops, Arrival, Destination, Days_left=i)
inp = model.matrix( ~ ., data =rbind(flight_inpt, select(flight_new,-Fare, -Flight_code, -Date_of_journey, -Duration_in_hours)))
temp_n = predict(model, newdata=inp)
prd_n = as.numeric(temp_n[1])
prd = append(prd, prd_n)
}
rtn = list(rev(prd[-1]), which.min(rev(prd[-1])))
return(rtn)
}
pred = flightPricePredict("SpiceJet", "Kolkata", "Hyderabad", as.Date("1/02/22"), 15, "Economy", 0, "Before 6 AM", "6 AM - 12 PM")
pred
```
```{r, warning = FALSE, message = FALSE, fig.width = 10, fig.height = 3}
Airline = "SpiceJet"
Source = "Kolkata"
Destination = "Hyderabad"
Flight_date = as.Date("1/02/22")
Days_left = 2
Class = "Economy"
Total_stops = 0
Arrival = "Before 6 AM"
Departure = "6 AM - 12 PM"
flight_new = read_csv("Flights Price Prediction Dataset\\Cleaned_dataset.csv", show_col_types = FALSE)
unique(flight_new$Total_stops)
flight_new["Total_stops"][flight_new["Total_stops"]== "non-stop"] <- '0'
flight_new["Total_stops"][flight_new["Total_stops"]== "1-stop"] <- '1'
flight_new["Total_stops"][flight_new["Total_stops"]== "2+-stop"] <- '2'
flight_new$Total_stops = as.numeric(flight_new$Total_stops)
model <- readRDS("model_file_Random_Forest.rds")
Journey_day = strftime(Flight_date,"%A")
flight_inpt = data.frame(Journey_day, Airline, Class, Source, Departure, Total_stops, Arrival, Destination, Days_left)
inp = model.matrix( ~ ., data =rbind(flight_inpt, select(flight_new,-Fare, -Flight_code, -Date_of_journey, -Duration_in_hours)))
temp = predict(model, newdata=inp)
prd = as.numeric(temp[1])
prd
for (i in Days_left:1) {
flight_inpt = data.frame(Journey_day, Airline, Class, Source, Departure, Total_stops, Arrival, Destination, Days_left=i)
inp = model.matrix( ~ ., data =rbind(flight_inpt, select(flight_new,-Fare, -Flight_code, -Date_of_journey, -Duration_in_hours)))
temp_n = predict(model, newdata=inp)
temp_n[1]
prd_n = as.numeric(temp_n[1])
prd_n
prd = append(prd, prd_n)
prd
}
rtn = list(rev(prd[-1]), which.min(rev(prd[-1])))
rtn
```