Loading W Code...
7
Topics
Math + Code
Approach
๐ Indian Context
All examples use Delhi NCR real estate data โ predict flat prices based on Area, Bedrooms, Age of property, and Floor number!
Foundation
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
# Delhi NCR Flat Prices Dataset
# Feature: Area (sqft), Target: Price (Lakhs)
area = np.array([500, 800, 1000, 1200, 1500, 1800, 2000, 2500]).reshape(-1, 1)
price = np.array([25, 40, 52, 60, 78, 95, 108, 135])
# Train the model
model = LinearRegression()
model.fit(area, price)
# Get learned parameters
w = model.coef_[0] # Weight (slope)
b = model.intercept_ # Bias (intercept)
print("=" * 50)
print("๐ LEARNED LINEAR EQUATION")
print("=" * 50)
print(f"ลท = {w:.4f} ร Area + {b:.2f}")
print(f"\nInterpretation:")
print(f"- Every 1 sqft increase โ โน{w*100:.0f} increase in price")
print(f"- Base price (intercept): โน{b:.2f} Lakhs")
# Make prediction
new_flat = np.array([[1800]])
predicted_price = model.predict(new_flat)[0]
print(f"\n๐ Prediction for 1800 sqft flat: โน{predicted_price:.2f} Lakhs")
# Visualize the best-fit line
plt.figure(figsize=(10, 6))
plt.scatter(area, price, color='blue', s=100, label='Actual Data', zorder=5)
plt.plot(area, model.predict(area), color='red', linewidth=2, label=f'Best Fit: ลท = {w:.2f}x + {b:.2f}')
plt.xlabel('Area (sqft)', fontsize=12)
plt.ylabel('Price (Lakhs)', fontsize=12)
plt.title('Linear Regression: Delhi NCR Flat Prices', fontsize=14)
plt.legend()
plt.grid(True, alpha=0.3)
# plt.show()