Skip to content

Instantly share code, notes, and snippets.

@astha-29
Created August 13, 2020 07:01
Show Gist options
  • Save astha-29/583db6f6b9eff73f8a1f14eab7446b8b to your computer and use it in GitHub Desktop.
Save astha-29/583db6f6b9eff73f8a1f14eab7446b8b to your computer and use it in GitHub Desktop.
Mini Project.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Mini Project.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyNSrbQeYNhL9zoIWVTh18r6",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/astha-29/583db6f6b9eff73f8a1f14eab7446b8b/mini-project.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "WD3KV2nNlIBa",
"colab_type": "code",
"colab": {}
},
"source": [
"import pandas as pd"
],
"execution_count": 1,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "ALeEBumTlgUN",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 415
},
"outputId": "93abcff1-7058-4275-9f75-b8fc5421dda6"
},
"source": [
"df= pd.read_csv('advertising.csv')\n",
"df"
],
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>TV</th>\n",
" <th>Radio</th>\n",
" <th>Newspaper</th>\n",
" <th>Sales</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>230.1</td>\n",
" <td>37.8</td>\n",
" <td>69.2</td>\n",
" <td>22.1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>44.5</td>\n",
" <td>39.3</td>\n",
" <td>45.1</td>\n",
" <td>10.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>17.2</td>\n",
" <td>45.9</td>\n",
" <td>69.3</td>\n",
" <td>12.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>151.5</td>\n",
" <td>41.3</td>\n",
" <td>58.5</td>\n",
" <td>16.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>180.8</td>\n",
" <td>10.8</td>\n",
" <td>58.4</td>\n",
" <td>17.9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>195</th>\n",
" <td>38.2</td>\n",
" <td>3.7</td>\n",
" <td>13.8</td>\n",
" <td>7.6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>196</th>\n",
" <td>94.2</td>\n",
" <td>4.9</td>\n",
" <td>8.1</td>\n",
" <td>14.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>197</th>\n",
" <td>177.0</td>\n",
" <td>9.3</td>\n",
" <td>6.4</td>\n",
" <td>14.8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>198</th>\n",
" <td>283.6</td>\n",
" <td>42.0</td>\n",
" <td>66.2</td>\n",
" <td>25.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>199</th>\n",
" <td>232.1</td>\n",
" <td>8.6</td>\n",
" <td>8.7</td>\n",
" <td>18.4</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>200 rows × 4 columns</p>\n",
"</div>"
],
"text/plain": [
" TV Radio Newspaper Sales\n",
"0 230.1 37.8 69.2 22.1\n",
"1 44.5 39.3 45.1 10.4\n",
"2 17.2 45.9 69.3 12.0\n",
"3 151.5 41.3 58.5 16.5\n",
"4 180.8 10.8 58.4 17.9\n",
".. ... ... ... ...\n",
"195 38.2 3.7 13.8 7.6\n",
"196 94.2 4.9 8.1 14.0\n",
"197 177.0 9.3 6.4 14.8\n",
"198 283.6 42.0 66.2 25.5\n",
"199 232.1 8.6 8.7 18.4\n",
"\n",
"[200 rows x 4 columns]"
]
},
"metadata": {
"tags": []
},
"execution_count": 2
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "d1QwkFh-lqTM",
"colab_type": "code",
"colab": {}
},
"source": [
"x=df.iloc[:,0:3].values\n",
"y=df.iloc[:,3].values"
],
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "YiDH0SD1lwwd",
"colab_type": "code",
"colab": {}
},
"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 =3)"
],
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "JABqG_Fsl0xV",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "1f8161f0-2f60-48e0-ef8f-483cdce84a4f"
},
"source": [
"x_train.shape"
],
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(140, 3)"
]
},
"metadata": {
"tags": []
},
"execution_count": 5
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "cNC2u1IVl3cj",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "134b422d-6226-4db5-f5f5-be5b39c0e307"
},
"source": [
"x_test.shape"
],
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(60, 3)"
]
},
"metadata": {
"tags": []
},
"execution_count": 6
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "XVCTJ9gol5r6",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 225
},
"outputId": "1fd6d8ac-f2fc-4da3-f8e7-192625cfb5ff"
},
"source": [
"from sklearn.linear_model import LinearRegression\n",
"model = LinearRegression()\n",
"model.fit(x_train,y_train)\n",
"y_pred = model.predict(x_test)\n",
"y_pred"
],
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([17.94221632, 11.28731032, 19.36406753, 15.25309499, 8.85035488,\n",
" 11.08345095, 24.54827272, 10.72184726, 18.64190205, 17.03877174,\n",
" 14.71887065, 13.30204368, 19.10529921, 11.4654086 , 13.82417942,\n",
" 14.56139355, 16.86156735, 17.27369971, 17.78634747, 21.28201581,\n",
" 19.1397699 , 11.05346066, 9.93276334, 11.49854807, 8.5309559 ,\n",
" 13.26073545, 21.75566382, 16.96066432, 24.25791572, 11.92392893,\n",
" 16.40376866, 21.96064207, 9.51770237, 10.16209996, 10.08141197,\n",
" 10.45644324, 15.54919097, 9.92133897, 13.83425453, 12.54320065,\n",
" 14.5093965 , 12.61758414, 6.46804914, 20.25656292, 23.16303373,\n",
" 24.65508581, 15.20817964, 9.27513655, 18.72004324, 18.16217728,\n",
" 12.73063894, 16.65175796, 15.79776032, 8.36188762, 21.22771856,\n",
" 9.52094834, 23.88078008, 23.29062902, 19.6930198 , 16.76467522])"
]
},
"metadata": {
"tags": []
},
"execution_count": 7
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "nmMsa5GLl7ga",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 121
},
"outputId": "beb88f11-84f2-468e-f3a6-f3cafe0f0b69"
},
"source": [
"y_test"
],
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([16.6, 10.7, 20.7, 14.8, 8.4, 11.3, 26.2, 10.6, 18.3, 16.7, 17.6,\n",
" 13.6, 17. , 10.8, 12.9, 16.7, 16.9, 16.5, 17.1, 23.8, 19. , 10.4,\n",
" 9.2, 11.8, 8.7, 13.3, 24.7, 16.6, 23.8, 12. , 16. , 23.2, 7.3,\n",
" 11.9, 10.1, 14. , 17.9, 8.8, 13.4, 10.3, 15.3, 12.6, 3.2, 22.4,\n",
" 24.2, 27. , 15.5, 1.6, 20.5, 16. , 11.9, 19.7, 16.7, 5.7, 19.8,\n",
" 6.6, 24.4, 21.4, 16.8, 19.9])"
]
},
"metadata": {
"tags": []
},
"execution_count": 8
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "tpTSH1jBl-Rf",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "3698026c-11af-40c7-af9e-2bc837d7076a"
},
"source": [
"from sklearn import metrics\n",
"metrics.r2_score(y_test,y_pred)*100 #Accuracy"
],
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"88.77675297095176"
]
},
"metadata": {
"tags": []
},
"execution_count": 9
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "gblu5_8emAnH",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment