{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Classification with PyTorch/TensorFlow\n", "\n", "Lab Assignment from [AI for Beginners Curriculum](https://github.com/microsoft/ai-for-beginners).\n", "\n", "## Part 1: Iris Classification\n", "\n", "Iris Dataset contains 150 records of 3 different classes of irises. Each record contains 4 numeric parameters: sepal length/width and petal length/width. It is an example of a simple dataset, for which you do not need a powerful neural network.\n", "\n", "### Getting the Dataset\n", "\n", "Iris dataset is build into Scikit Learn, so we can easily get it:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Features: ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'], Classes: ['setosa' 'versicolor' 'virginica']\n" ] } ], "source": [ "from sklearn.datasets import load_iris\n", "from sklearn.model_selection import train_test_split\n", "\n", "iris = load_iris()\n", "features = iris['data']\n", "labels = iris['target']\n", "class_names = iris['target_names']\n", "feature_names = iris['feature_names']\n", "\n", "print(f\"Features: {feature_names}, Classes: {class_names}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Visualize the Data\n", "\n", "In many cases, it makes sense to visualize the data to see if they look separable - it would assure us that we should be able to build good classification model. Because we have a few features, we can build a series of pairwise 2D scatter plots, showing different classes by different dot colors. This can be automatically done by a package called **seaborn**:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | sepal length (cm) | \n", "sepal width (cm) | \n", "petal length (cm) | \n", "petal width (cm) | \n", "Label | \n", "
|---|---|---|---|---|---|
| 0 | \n", "5.1 | \n", "3.5 | \n", "1.4 | \n", "0.2 | \n", "0 | \n", "
| 1 | \n", "4.9 | \n", "3.0 | \n", "1.4 | \n", "0.2 | \n", "0 | \n", "
| 2 | \n", "4.7 | \n", "3.2 | \n", "1.3 | \n", "0.2 | \n", "0 | \n", "
| 3 | \n", "4.6 | \n", "3.1 | \n", "1.5 | \n", "0.2 | \n", "0 | \n", "
| 4 | \n", "5.0 | \n", "3.6 | \n", "1.4 | \n", "0.2 | \n", "0 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 145 | \n", "6.7 | \n", "3.0 | \n", "5.2 | \n", "2.3 | \n", "2 | \n", "
| 146 | \n", "6.3 | \n", "2.5 | \n", "5.0 | \n", "1.9 | \n", "2 | \n", "
| 147 | \n", "6.5 | \n", "3.0 | \n", "5.2 | \n", "2.0 | \n", "2 | \n", "
| 148 | \n", "6.2 | \n", "3.4 | \n", "5.4 | \n", "2.3 | \n", "2 | \n", "
| 149 | \n", "5.9 | \n", "3.0 | \n", "5.1 | \n", "1.8 | \n", "2 | \n", "
150 rows × 5 columns
\n", "