-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHousePredModel.py
More file actions
75 lines (61 loc) · 2.14 KB
/
HousePredModel.py
File metadata and controls
75 lines (61 loc) · 2.14 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import numpy as np
import pandas as pd
import streamlit as st
import plotly.express as px
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
def gen_house_data(n_samples=100):
np.random.seed(50)
size = np.maximum(np.random.normal(1800, 500, n_samples), 0)
price = size * 100 + np.random.normal(0, 10000, n_samples)
return pd.DataFrame({'size': size, 'price': price})
def train_model():
df = gen_house_data()
X = df[["size"]]
Y = df[["price"]]
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=42)
model = LinearRegression()
model.fit(X_train, Y_train)
return model
def main():
st.title("House Price Prediction App")
st.write("This app predicts house prices based on their size using a simple linear regression model.")
col1, col2 = st.columns(2)
with col1:
st.subheader("Adjust House Size")
size = st.slider(
"House size (sq ft)",
min_value=500,
max_value=3000,
value=1500,
step=10,
# orientation="vertical" # Makes the slider vertical
)
model = train_model()
pred_price = model.predict([[size]])
df = gen_house_data()
fig = px.scatter(df, x="size", y="price", title="House Size vs Price")
fig.add_scatter(
x=[size],
y=[pred_price[0].item()],
mode='markers',
marker=dict(size=15, color='red'),
name='Prediction'
)
fig.update_layout(
xaxis_title="House Size (sq ft)",
yaxis_title="Price ($)",
title_font=dict(size=20),
title_x=0.5,
template="plotly_white"
)
with col2:
st.subheader("Predicted Price")
st.success(f"Estimated Price: ${pred_price[0].item():,.2f}")
st.subheader("Scatter Plot")
st.plotly_chart(fig)
st.markdown("---")
st.markdown("**Created by [Swarna Sre :)](https://github.com/chanabyte)**")
st.markdown("Source code available on [GitHub](https://github.com/chanabyte/SimpleLinReg/blob/main/HousePredModel.py).")
if __name__ == "__main__":
main()