File size: 1,467 Bytes
a4af8a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt

# Title of the web app
st.title("🏠 House Price Predictor using Linear Regression")

# Step 1: Define the dataset
house_size = np.array([500, 1000, 1500, 2000, 2500])  # Feature (x)
house_price = np.array([100000, 200000, 300000, 400000, 500000])  # Target (y)

# Step 2: Compute the best-fit line using the Normal Equation
X = np.c_[np.ones(len(house_size)), house_size]  # Adding bias term (θ0)
theta = np.linalg.inv(X.T @ X) @ X.T @ house_price  # Normal Equation formula
theta_0, theta_1 = theta  # Extract parameters

# Step 3: Hypothesis Function
def hypothesis(x):
    return theta_0 + theta_1 * x

# Streamlit Input - User enters house size
size_input = st.number_input("Enter House Size (sq. ft.)", min_value=100, max_value=5000, step=100, value=1800)

# Step 4: Prediction
predicted_price = hypothesis(size_input)
st.write(f"💰 Predicted House Price: **${predicted_price:,.2f}**")

# Step 5: Visualization
fig, ax = plt.subplots()
ax.scatter(house_size, house_price, color='blue', label='Actual Data')
ax.plot(house_size, hypothesis(house_size), color='red', label='Regression Line')
ax.scatter(size_input, predicted_price, color='green', label="Predicted Price", marker='o', s=100)
ax.set_xlabel("House Size (sq. ft.)")
ax.set_ylabel("House Price ($)")
ax.set_title("Linear Regression: House Size vs. Price")
ax.legend()
ax.grid()

st.pyplot(fig)  # Display plot in Streamlit