Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,23 @@ import joblib
|
|
5 |
# Load your Random Forest model
|
6 |
loaded_model = joblib.load('random_forest_model.pkl')
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
# Function to make predictions
|
9 |
def predict(host_id, neighbourhood_group, neighbourhood, room_type, latitude, longitude, number_of_reviews, calculated_host_listings_count):
|
10 |
# Prepare input data as DataFrame
|
@@ -21,9 +38,9 @@ def predict(host_id, neighbourhood_group, neighbourhood, room_type, latitude, lo
|
|
21 |
|
22 |
# One-hot encode the categorical features
|
23 |
input_data = pd.get_dummies(input_data, columns=['room_type', 'neighbourhood_group', 'neighbourhood'], drop_first=True)
|
24 |
-
|
25 |
# Ensure the input data has the same columns as the training data
|
26 |
-
input_data = input_data.reindex(columns=
|
27 |
|
28 |
# Make the prediction
|
29 |
predicted_price = loaded_model.predict(input_data)
|
@@ -34,18 +51,16 @@ iface = gr.Interface(
|
|
34 |
fn=predict,
|
35 |
inputs=[
|
36 |
gr.Number(label="Host ID"),
|
37 |
-
gr.Dropdown(
|
38 |
-
gr.Dropdown([
|
39 |
-
gr.Dropdown([
|
40 |
gr.Number(label="Latitude"),
|
41 |
gr.Number(label="Longitude"),
|
42 |
gr.Number(label="Number of Reviews"),
|
43 |
-
gr.Number(label="Calculated Host Listings Count")
|
44 |
],
|
45 |
outputs="number",
|
46 |
-
title="NYC Rental Price Prediction",
|
47 |
-
description="Predict the rental price of an Airbnb listing in NYC."
|
48 |
)
|
49 |
|
50 |
-
# Launch the
|
51 |
iface.launch()
|
|
|
5 |
# Load your Random Forest model
|
6 |
loaded_model = joblib.load('random_forest_model.pkl')
|
7 |
|
8 |
+
# Define the expected feature columns
|
9 |
+
feature_columns = [
|
10 |
+
'host_id',
|
11 |
+
'latitude',
|
12 |
+
'longitude',
|
13 |
+
'number_of_reviews',
|
14 |
+
'calculated_host_listings_count',
|
15 |
+
'room_type_Entire home/apt',
|
16 |
+
'room_type_Private room',
|
17 |
+
'room_type_Shared room',
|
18 |
+
'neighbourhood_group_Bronx',
|
19 |
+
'neighbourhood_group_Brooklyn',
|
20 |
+
'neighbourhood_group_Manhattan',
|
21 |
+
'neighbourhood_group_Queens',
|
22 |
+
'neighbourhood_group_Staten Island',
|
23 |
+
]
|
24 |
+
|
25 |
# Function to make predictions
|
26 |
def predict(host_id, neighbourhood_group, neighbourhood, room_type, latitude, longitude, number_of_reviews, calculated_host_listings_count):
|
27 |
# Prepare input data as DataFrame
|
|
|
38 |
|
39 |
# One-hot encode the categorical features
|
40 |
input_data = pd.get_dummies(input_data, columns=['room_type', 'neighbourhood_group', 'neighbourhood'], drop_first=True)
|
41 |
+
|
42 |
# Ensure the input data has the same columns as the training data
|
43 |
+
input_data = input_data.reindex(columns=feature_columns, fill_value=0)
|
44 |
|
45 |
# Make the prediction
|
46 |
predicted_price = loaded_model.predict(input_data)
|
|
|
51 |
fn=predict,
|
52 |
inputs=[
|
53 |
gr.Number(label="Host ID"),
|
54 |
+
gr.Dropdown(label="Neighbourhood Group", choices=['Bronx', 'Brooklyn', 'Manhattan', 'Queens', 'Staten Island']),
|
55 |
+
gr.Dropdown(label="Neighbourhood", choices=['Chelsea', 'Flatiron District', 'Upper West Side', 'East Village', '...']),
|
56 |
+
gr.Dropdown(label="Room Type", choices=['Entire home/apt', 'Private room', 'Shared room']),
|
57 |
gr.Number(label="Latitude"),
|
58 |
gr.Number(label="Longitude"),
|
59 |
gr.Number(label="Number of Reviews"),
|
60 |
+
gr.Number(label="Calculated Host Listings Count"),
|
61 |
],
|
62 |
outputs="number",
|
|
|
|
|
63 |
)
|
64 |
|
65 |
+
# Launch the Gradio app
|
66 |
iface.launch()
|