-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
84 lines (67 loc) · 2.55 KB
/
app.py
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
from flask import Flask, render_template, request
import pandas as pd
import pickle
from sklearn.preprocessing import StandardScaler
from werkzeug.utils import secure_filename
import os
app = Flask(__name__)
# Load the dataset
df = pd.read_csv("purchase_history.csv")
# Load the trained model and scaler
with open('knn_model.pickle', 'rb') as f:
knn_model = pickle.load(f)
with open('scaler.pickle', 'rb') as f:
scaler = pickle.load(f)
# Function to make predictions
def predict_purchase(gender, age, salary, price):
# Encode gender
gender_encoded = 1 if gender == 'Male' else 0
# Scale the input features
input_features = scaler.transform([[gender_encoded, age, salary, price]])
# Make prediction
prediction = knn_model.predict(input_features)[0]
return prediction
# Home page route
@app.route('/')
def home():
return render_template('index.html')
# Prediction route
@app.route('/predict', methods=['POST'])
def predict():
if request.method == 'POST':
gender = request.form['gender']
age = float(request.form['age'])
salary = float(request.form['salary'])
price = float(request.form['price'])
prediction = predict_purchase(gender, age, salary, price)
prediction_text = "Likely to purchase" if prediction == 1 else "Not likely to purchase"
return render_template('result.html', prediction_text=prediction_text)
def predict_from_csv(file_path):
df = pd.read_csv(file_path)
predictions = []
for index, row in df.iterrows():
gender = row['Gender']
age = row['Age']
salary = row['Salary']
price = row['Price']
prediction = predict_purchase(gender, age, salary, price)
prediction_text = "Likely to purchase" if prediction == 1 else "Not likely to purchase"
predictions.append(prediction_text)
return predictions
# File upload route
@app.route('/upload', methods=['POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
return 'No file part'
file = request.files['file']
if file.filename == '':
return 'No selected file'
if file:
filename = secure_filename(file.filename)
file.save(os.path.join('uploads', filename))
predictions = predict_from_csv(os.path.join('uploads', filename))
return render_template('result.html', predictions=predictions)
# Function to predict purchases for customers in CSV file
if __name__ == '__main__':
app.run(debug=True)