Skip to content

Commit

Permalink
Created using Colaboratory
Browse files Browse the repository at this point in the history
  • Loading branch information
tushar-ghule committed Mar 15, 2023
1 parent 894e2f4 commit 69886dd
Showing 1 changed file with 213 additions and 3 deletions.
216 changes: 213 additions & 3 deletions Ch03_Tushar.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyPmOUQsb0KaXIHLHFWPL3t1",
"authorship_tag": "ABX9TyORLC2JDCkaONjXGhBNNON1",
"include_colab_link": true
},
"kernelspec": {
Expand All @@ -28,12 +28,222 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"id": "N8FP075EIiEO"
},
"outputs": [],
"source": []
"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": [
"<style>#sk-container-id-1 {color: black;background-color: white;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>Perceptron(eta0=0.1, random_state=1)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">Perceptron</label><div class=\"sk-toggleable__content\"><pre>Perceptron(eta0=0.1, random_state=1)</pre></div></div></div></div></div>"
]
},
"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": []
}
]
}

0 comments on commit 69886dd

Please sign in to comment.