Loading W Code...
Understand the fundamentals: What is ML, how it works, types of learning, and real-world applications you use every day.
4
Topics
Python
Examples
# TRADITIONAL PROGRAMMING
# You manually write all the rules
def is_hot(temperature):
if temperature > 30:
return "Hot"
elif temperature > 20:
return "Warm"
else:
return "Cold"
# MACHINE LEARNING APPROACH
# The computer learns the rules from EXAMPLES
from sklearn.tree import DecisionTreeClassifier
# Training data: (temperature, label)
temperatures = [[35], [28], [15], [40], [10], [25]]
labels = ["Hot", "Warm", "Cold", "Hot", "Cold", "Warm"]
# Model learns the pattern automatically
model = DecisionTreeClassifier()
model.fit(temperatures, labels)
# Now it can predict for new data
print(model.predict([[32]])) # Output: "Hot"
# Complete ML Pipeline Example
# Problem: Predict house prices based on size
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error
# Step 2: Load Data
data = {
'size_sqft': [1000, 1500, 2000, 2500, 3000],
'price_lakhs': [50, 75, 100, 125, 150]
}
df = pd.DataFrame(data)
# Step 3: Prepare Data (split into train/test)
X = df[['size_sqft']] # Features
y = df['price_lakhs'] # Target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Step 4 & 5: Choose Model and Train
model = LinearRegression()
model.fit(X_train, y_train)
# Step 6: Evaluate
predictions = model.predict(X_test)
error = mean_absolute_error(y_test, predictions)
print(f"Average Error: ₹{error} Lakhs")
# Step 7: Use for new predictions
new_house = [[1800]]
predicted_price = model.predict(new_house)
print(f"Predicted Price for 1800 sqft: ₹{predicted_price[0]:.2f} Lakhs")# SUPERVISED LEARNING EXAMPLE
# Classification: Is this email spam?
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer
emails = ["Free money now!", "Meeting at 3pm", "You won a lottery!", "Project update"]
labels = ["spam", "not_spam", "spam", "not_spam"]
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(emails)
model = MultinomialNB()
model.fit(X, labels)
# Predict new email
new_email = vectorizer.transform(["Claim your free prize today!"])
print(model.predict(new_email)) # Output: ['spam']
# UNSUPERVISED LEARNING EXAMPLE
# Clustering: Group customers by behavior
from sklearn.cluster import KMeans
import numpy as np
# Customer data: [age, annual_spend_in_thousands]
customers = np.array([[25, 50], [30, 55], [45, 120], [50, 130], [22, 45]])
kmeans = KMeans(n_clusters=2)
kmeans.fit(customers)
print(kmeans.labels_) # Output: [0, 0, 1, 1, 0]
# Cluster 0: Young, low spenders | Cluster 1: Older, high spenders# RECOMMENDATION SYSTEM (Simplified)
# "Users who liked X also liked Y"
import numpy as np
# User-Item ratings matrix (0 = not rated)
# Rows: Users, Columns: Movies (Inception, Avengers, Titanic)
ratings = np.array([
[5, 4, 0], # User 1: Loves action
[4, 5, 2], # User 2: Loves action, dislikes romance
[0, 0, 5], # User 3: Loves romance
[5, 0, 0], # User 4: Only rated Inception
])
# For User 4 who only rated Inception (5 stars):
# Find similar users → User 1 also gave Inception 5 stars
# User 1 liked Avengers (4 stars) → Recommend Avengers to User 4
from sklearn.metrics.pairwise import cosine_similarity
user_4 = ratings[3].reshape(1, -1)
similarities = cosine_similarity(user_4, ratings)
print(f"User 4 is most similar to: User {similarities.argmax() + 1}")
# Output: User 4 is most similar to: User 1
# Recommendation: Avengers (since User 1 rated it 4/5)Now that you understand the basics, dive deeper into the algorithms: