-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (51 loc) · 2.15 KB
/
Copy pathapp.py
File metadata and controls
59 lines (51 loc) · 2.15 KB
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
import streamlit as st
import pandas as pd
import joblib
# Load saved model and encoder
artifacts = joblib.load("melb_model_artifacts.pkl")
model = artifacts["model"]
encoder = artifacts["encoder"]
feature_names = artifacts["feature_names"]
# Streamlit UI
st.title("🏠 Melbourne House Price Predictor")
st.markdown("Enter property details to get a price prediction:")
# Basic numeric inputs
rooms = st.number_input("Rooms", min_value=1, max_value=10, value=3)
bathroom = st.number_input("Bathroom", min_value=1, max_value=10, value=2)
car = st.number_input("Car Spaces", min_value=0, max_value=10, value=1)
landsize = st.number_input("Land Size (m²)", min_value=0.0, value=200.0)
building_area = st.number_input("Building Area (m²)", min_value=0.0, value=120.0)
# Categorical inputs — values must exist in training data
suburb = st.text_input("Suburb (e.g. 'Kew', 'Richmond', or 'Other')", "Other")
type_ = st.selectbox("Type", ["h", "u", "t"])
method = st.selectbox("Method", ["S", "SP", "PI", "VB"])
sellerg = st.text_input("SellerG (e.g. 'Nelson', 'Other')", "Other")
council = st.text_input("Council Area", "Other")
region = st.text_input("Region Name", "Other")
# Predict button
if st.button("Predict Price"):
# Build a single-row dataframe from inputs
data = {
"Rooms": rooms,
"Bathroom": bathroom,
"Car": car,
"Landsize": landsize,
"BuildingArea": building_area,
"Suburb": suburb,
"Type": type_,
"Method": method,
"SellerG": sellerg,
"CouncilArea": council,
"Regionname": region
}
df = pd.DataFrame([data])
# Match training preprocessing
useful_cats = ['Suburb', 'Type', 'Method', 'SellerG', 'CouncilArea', 'Regionname']
cats_df = df[useful_cats].fillna("Other")
encoded = encoder.transform(cats_df)
encoded_df = pd.DataFrame(encoded, columns=encoder.get_feature_names_out(useful_cats))
numeric_df = df.select_dtypes(include=["int64", "float64"])
X = pd.concat([numeric_df, encoded_df], axis=1).reindex(columns=feature_names, fill_value=0)
# Predict
prediction = model.predict(X)[0]
st.success(f"Estimated Price: ${prediction:,.0f}")