Two more classic classifiers with very different ideas. KNN classifies by the majority vote of nearby points; SVM finds the boundary with the widest margin between classes.
K-nearest neighbours
from sklearn.neighbors import KNeighborsClassifierKNeighborsClassifier(n_neighbors=5).fit(X_train, y_train)# a new point gets the majority label of its 5 nearest neighbours
Support vector machines
from sklearn.svm import SVCSVC(kernel="rbf", C=1.0).fit(X_train, y_train) # non-linear boundaries via the kernel
Key takeaways
- 01KNN classifies by the majority label of the k nearest points.
- 02SVM finds the maximum-margin boundary; kernels enable non-linear boundaries.
- 03Both are distance-based — feature scaling is essential.
- 04KNN is slow to predict on big data; SVM doesn't scale to huge datasets.
