Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Machine Learning Complete the lab as given in the notebook file. Include all screenshots of code and output. { nbformat: 4, nbformat_minor: 0, metadata: {

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 ", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "source": [ "# Import datasets, classifiers and performance metrics ", "from sklearn import datasets, svm, metrics ", "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)) ", "for ax, image, label in zip(axes, digits.images, digits.target): ", " ax.set_axis_off() ", " ax.imshow(image, cmap=plt.cm.gray_r, interpolation=\"nearest\") ", " ax.set_title(\"Training: %i\" % label)" ], "metadata": { "id": "C2_v9MqbLhQX" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# flatten the images ", "n_samples = len(digits.images) ", "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 ", "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 ", "X_train, X_test, y_train, y_test = train_test_split( ", " data, digits.target, test_size=0.5, shuffle=False ", ")" ], "metadata": { "id": "B-AEWWiTLlqG" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# Learn the digits on the train subset ", "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 ", "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)) ", "for ax, image, prediction in zip(axes, X_test, predicted): ", " ax.set_axis_off() ", " image = image.reshape(8, 8) ", " ax.imshow(image, cmap=plt.cm.gray_r, interpolation=\"nearest\") ", " ax.set_title(f\"Prediction: {prediction}\")" ], "metadata": { "id": "6sHCRYZOLOvz" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "print( ", " f\"Classification report for classifier {clf}:\ \" ", " f\"{metrics.classification_report(y_test, predicted)}\ \" ", ")" ], "metadata": { "id": "cn875FFXLQql" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "disp = metrics.ConfusionMatrixDisplay.from_predictions(y_test, predicted) ", "disp.figure_.suptitle(\"Confusion Matrix\") ", "print(f\"Confusion matrix:\ {disp.confusion_matrix}\") ", " ", "plt.show()" ], "metadata": { "id": "wVU73pCgLSun" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "3PpbWPoFLUyb" }, "execution_count": null, "outputs": [] } ] }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions