Spaces:
Sleeping
Sleeping
| 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 | |