Lecture

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.

Please refer to the slide deck for this lesson for a full conceptual breakdown.

Here, we'll focus on practical examples in Scikit-learn.


Supervised Learning Example – Classification

The following example shows how to use Scikit-learn to train a K-Nearest Neighbors classifier.

The K-Nearest Neighbors classifier is a simple classifier that predicts the class of a new data point based on the classes of the nearest data points in the training set.


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))

The dataset used is the Iris dataset, which is a classic dataset for classification tasks.

The dataset contains 150 samples of iris flowers, with 4 features: sepal length, sepal width, petal length, and petal width.

The target variable is the species of the iris flower.


Unsupervised Learning Example – Clustering

The following example shows how to use Scikit-learn to train a K-Means clustering model.

The K-Means clustering model is a simple clustering model that groups data points into clusters based on their similarity.


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.
Quiz
0 / 1

In supervised learning, models are trained using datasets that do not contain labels.

True
False

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help