diff --git a/Ch03_Tushar.ipynb b/Ch03_Tushar.ipynb new file mode 100644 index 00000000..322a2ef5 --- /dev/null +++ b/Ch03_Tushar.ipynb @@ -0,0 +1,249 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [], + "authorship_tag": "ABX9TyORLC2JDCkaONjXGhBNNON1", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "N8FP075EIiEO" + }, + "outputs": [], + "source": [ + "from sklearn import datasets \n", + "import numpy as np " + ] + }, + { + "cell_type": "code", + "source": [ + "iris = datasets.load_iris()\n", + "X = iris.data[:, [2,3]]\n", + "Y = iris.target \n", + "\n", + "print(\"Class labels: \", np.unique(Y))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "idX04APuJ_g0", + "outputId": "e983a538-d7ce-4540-a4b4-cf0c3d1a73fd" + }, + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Class labels: [0 1 2]\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "from sklearn.model_selection import train_test_split\n", + "\n", + "X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size= 0.3, random_state=1, stratify = Y)" + ], + "metadata": { + "id": "EgcMvtktKRnN" + }, + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "print('Labels count in Y:', np.bincount(Y))\n", + "print('Labels count in Y_train:', np.bincount(y_train))\n", + "print('Labels count in Y_test:', np.bincount(y_test))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "vaDTfII-LqWc", + "outputId": "e32166c3-598f-4f1e-8682-54c5cd9ffa85" + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Labels count in Y: [50 50 50]\n", + "Labels count in Y_train: [35 35 35]\n", + "Labels count in Y_test: [15 15 15]\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "from sklearn.preprocessing import StandardScaler\n", + "\n", + "sc = StandardScaler()\n", + "sc.fit(X_train)\n", + "# this will get the values of mean and standard deviation \n", + "X_train_std = sc.transform(X_train)\n", + "X_test_std = sc.transform(X_test)\n" + ], + "metadata": { + "id": "BGg2xIMdL0Ib" + }, + "execution_count": 6, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "Training the perceptron " + ], + "metadata": { + "id": "gXQ8fetLMh7J" + } + }, + { + "cell_type": "code", + "source": [ + "from sklearn.linear_model import Perceptron\n", + "\n", + "ppn = Perceptron(eta0 = 0.1, random_state = 1)\n", + "ppn.fit(X_train_std, y_train)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 74 + }, + "id": "o5bohE6xMgYv", + "outputId": "77f1c87f-db79-4b9b-d884-57ba32d7eb84" + }, + "execution_count": 8, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "Perceptron(eta0=0.1, random_state=1)" + ], + "text/html": [ + "
Perceptron(eta0=0.1, random_state=1)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" + ] + }, + "metadata": {}, + "execution_count": 8 + } + ] + }, + { + "cell_type": "code", + "source": [ + "y_pred = ppn.predict(X_test_std)\n", + "print('Misclassification:%d' %(y_test != y_pred).sum() )" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "tBwZ8eEvM1pV", + "outputId": "be03d1f4-9d44-4726-b378-bb8ee90c30b9" + }, + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Misclassification:1\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "from sklearn.metrics import accuracy_score\n", + "\n", + "print('Accusracy: %.3f' % accuracy_score(y_test, y_pred))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "QUJdjM3cOI8g", + "outputId": "75d31324-8f37-45e3-8914-b183fb85fb3c" + }, + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Accusracy: 0.978\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "print('Accusracy: %.3f' % ppn.score(X_test_std, y_test))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "M80HPZHfOaE5", + "outputId": "2f192459-9d37-4bd3-f95d-4b48050efaf0" + }, + "execution_count": 12, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Accusracy: 0.978\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "qsy8XJFxOlfI" + }, + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file