Supervised and unsupervised
- Machine Learning course
- Beginner
- Lesson 2/112
- 5 min
Supervised learning trains models with labeled data; each example comes with a correct output (label).
Supervised learning
In supervised learning the correct answer (label) of every example is known. The model learns the mapping from input to label.
- If the label is a number, it is regression: price, temperature.
- If the label is a class, it is classification: spam/normal, cat/dog.
X = [[1], [2], [3], [4]] # inputs
y = [0, 0, 1, 1] # labels (classes)
from sklearn.neighbors import KNeighborsClassifier
KNeighborsClassifier(n_neighbors=1).fit(X, y)Unsupervised learning
In unsupervised learning there are no labels; the algorithm finds the structure of the data itself.
- Clustering: groups similar customers (KMeans).
- Dimensionality reduction: summarises many-column data with few columns (PCA).
from sklearn.cluster import KMeans
X = [[1, 1], [1, 2], [9, 9], [9, 8]]
KMeans(n_clusters=2, n_init=10).fit(X) # no y givenThe difference also shows in the fit call: unsupervised models get only X.
Practice Train a classifier
Train a KNeighborsClassifier(n_neighbors=1) model with X and y, and print the class of the input [[3.5]].
Starter code
from sklearn.neighbors import KNeighborsClassifier
X = [[1], [2], [3], [4]]
y = [0, 0, 1, 1]
# buraya yazSample solution
from sklearn.neighbors import KNeighborsClassifier
X = [[1], [2], [3], [4]]
y = [0, 0, 1, 1]
model = KNeighborsClassifier(n_neighbors=1)
model.fit(X, y)
print(model.predict([[3.5]])[0])Expected output
1
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Predicting a house price from its square metres is which kind of problem?
- Dimensionality reduction
- Supervised regression Correct answer
- Supervised classification
- Clustering
Explanation: The label (price) is known and is a continuous number, so it is supervised regression.
2 Train the unsupervised model with the inputs only.
km = KMeans(n_clusters=2, n_init=10)
km.fit(____)
km = KMeans(n_clusters=2, n_init=10)
km.fit(X)
Explanation: Clustering has no labels; fit takes only X.
3 Match the problems with their types.
| Term | Meaning |
|---|---|
Spam detection |
Classification |
Temperature forecast |
Regression |
Customer grouping |
Clustering |
Reducing the number of columns |
Dimensionality reduction |