0% found this document useful (0 votes)
12 views8 pages

Code

The document outlines a web application for predicting reservation cancellations using a trained machine learning model. It includes HTML templates for user input and displaying results, a Flask app for handling requests, and a model training script that employs a Random Forest classifier. The application collects user data through a form, processes it, and provides a prediction based on the trained model.

Uploaded by

sameersp2321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

Code

The document outlines a web application for predicting reservation cancellations using a trained machine learning model. It includes HTML templates for user input and displaying results, a Flask app for handling requests, and a model training script that employs a Random Forest classifier. The application collects user data through a form, processes it, and provides a prediction based on the trained model.

Uploaded by

sameersp2321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Index.

html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reservation Cancellation Prediction</title>
<!-- Bootstrap CSS -->
<link
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
rel="stylesheet">
<!-- Link to your CSS file -->
<link href="{{ url_for('static', filename='style.css') }}" rel="stylesheet">
</head>
<body>
<div class="background">
<div class="container">
<h1 class="text-center">Reservation Cancellation Prediction</h1>
<form action="/predict" method="post" class="mt-4">
<div class="form-group">
<label for="adults">Adults:</label>
<input type="number" name="adults" class="form-control"
required value="{{ request.form.get('adults', '') }}">
</div>
<div class="form-group">
<label for="children">Children:</label>
<input type="number" name="children" class="form-control"
required value="{{ request.form.get('children', '') }}">
</div>
<div class="form-group">
<label for="weekday_nights">Weekday Nights:</label>
<input type="number" name="weekday_nights" class="form-
control" required value="{{ request.form.get('weekday_nights', '') }}">
</div>
<div class="form-group">
<label for="weekend_nights">Weekend Nights:</label>
<input type="number" name="weekend_nights" class="form-
control" required value="{{ request.form.get('weekend_nights', '') }}">
</div>
<div class="form-group">
<label for="lead_time">Lead Time:</label>
<input type="number" name="lead_time" class="form-control"
required value="{{ request.form.get('lead_time', '') }}">
</div>
<div class="form-group">
<label for="previous_cancellations">Previous
Cancellations:</label>
<input type="number" name="previous_cancellations"
class="form-control" required
value="{{ request.form.get('previous_cancellations', '') }}" >
</div>
<div class="form-group">
<label for="special_requests">Special Requests:</label>
<input type="number" name="special_requests" class="form-
control" required value="{{ request.form.get('special_requests', '') }}" >
</div>
<button type="submit" class="btn btn-primary btn-
block">Predict</button>
</form>
</div>
</div>

<!-- Bootstrap JS and dependencies -->


<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></
script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" ></sc
ript>
</body>
</html>

result.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prediction Result</title>
<!-- Bootstrap CSS -->
<link
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
rel="stylesheet">
<!-- Link to your CSS file -->
<link href="{{ url_for('static', filename='style.css') }}" rel="stylesheet">
</head>
<body>
<div class="container text-center">
<h1>Prediction Result</h1>
<div class="mt-4">
{% if result == "Reservation will Cancel" %}
<div class="alert alert-danger" role="alert">
{{ result }}
</div>
{% else %}
<div class="alert alert-success" role="alert">
{{ result }}
</div>
{% endif %}
</div>
<div class="mt-4">
<a href="/" class="btn btn-primary">Go Back</a>
</div>
</div>

<!-- Bootstrap JS and dependencies -->


<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></
script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" ></sc
ript>
</body>
</html>

Style.css:

body {
background-color: #f8f9fa; /* Light background color */
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
height: 110vh;
overflow: auto; /* Ensure no scrollbars if unnecessary */
}

.background {
background-image: url('/static/hotel-background2.jpg'); /* Corrected path */
background-size: cover;
background-position: center;
background-repeat: no-repeat;
height: 120vh; /* Full viewport height */
width: 100vw; /* Full viewport width */
position: absolute;
}

.container {
margin-top: 10px;
padding: 20px;
background-color: rgba(255, 255, 255, 0.9); /* Semi-transparent white */
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
max-width: 600px;
margin-left: auto;
margin-right: auto;
}

.alert {
font-size: 1.2em;
font-weight: bold;
}

.btn-primary {
margin-top: 20px;
}

App.py:
from flask import Flask, render_template, request
import numpy as np
import joblib

app = Flask(__name__)

# Load the trained model


model = joblib.load('reservation_cancellation_model.pkl')

@app.route('/')
def home():
return render_template('index.html')

@app.route('/predict', methods=['POST'])
def predict():
try:
# Collect data from the form
features = [
float(request.form['adults']),
float(request.form['children']),
float(request.form['weekday_nights']),
float(request.form['weekend_nights']),
float(request.form['lead_time']),
float(request.form['previous_cancellations']),
float(request.form['special_requests'])
]
input_data = np.array([features])
prediction = model.predict(input_data)[0]
result = "Reservation will Cancel" if prediction == 1 else "Reservation
will Not Cancel"
return render_template('result.html', result=result)
except Exception as e:
return f"Error: {e}"

if __name__ == "__main__":
app.run(debug=True)

Model_training.py:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score,
f1_score
import joblib

# Load dataset from CSV file


data = pd.read_csv('data/reservations.csv')

# Features and target variable


X = data.drop('is_canceled', axis=1)
y = data['is_canceled']

# Split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)

# Train the Random Forest model


model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)

# Evaluate the model


y_pred = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Precision:", precision_score(y_test, y_pred))
print("Recall:", recall_score(y_test, y_pred))
print("F1 Score:", f1_score(y_test, y_pred))

# Save the trained model


joblib.dump(model, 'reservation_cancellation_model.pkl')

Reservations.csv:

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score,
f1_score
import joblib

# Load dataset from CSV file


data = pd.read_csv('data/reservations.csv')

# Features and target variable


X = data.drop('is_canceled', axis=1)
y = data['is_canceled']

# Split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)

# Train the Random Forest model


model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)

# Evaluate the model


y_pred = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Precision:", precision_score(y_test, y_pred))
print("Recall:", recall_score(y_test, y_pred))
print("F1 Score:", f1_score(y_test, y_pred))

# Save the trained model


joblib.dump(model, 'reservation_cancellation_model.pkl')

You might also like