Let’s explore these two core types of machine learning in detail and how you can apply them using Python and Scikit-learn.
1️⃣ Supervised Learning
Supervised learning means the model learns from data where both input and the correct output are provided. You "supervise" the model with answers.
For example, you give it house size and price, and it learns to predict the price for a new house.
Key use cases:
• Predicting house prices
• Classifying emails as spam or not
• Recognizing handwritten digits
Supervised learning includes two types:
• Classification – Output is a category (e.g., dog or cat)
• Regression – Output is a number (e.g., price, age)
Example: Classification using Iris dataset
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = RandomForestClassifier()
model.fit(X_train, y_train)
accuracy = model.score(X_test, y_test)
print("Model Accuracy:", accuracy)
Example: Regression using California housing data
from sklearn.linear_model import LinearRegression
from sklearn.datasets import fetch_california_housing
data = fetch_california_housing()
X = data.data
y = data.target
model = LinearRegression()
model.fit(X, y)
prediction = model.predict([X[0]])
print("Predicted price:", prediction)
2️⃣ Unsupervised Learning
In unsupervised learning, you give the model *only inputs*, without telling it what the correct output should be. The model tries to find patterns or groupings on its own.
Key use cases:
• Segmenting customers into groups
• Finding hidden patterns in data
• Reducing high-dimensional data for visualization
Main types:
• Clustering – Group similar items
• Dimensionality Reduction – Simplify data while keeping meaning
Example: Clustering using KMeans
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
X, _ = make_blobs(n_samples=300, centers=3)
kmeans = KMeans(n_clusters=3)
kmeans.fit(X)
plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels_)
plt.title("KMeans Clustering")
plt.show()
Key Differences
In supervised learning:
• You teach the model using examples with answers
• It predicts labels or numbers
• It's used for tasks like price prediction, image recognition
In unsupervised learning:
• You give the model raw data without answers
• It discovers patterns or groups
• It's used for things like customer segmentation
Pro Tip:
Use Scikit-learn’s built-in datasets to explore both types. Try changing the model or parameters and see how outputs change!
💬 Tap ❤️ for more!