Lab Assignment 05: Classification with the Wine Dataset
Objective: In this lab, you will train and evaluate classification models (Naive Bayes, KNN, Decision Tree, and SVM) using the Wine dataset
Instructions
Create your Colab notebook
Follow the steps below to complete the lab. Answer all questions marked as QUESTION. Submit the completed notebook on Gradescope as a public URL. Collaboration is allowed but submissions must be individual.
Make sure to add your name at the beginning of the notebook
TIP: once you complete your Lab, tidy-up your notebook and move all library imports into once coding block and rerun
Submission: code cells should be executed and show outputs. However, avoid printing the entire data just use head(), for example. If you have collaborated with another student, state their name, but the submission and the notebook must be individual (no shared URL). Set the Shared URL Link to Viewable publicly and paste the link to Gradescope assignment
Step 1. Wine dataset (15pts)
-
Load the Wine dataset using
sklearn.datasets.load_wine() -
Explore the dataset to understand the features and target classes
Choose the right plot to examine the classes distributions. For example, you should be able to check if classes are balanced.
Check if you have any missing values. If yes, apply any relevant standard approach to deal with NA.
Normalize dataset using
StandardScalerSplit the dataset into training and testing sets. Suggested split ratio: 80% training, 20% testing
- How many classes are in the dataset?
- Which column will you use as a target (y)? Provide its name.
- What type of features do you have (categorical, continuous..) and how many features?
Step 2: Naive Bayes (NB) (20pts)
Import Gaussian NB
from sklearn.naive_bayes import GaussianNBYou can also consult the sklearn documentation: GaussianNB
Import evaluation metrics
from sklearn.metrics import accuracy_score, precision_score, recall_score, ConfusionMatrixDisplayTrain model. As you learned from regression, sklearn has the same pipeline: fit, predict
# Train model = GaussianNB() model.fit(X_train, y_train) # Predict y_pred = model.predict(X_test)Apply the evaluation metrics. These metrics will be similar for other classifiers to use.
For precision, we use weighted average since we are dealing with multiclass classification. By default, precision_score calculates precision for each class individually. By specifying average='weighted', the metric combines these per-class precision scores into a single number.
accuracy = accuracy_score(y_test, y_pred) precision = precision_score(y_test, y_pred, average='weighted') recall = recall_score(y_test, y_pred, average='weighted') print(f"Accuracy: {accuracy}") print(f"Precision: {precision}") print(f"Recall: {recall}") # Confusion matrix ConfusionMatrixDisplay.from_predictions(y_test, y_pred) plt.show()
What do the confusion matrix and metrics tell you about model performance?
Why do you think Gaussian Naive Bayes is an appropriate choice for this dataset?
Step 3: KNN (20 Points)
Import a KNN classifier.
Train a KNN classifier
from sklearn.neighbors import KNeighborsClassifierAssign
k=3Note: k=3 is a commonly chosen starting point. You will later explore how to find the best k.
from sklearn.model_selection import cross_val_score # Train model Example (no cross-validation) # K-Nearest Neighbors with k=3 knn_model_k3 = KNeighborsClassifier(n_neighbors=3) knn_model_k3.fit(X_train, y_train) y_pred_knn_k3 = knn_model_k3.predict(X_test) # Evaluate KNN with k=3 print("KNN (k=3) Evaluation:") print(f"Accuracy: {accuracy_score(y_test, y_pred_knn_k3):.4f}") print(f"Precision: {precision_score(y_test, y_pred_knn_k3, average='weighted'):.4f}") print(f"Recall: {recall_score(y_test, y_pred_knn_k3, average='weighted'):.4f}") ConfusionMatrixDisplay.from_predictions(y_test, y_pred_knn_k3) plt.show()Import cross-validation
from sklearn.model_selection import cross_val_scoreEvaluate the model using K-fold cross-validation, using 5 folds
scores = cross_val_score(KNeighborsClassifier(), X_train, y_train, cv=5) print(f"Cross-validation scores: {scores}") print(f"Mean score: {scores.mean()}")Learn how to set a range of k and determine the best value, see Reading: Best K
Here is an example how you can loop over several odd k values and use cross-validation technique:
#select the range, for example from 1 to 21 k_values = range(1, 21, 2) # create an array to get the average metrics for each fold mean_scores = [] for k in k_values: knn_model = KNeighborsClassifier(n_neighbors=k) scores = cross_val_score(knn_model, X_train, y_train, cv=5) mean_scores.append(scores.mean())Find the best score (remember to import numpy) and print it:
best_k = k_values[np.argmax(mean_scores)] print(f"Best k: {best_k}, Accuracy: {max(mean_scores):.4f}")- Train KNN on the best_k (see how it was done with k=3) and display the evaluation metrics
Bonus: Decision boundaries are typically visualized in 2D or 3D. Plotting the decision boundary with 13 features is not feasible and we learn soon how to do PCA (dimensionality reduction). To visualize plot for now, choose 2 features only, for example (you can manually choose two features from the dataset (e.g., alcohol and malic_acid)). Then you have to retrain your KNN and follow the example from the provided document to visualize the plot
What is the best value of
kfor this dataset?
Step 4: Decision Tree (20 Points)
Import Decision Tree
from sklearn.tree import DecisionTreeClassifierTrain the model (see Documentation)
Set max depth to 3: "max_depth" refers to a hyperparameter that controls the maximum depth or number of levels a tree can grow to during training
# Train model model = DecisionTreeClassifier(max_depth=3) model.fit(X_train, y_train)Import plot_tree
from sklearn.tree import plot_treePlot Decision tree, using this Documentation
Modify the tree depth and create another plot
Do not forget to evaluate the model
How does the tree depth affect performance and interpretability?
Step 5: SVM (20 Points)
Import SVM
from sklearn.svm import SVCTrain an SVM with a linear kernel
# Train model model = SVC(kernel='linear') model.fit(X_train, y_train)Experiment with other kernels (see Documentation).
- Predict and evaluate the model for each kernel you choose
Which kernel performs best, and why?
Step 6: Reflections
What did you learn in this Lab?
What was the most interesting (for example, specific model, evaluation etc)
Do you feel comfortable using classifiers? Do you feel you gained useful practical experience?
Grading Rubrics
- Step 1 Wine Dataset 10pts
- Step 2 NB 20pts
- Step 3 KNN 20pts
- Step 4 Decision Tree 20pts
- Step 5 SVM 20pts
- Submission, Code, Format, Help citation (10 Points)