Jun 2023 · Jul 2023
Car Evaluation Classifier with KNN and PCA
K-nearest neighbours with PCA-projected decision boundaries and error analysis.
- Python
- scikit-learn
- Matplotlib
- Seaborn
A K-nearest neighbours classifier for the Car Evaluation dataset, predicting acceptability from categorical attributes like buying price, maintenance cost and safety rating. The four classes are unacceptable, acceptable, good and very good.
Preparation and tuning
Categorical attributes are encoded numerically with scikit-learn’s LabelEncoder, then split into
training and test sets. Rather than guessing K, I plotted the error rate across candidate values and
chose from the curve.
Interpretation
The part that made this worthwhile was visualisation. Principal Component Analysis reduces the features to two dimensions, which allows the decision boundaries to be drawn: a meshgrid plot shows how the classifier partitions the feature space, and scatter plots compare actual against predicted classes.
A confusion matrix completes the picture, showing which classes are being confused rather than just how often. In a four-class problem with imbalanced classes, that distinction is the whole story.