ML Fundamentals
scikit-learn, train/test split, linear regression — predict house prices from scratch.
MagicBricks and 99acres don't have humans pricing every listing. A regression model looks at area, location, bedrooms, age — and outputs a price estimate in milliseconds.
The same math Gauss used in 1809 to predict asteroid orbits. Today it prices 10 million homes per day. That's ML in production.
📈 The ML Workflow (5–25 min)
1. Load data — CSV, database, API.
2. Split — 80% train, 20% test. Never evaluate on training data.
3. Train — model.fit(X_train, y_train).
4. Evaluate — RMSE, R² on the test set.
5. Predict — model.predict(new_data).
A model that memorises training data but fails on new data. Like a student who copies answers for last year's exam but can't solve new problems. Always check test score vs train score.
💻 Live Demo — House Price Predictor (25–45 min)
>import numpy as np import pandas as pd from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error, r2_score # Synthetic house price dataset np.random.seed(42) n = 200 area = np.random.randint(600, 3000, n) # sq ft rooms = np.random.randint(1, 6, n) age = np.random.randint(0, 30, n) # years price = 2000*area + 50000*rooms - 3000*age + np.random.normal(0, 80000, n) df = pd.DataFrame({"area": area, "rooms": rooms, "age": age, "price": price}) # 1. Split X = df[["area", "rooms", "age"]] y = df["price"] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 2. Train model = LinearRegression() model.fit(X_train, y_train) # 3. Evaluate y_pred = model.predict(X_test) rmse = mean_squared_error(y_test, y_pred) ** 0.5 r2 = r2_score(y_test, y_pred) print(f"RMSE: ₹{rmse:,.0f} R²: {r2:.3f}" ) # R² close to 1.0 is good # 4. Predict a new house new_house = [[1800, 3, 5]] # 1800 sqft, 3 rooms, 5 yr old est_price = model.predict(new_house)[0] print(f"Estimated price: ₹{est_price:,.0f}" )
📋 You Code It (45–55 min)
Add a location_score feature (1–5) to the dataset. Re-train. Does R² improve? Print the model's coefficients with model.coef_ to see which feature matters most.
⚡ Day 5 Cheat Sheet
train_test_split(X, y, test_size=0.2)model.fit(X_train, y_train)model.predict(X_test)r2_score(y_test, y_pred)mean_squared_error(...)**0.5model.coef_model.intercept_StandardScaler().fit_transform(X)