Machine Learning
Complete the lab as given in the notebook file. Include all screenshots of code and output.
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "An7pIRrhKhTb"
},
"outputs": [],
"source": [
"# Source: https://scikit-learn.org/stable/auto_examples/classification/plot_digits_classification.html\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"source": [
"# Import datasets, classifiers and performance metrics\n",
"from sklearn import datasets, svm, metrics\n",
"from sklearn.model_selection import train_test_split"
],
"metadata": {
"id": "m-4vkyOSLHY-"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"digits = datasets.load_digits()"
],
"metadata": {
"id": "JUK7wpLQLJ0Y"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"_, axes = plt.subplots(nrows=1, ncols=4, figsize=(10, 3))\n",
"for ax, image, label in zip(axes, digits.images, digits.target):\n",
" ax.set_axis_off()\n",
" ax.imshow(image, cmap=plt.cm.gray_r, interpolation=\"nearest\")\n",
" ax.set_title(\"Training: %i\" % label)"
],
"metadata": {
"id": "C2_v9MqbLhQX"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# flatten the images\n",
"n_samples = len(digits.images)\n",
"data = digits.images.reshape((n_samples, -1))"
],
"metadata": {
"id": "L65XSQXhLLw0"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Create a classifier: a support vector classifier\n",
"clf = svm.SVC(gamma=0.001)"
],
"metadata": {
"id": "6SO7xR-QMUa9"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Split data into 50% train and 50% test subsets\n",
"X_train, X_test, y_train, y_test = train_test_split(\n",
" data, digits.target, test_size=0.5, shuffle=False\n",
")"
],
"metadata": {
"id": "B-AEWWiTLlqG"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Learn the digits on the train subset\n",
"clf.fit(X_train, y_train)"
],
"metadata": {
"id": "0QXu0qt5LkA_"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Predict the value of the digit on the test subset\n",
"predicted = clf.predict(X_test)"
],
"metadata": {
"id": "v-hhRIHWLwB3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"_, axes = plt.subplots(nrows=1, ncols=4, figsize=(10, 3))\n",
"for ax, image, prediction in zip(axes, X_test, predicted):\n",
" ax.set_axis_off()\n",
" image = image.reshape(8, 8)\n",
" ax.imshow(image, cmap=plt.cm.gray_r, interpolation=\"nearest\")\n",
" ax.set_title(f\"Prediction: {prediction}\")"
],
"metada.