-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_regression.py
More file actions
31 lines (24 loc) · 936 Bytes
/
linear_regression.py
File metadata and controls
31 lines (24 loc) · 936 Bytes
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
"""A simple implementation of linear regrassion model
with visualization using matplotlib"""
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
# Sample Data: House Size (in square feet) and Price (in thousands)
# Features (House Size)
X = np.array([500, 700, 800, 1000, 1200, 1500]).reshape(-1, 1)
# Target (House Price)
y = np.array([150, 200, 220, 260, 300, 360])
# Create and Train the Model
model = LinearRegression()
model.fit(X, y)
# Make a Prediction
new_size = np.array([[1100]]) # Predict price for 1100 sq ft house
predicted_price = model.predict(new_size)
print(f"Predicted Price for 1100 sq ft: {predicted_price[0]:.2f}K")
# Plot the Data and Regression Line
plt.scatter(X, y, color="blue", label="Actual Data")
plt.plot(X, model.predict(X), color="red", label="Regression Line")
plt.xlabel("House Size (sq ft)")
plt.ylabel("Price (K)")
plt.legend()
plt.show()