-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
105 lines (80 loc) · 3.57 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from flask import Flask, render_template, request, jsonify
import pandas as pd
import pickle
app = Flask(__name__)
data = pd.read_csv('final_dataset.csv')
pipe = pickle.load(open("RidgeModel.pkl", 'rb'))
@app.route('/')
def index():
bedrooms = sorted(data['beds'].unique())
bathrooms = sorted(data['baths'].unique())
sizes = sorted(data['size'].unique())
zip_codes = sorted(data['zip_code'].unique())
return render_template('index.html', bedrooms=bedrooms, bathrooms=bathrooms, sizes=sizes, zip_codes=zip_codes)
@app.route('/predict', methods=['POST'])
def predict():
bedrooms = request.form.get('beds')
bathrooms = request.form.get('baths')
size = request.form.get('size')
zipcode = request.form.get('zip_code')
# Create a DataFrame with the input data
input_data = pd.DataFrame([[bedrooms, bathrooms, size, zipcode]],
columns=['beds', 'baths', 'size', 'zip_code'])
print("Input Data:")
print(input_data)
# Handle unknown categories in the input data
for column in input_data.columns:
unknown_categories = set(input_data[column]) - set(data[column].unique())
if unknown_categories:
# Handle unknown categories (e.g., replace with a default value)
input_data[column] = input_data[column].replace(unknown_categories, data[column].mode()[0])
print("Processed Input Data:")
print(input_data)
# Predict the price
prediction = pipe.predict(input_data)[0]
return str(prediction)
from flask import Flask, render_template, request
import pandas as pd
import pickle
app = Flask(__name__)
data = pd.read_csv('final_dataset.csv')
bedrooms = [1, 2, 3, 4]
pipe = pickle.load(open("RidgeModel.pkl", 'rb'))
@app.route('/')
def index():
bedrooms = sorted(map(str, data['beds'].unique()))
bathrooms = sorted(map(str, data['baths'].unique()))
sizes = sorted(map(str, data['size'].unique()))
zip_codes = sorted(map(str, data['zip_code'].unique()))
return render_template('index.html', bedrooms=bedrooms, bathrooms=bathrooms, sizes=sizes, zip_codes=zip_codes)
@app.route('/predict', methods=['POST'])
def predict():
bedrooms = request.form.get('beds')
bathrooms = request.form.get('baths')
size = request.form.get('size')
zipcode = request.form.get('zip_code')
# Create a DataFrame with the input data
input_data = pd.DataFrame([[bedrooms, bathrooms, size, zipcode]],
columns=['beds', 'baths', 'size', 'zip_code'])
print("Input Data:")
print(input_data)
# Convert 'baths' column to numeric with errors='coerce'
input_data['baths'] = pd.to_numeric(input_data['baths'], errors='coerce')
# Convert input data to numeric types
input_data = input_data.astype({'beds': int, 'baths': float, 'size': float, 'zip_code': int})
# Convert 'size' column to numeric
input_data['size'] = pd.to_numeric(input_data['size'], errors='coerce')
# Handle unknown categories in the input data
for column in input_data.columns:
unknown_categories = set(input_data[column]) - set(data[column].unique())
if unknown_categories:
print(f"Unknown categories in {column}: {unknown_categories}")
# Handle unknown categories (e.g., replace with a default value)
input_data[column] = input_data[column].replace(unknown_categories, data[column].mode()[0])
print("Processed Input Data:")
print(input_data)
# Predict the price
prediction = pipe.predict(input_data)[0]
return str(prediction)
if __name__ == "__main__":
app.run(debug=True, port=5000)