Skip to main content
Practice

Types of Machine Learning: Supervised vs Unsupervised


Machine learning can be broadly divided into:

  • Supervised Learning – Models learn from labeled data to make predictions.
  • Unsupervised Learning – Models find patterns in unlabeled data.

For the full conceptual breakdown, refer to the Slide Deck for this lesson.
Here, we’ll focus on practical examples in Scikit-learn.


Supervised Learning Example – Classification

K-Nearest Neighbors Classification
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

# Load dataset
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(
iris.data, iris.target, test_size=0.2, random_state=42
)

# Create and train the model
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X_train, y_train)

# Evaluate accuracy
print("Accuracy:", model.score(X_test, y_test))

Unsupervised Learning Example – Clustering

K-Means Clustering
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans

# Load dataset
iris = load_iris()
X = iris.data

# Create and fit the model
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(X)

# Show first 10 cluster assignments
print("Cluster labels:", kmeans.labels_[:10])

Key Takeaways

  • Supervised learning uses labeled data to predict outcomes.
  • Unsupervised learning uses unlabeled data to discover patterns.
  • Scikit-learn makes switching between the two approaches simple with a consistent API.

What’s Next?

In the next lesson, we’ll look at Dataset Structure: Features and Labels — the building blocks of any machine learning project.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.