Welcome to Day 12 of WisdomAcademyAI, where we’re predicting numbers with the magic of Linear Regression! I’m Anastasia, your super thrilled AI guide, and today we’ll explore the basics of Linear Regression—a powerful ML technique to forecast numerical values like house prices. Sophia joins me with a magical demo using Python and scikit-learn to predict house prices based on size—it’s spellbinding! Whether you’re new to AI or following along from Days 1–11, this 27-minute lesson will ignite your curiosity. Let’s make AI magic together!
Task of the Day: Build a Linear Regression model using Python (like in the demo) and share your R-squared in the comments! Let’s see your magical results!
Subscribe for Daily Lessons: Don’t miss Day 13, where we’ll explore Logistic Regression Basics. Hit the bell to stay updated!
#AIForBeginners #LinearRegression #MachineLearning #WisdomAcademyAI #PythonDemo #ScikitLearnDemo
Generate house_prices.csv:
import pandas as pd
import numpy as np
#Set a random seed for reproducibility
np.random.seed(42)
#Generate data for 100 houses
num_rows = 100
#Size: 800-3000 square feet
size = np.random.randint(800, 3001, size=num_rows)
#Price: Linear relationship with size (price = 200 * size + 50000 + noise)
noise = np.random.normal(0, 20000, size=num_rows)
price = 200 * size + 50000 + noise
#Create DataFrame
df = pd.DataFrame({
'size': size,
'price': price
})
#Save to CSV
df.to_csv("house_prices.csv", index=False)
print("Generated house_prices.csv with 100 rows!")
Linear Regression Script:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
#Step 1: Load the dataset
df = pd.read_csv("house_prices.csv")
print("Original Dataset:")
print(df.head())
#Step 2: Prepare the data
X = df[['size']]
y = df['price']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
#Step 3: Train Linear Regression
model = LinearRegression()
model.fit(X_train, y_train)
#Step 4: Predict and evaluate
y_pred = model.predict(X_test)
r2 = r2_score(y_test, y_pred)
print("\nR-squared:", r2)