You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ML-For-Beginners/2-Regression/3-Linear/solution/notebook.ipynb

333 lines
101 KiB

4 years ago
{
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3-final"
},
"orig_nbformat": 2,
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2,
"cells": [
{
"source": [
"## Pumpkin Pricing Per Bushel, by City\n",
"\n",
"Load up required libraries and dataset. Convert the data to a dataframe containing a subset of the data: \n",
"\n",
"- Only get pumpkins priced by the bushel\n",
"- Convert the date to a month\n",
"- Calculate the price to be an average of high and low prices\n",
"- Convert the price to reflect the pricing by bushel quantity"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" City Name Type Package Variety Sub Variety Grade Date \\\n",
"0 BALTIMORE NaN 24 inch bins NaN NaN NaN 4/29/17 \n",
"1 BALTIMORE NaN 24 inch bins NaN NaN NaN 5/6/17 \n",
"2 BALTIMORE NaN 24 inch bins HOWDEN TYPE NaN NaN 9/24/16 \n",
"3 BALTIMORE NaN 24 inch bins HOWDEN TYPE NaN NaN 9/24/16 \n",
"4 BALTIMORE NaN 24 inch bins HOWDEN TYPE NaN NaN 11/5/16 \n",
"\n",
" Low Price High Price Mostly Low ... Unit of Sale Quality Condition \\\n",
"0 270.0 280.0 270.0 ... NaN NaN NaN \n",
"1 270.0 280.0 270.0 ... NaN NaN NaN \n",
"2 160.0 160.0 160.0 ... NaN NaN NaN \n",
"3 160.0 160.0 160.0 ... NaN NaN NaN \n",
"4 90.0 100.0 90.0 ... NaN NaN NaN \n",
"\n",
" Appearance Storage Crop Repack Trans Mode Unnamed: 24 Unnamed: 25 \n",
"0 NaN NaN NaN E NaN NaN NaN \n",
"1 NaN NaN NaN E NaN NaN NaN \n",
"2 NaN NaN NaN N NaN NaN NaN \n",
"3 NaN NaN NaN N NaN NaN NaN \n",
"4 NaN NaN NaN N NaN NaN NaN \n",
"\n",
"[5 rows x 26 columns]"
],
"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>City Name</th>\n <th>Type</th>\n <th>Package</th>\n <th>Variety</th>\n <th>Sub Variety</th>\n <th>Grade</th>\n <th>Date</th>\n <th>Low Price</th>\n <th>High Price</th>\n <th>Mostly Low</th>\n <th>...</th>\n <th>Unit of Sale</th>\n <th>Quality</th>\n <th>Condition</th>\n <th>Appearance</th>\n <th>Storage</th>\n <th>Crop</th>\n <th>Repack</th>\n <th>Trans Mode</th>\n <th>Unnamed: 24</th>\n <th>Unnamed: 25</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>BALTIMORE</td>\n <td>NaN</td>\n <td>24 inch bins</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>4/29/17</td>\n <td>270.0</td>\n <td>280.0</td>\n <td>270.0</td>\n <td>...</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>E</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>1</th>\n <td>BALTIMORE</td>\n <td>NaN</td>\n <td>24 inch bins</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>5/6/17</td>\n <td>270.0</td>\n <td>280.0</td>\n <td>270.0</td>\n <td>...</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>E</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>2</th>\n <td>BALTIMORE</td>\n <td>NaN</td>\n <td>24 inch bins</td>\n <td>HOWDEN TYPE</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>9/24/16</td>\n <td>160.0</td>\n <td>160.0</td>\n <td>160.0</td>\n <td>...</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>N</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>3</th>\n <td>BALTIMORE</td>\n <td>NaN</td>\n <td>24 inch bins</td>\n <td>HOWDEN TYPE</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>9/24/16</td>\n <td>160.0</td>\n <td>160.0</td>\n <td>160.0</td>\n <td>...</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>N</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>4</th>\n <td>BALTIMORE</td>\n <td>NaN</td>\n <td>24 inch bins</td>\n <td>HOWDEN TYPE</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>11/5/16</td>\n <td>90.0</td>\n <td>100.0</td>\n <td>90.0</td>\n <td>...</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>N</td>\n <td>NaN</td>\n <td>NaN</td>\n <td>NaN</td>\n </tr>\n </tbody>\n</table>\n<p>5 rows × 26 columns</p>\n</div>"
},
"metadata": {},
"execution_count": 18
}
],
"source": [
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"pumpkins = pd.read_csv('../../data/US-pumpkins.csv')\n",
"\n",
"pumpkins.head()\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" Month Variety City Package Low Price High Price Price\n",
"70 1 3 1 0 5 3 13.636364\n",
"71 1 3 1 0 10 7 16.363636\n",
"72 2 3 1 0 10 7 16.363636\n",
"73 2 3 1 0 9 6 15.454545\n",
"74 2 3 1 0 5 3 13.636364"
],
"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>Month</th>\n <th>Variety</th>\n <th>City</th>\n <th>Package</th>\n <th>Low Price</th>\n <th>High Price</th>\n <th>Price</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>70</th>\n <td>1</td>\n <td>3</td>\n <td>1</td>\n <td>0</td>\n <td>5</td>\n <td>3</td>\n <td>13.636364</td>\n </tr>\n <tr>\n <th>71</th>\n <td>1</td>\n <td>3</td>\n <td>1</td>\n <td>0</td>\n <td>10</td>\n <td>7</td>\n <td>16.363636</td>\n </tr>\n <tr>\n <th>72</th>\n <td>2</td>\n <td>3</td>\n <td>1</td>\n <td>0</td>\n <td>10</td>\n <td>7</td>\n <td>16.363636</td>\n </tr>\n <tr>\n <th>73</th>\n <td>2</td>\n <td>3</td>\n <td>1</td>\n <td>0</td>\n <td>9</td>\n <td>6</td>\n <td>15.454545</td>\n </tr>\n <tr>\n <th>74</th>\n <td>2</td>\n <td>3</td>\n <td>1</td>\n <td>0</td>\n <td>5</td>\n <td>3</td>\n <td>13.636364</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {},
"execution_count": 19
}
],
"source": [
"from sklearn.preprocessing import LabelEncoder\n",
"\n",
"pumpkins = pumpkins[pumpkins['Package'].str.contains('bushel', case=True, regex=True)]\n",
"\n",
"new_columns = ['Package', 'Variety', 'City Name', 'Month', 'Low Price', 'High Price', 'Date']\n",
"\n",
"pumpkins = pumpkins.drop([c for c in pumpkins.columns if c not in new_columns], axis=1)\n",
"\n",
"price = (pumpkins['Low Price'] + pumpkins['High Price']) / 2\n",
"\n",
"month = pd.DatetimeIndex(pumpkins['Date']).month\n",
"\n",
"new_pumpkins = pd.DataFrame({'Month': month, 'Variety': pumpkins['Variety'], 'City': pumpkins['City Name'], 'Package': pumpkins['Package'], 'Low Price': pumpkins['Low Price'],'High Price': pumpkins['High Price'], 'Price': price})\n",
"\n",
"new_pumpkins.loc[new_pumpkins['Package'].str.contains('1 1/9'), 'Price'] = price/1.1\n",
"\n",
"new_pumpkins.loc[new_pumpkins['Package'].str.contains('1/2'), 'Price'] = price*2\n",
"\n",
"new_pumpkins.iloc[:, 0:-1] = new_pumpkins.iloc[:, 0:-1].apply(LabelEncoder().fit_transform)\n",
"new_pumpkins.iloc[:, 0:-1] = new_pumpkins.iloc[:, 0:-1].apply(LabelEncoder().fit_transform)\n",
"\n",
"\n",
"new_pumpkins.head()\n"
]
},
{
"source": [
"A basic scatterplot reminds us that we only have month data from August through December. We probably need more data to be able to draw conclusions in a linear fashion."
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<matplotlib.collections.PathCollection at 0x7fd9e0e342b0>"
]
},
"metadata": {},
"execution_count": 20
},
{
"output_type": "display_data",
"data": {
"text/plain": "<Figure size 432x288 with 1 Axes>",
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Created with matplotlib (https://matplotlib.org/) -->\n<svg height=\"248.518125pt\" version=\"1.1\" viewBox=\"0 0 368.925 248.518125\" width=\"368.925pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n <defs>\n <style type=\"text/css\">\n*{stroke-linecap:butt;stroke-linejoin:round;}\n </style>\n </defs>\n <g id=\"figure_1\">\n <g id=\"patch_1\">\n <path d=\"M -0 248.518125 \nL 368.925 248.518125 \nL 368.925 0 \nL -0 0 \nz\n\" style=\"fill:none;\"/>\n </g>\n <g id=\"axes_1\">\n <g id=\"patch_2\">\n <path d=\"M 26.925 224.64 \nL 361.725 224.64 \nL 361.725 7.2 \nL 26.925 7.2 \nz\n\" style=\"fill:#ffffff;\"/>\n </g>\n <g id=\"PathCollection_1\">\n <defs>\n <path d=\"M 0 3 \nC 0.795609 3 1.55874 2.683901 2.12132 2.12132 \nC 2.683901 1.55874 3 0.795609 3 0 \nC 3 -0.795609 2.683901 -1.55874 2.12132 -2.12132 \nC 1.55874 -2.683901 0.795609 -3 0 -3 \nC -0.795609 -3 -1.55874 -2.683901 -2.12132 -2.12132 \nC -2.683901 -1.55874 -3 -0.795609 -3 0 \nC -3 0.795609 -2.683901 1.55874 -2.12132 2.12132 \nC -1.55874 2.683901 -0.795609 3 0 3 \nz\n\" id=\"m7b60e3a334\" style=\"stroke:#1f77b4;\"/>\n </defs>\n <g clip-path=\"url(#p8c3b97fedb)\">\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"201.474881\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"188.193399\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"188.193399\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"192.62056\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"201.474881\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"188.193399\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"192.62056\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"189.300189\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"201.474881\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"192.62056\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"189.300189\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"201.474881\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"192.62056\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"189.300189\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"201.474881\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"194.83414\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"192.62056\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"188.193399\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"197.047721\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"188.193399\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"197.047721\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"197.047721\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"346.506818\" xlink:href=\"#m7b60e3a334\" y=\"197.047721\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"143.597727\" xlink:hre
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAULUlEQVR4nO3dcYyU9Z3H8c+H7SobtbdyLNy6QOkRQq9X2qWZIA3JhdZ6GDR1NedVUj1yZ4p3qYmNDT2ozam5NpqjVnPJxQtWU+7q2TMpRWNpOUI1TU1LuygCHnLUhlpxA1s9qva2Fpfv/TEPdBlmmefZnWfneXbfr2Qy83znGeb7+AsfHp95nufniBAAoHymtboBAMDYEOAAUFIEOACUFAEOACVFgANASb1rIr9s5syZMX/+/In8SgAovd27d/8qIrpq6xMa4PPnz1d/f/9EfiUAlJ7tX9SrcwgFAEqKAAeAkiLAAaCkCHAAKCkCHABKakLPQsHksfW5I9q4/aBePT6kSzo7tG7lIvUt6Wl1W8CUQoAjs63PHdGGLfs0dGJYknTk+JA2bNknSYQ4MIE4hILMNm4/eDq8Txk6MayN2w+2qCNgaiLAkdmrx4cy1QHkgwBHZpd0dmSqA8gHAY7M1q1cpPZpPqPWPs1at3JRizoCpiYCHGPjBssAcpcqwG0ftr3P9h7b/Ulthu0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m2iSNatXJSpDiAfac5CsaSHJB2IiK+OqHePWO0aSfub3x6K6F+eOpSpDiAfac5CWS7pRkn7bO9Jal+QtNp2r6SQdFjSzbl0iMI5dOw3meo
},
"metadata": {
"needs_background": "light"
}
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"plt.scatter('Variety','Price',data=new_pumpkins)\n"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"-0.8634790400214403\n"
]
}
],
"source": [
"print(new_pumpkins['Variety'].corr(new_pumpkins['Price']))"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"<class 'pandas.core.frame.DataFrame'>\nInt64Index: 415 entries, 70 to 1742\nData columns (total 7 columns):\n # Column Non-Null Count Dtype \n--- ------ -------------- ----- \n 0 Month 415 non-null int64 \n 1 Variety 415 non-null int64 \n 2 City 415 non-null int64 \n 3 Package 415 non-null int64 \n 4 Low Price 415 non-null int64 \n 5 High Price 415 non-null int64 \n 6 Price 415 non-null float64\ndtypes: float64(1), int64(6)\nmemory usage: 25.9 KB\n"
]
}
],
"source": [
"\n",
"new_pumpkins.dropna(inplace=True)\n",
"new_pumpkins.info()\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" Variety Price\n",
"70 3 13.636364\n",
"71 3 16.363636\n",
"72 3 16.363636\n",
"73 3 15.454545\n",
"74 3 13.636364\n",
"... ... ...\n",
"1738 1 30.000000\n",
"1739 1 28.750000\n",
"1740 1 25.750000\n",
"1741 1 24.000000\n",
"1742 1 24.000000\n",
"\n",
"[415 rows x 2 columns]"
],
"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>Variety</th>\n <th>Price</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>70</th>\n <td>3</td>\n <td>13.636364</td>\n </tr>\n <tr>\n <th>71</th>\n <td>3</td>\n <td>16.363636</td>\n </tr>\n <tr>\n <th>72</th>\n <td>3</td>\n <td>16.363636</td>\n </tr>\n <tr>\n <th>73</th>\n <td>3</td>\n <td>15.454545</td>\n </tr>\n <tr>\n <th>74</th>\n <td>3</td>\n <td>13.636364</td>\n </tr>\n <tr>\n <th>...</th>\n <td>...</td>\n <td>...</td>\n </tr>\n <tr>\n <th>1738</th>\n <td>1</td>\n <td>30.000000</td>\n </tr>\n <tr>\n <th>1739</th>\n <td>1</td>\n <td>28.750000</td>\n </tr>\n <tr>\n <th>1740</th>\n <td>1</td>\n <td>25.750000</td>\n </tr>\n <tr>\n <th>1741</th>\n <td>1</td>\n <td>24.000000</td>\n </tr>\n <tr>\n <th>1742</th>\n <td>1</td>\n <td>24.000000</td>\n </tr>\n </tbody>\n</table>\n<p>415 rows × 2 columns</p>\n</div>"
},
"metadata": {},
"execution_count": 23
}
],
"source": [
"# create new dataframe \n",
"new_columns = ['Variety', 'Price']\n",
"ml_pumpkins = new_pumpkins.drop([c for c in new_pumpkins.columns if c not in new_columns], axis='columns')\n",
"\n",
"ml_pumpkins\n"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"X = ml_pumpkins.values[:, :1]\n",
"y = ml_pumpkins.values[:, 1:2]\n"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Model Accuracy: 0.7327987875929955\nCoefficients: [[-8.54296764]]\nMean squared error: 23.443815358076087\nCoefficient of determination: 0.7802537224707632\n"
]
}
],
"source": [
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.metrics import r2_score, mean_absolute_error\n",
"from sklearn.model_selection import train_test_split\n",
"\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
"lin_reg = LinearRegression()\n",
"lin_reg.fit(X_train,y_train)\n",
"\n",
"pred = lin_reg.predict(X_test)\n",
"\n",
"accuracy_score = lin_reg.score(X_train,y_train)\n",
"print('Model Accuracy: ', accuracy_score)\n",
"\n",
"# The coefficients\n",
"print('Coefficients: ', lin_reg.coef_)\n",
"# The mean squared error\n",
"print('Mean squared error: ',\n",
" mean_squared_error(y_test, pred))\n",
"# The coefficient of determination: 1 is perfect prediction\n",
"print('Coefficient of determination: ',\n",
" r2_score(y_test, pred)) "
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "<Figure size 432x288 with 1 Axes>",
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Created with matplotlib (https://matplotlib.org/) -->\n<svg height=\"235.34pt\" version=\"1.1\" viewBox=\"0 0 352.7 235.34\" width=\"352.7pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n <defs>\n <style type=\"text/css\">\n*{stroke-linecap:butt;stroke-linejoin:round;}\n </style>\n </defs>\n <g id=\"figure_1\">\n <g id=\"patch_1\">\n <path d=\"M 0 235.34 \nL 352.7 235.34 \nL 352.7 0 \nL 0 0 \nz\n\" style=\"fill:none;\"/>\n </g>\n <g id=\"axes_1\">\n <g id=\"patch_2\">\n <path d=\"M 10.7 224.64 \nL 345.5 224.64 \nL 345.5 7.2 \nL 10.7 7.2 \nz\n\" style=\"fill:#ffffff;\"/>\n </g>\n <g id=\"PathCollection_1\">\n <defs>\n <path d=\"M 0 3 \nC 0.795609 3 1.55874 2.683901 2.12132 2.12132 \nC 2.683901 1.55874 3 0.795609 3 0 \nC 3 -0.795609 2.683901 -1.55874 2.12132 -2.12132 \nC 1.55874 -2.683901 0.795609 -3 0 -3 \nC -0.795609 -3 -1.55874 -2.683901 -2.12132 -2.12132 \nC -2.683901 -1.55874 -3 -0.795609 -3 0 \nC -3 0.795609 -2.683901 1.55874 -2.12132 2.12132 \nC -1.55874 2.683901 -0.795609 3 0 3 \nz\n\" id=\"mdf5f7ba9d3\" style=\"stroke:#000000;\"/>\n </defs>\n <g clip-path=\"url(#p6f924b54ce)\">\n <use style=\"stroke:#000000;\" x=\"330.281818\" xlink:href=\"#mdf5f7ba9d3\" y=\"202.581672\"/>\n <use style=\"stroke:#000000;\" x=\"330.281818\" xlink:href=\"#mdf5f7ba9d3\" y=\"192.62056\"/>\n <use style=\"stroke:#000000;\" x=\"25.918182\" xlink:href=\"#mdf5f7ba9d3\" y=\"82.826973\"/>\n <use style=\"stroke:#000000;\" x=\"330.281818\" xlink:href=\"#mdf5f7ba9d3\" y=\"202.581672\"/>\n <use style=\"stroke:#000000;\" x=\"25.918182\" xlink:href=\"#mdf5f7ba9d3\" y=\"63.347466\"/>\n <use style=\"stroke:#000000;\" x=\"25.918182\" xlink:href=\"#mdf5f7ba9d3\" y=\"92.566727\"/>\n <use style=\"stroke:#000000;\" x=\"330.281818\" xlink:href=\"#mdf5f7ba9d3\" y=\"188.193399\"/>\n <use style=\"stroke:#000000;\" x=\"25.918182\" xlink:href=\"#mdf5f7ba9d3\" y=\"151.005249\"/>\n <use style=\"stroke:#000000;\" x=\"25.918182\" xlink:href=\"#mdf5f7ba9d3\" y=\"92.566727\"/>\n <use style=\"stroke:#000000;\" x=\"178.1\" xlink:href=\"#mdf5f7ba9d3\" y=\"179.339078\"/>\n <use style=\"stroke:#000000;\" x=\"25.918182\" xlink:href=\"#mdf5f7ba9d3\" y=\"121.785988\"/>\n <use style=\"stroke:#000000;\" x=\"330.281818\" xlink:href=\"#mdf5f7ba9d3\" y=\"192.62056\"/>\n <use style=\"stroke:#000000;\" x=\"25.918182\" xlink:href=\"#mdf5f7ba9d3\" y=\"92.566727\"/>\n <use style=\"stroke:#000000;\" x=\"25.918182\" xlink:href=\"#mdf5f7ba9d3\" y=\"107.176358\"/>\n <use style=\"stroke:#000000;\" x=\"330.281818\" xlink:href=\"#mdf5f7ba9d3\" y=\"199.261301\"/>\n <use style=\"stroke:#000000;\" x=\"25.918182\" xlink:href=\"#mdf5f7ba9d3\" y=\"102.306481\"/>\n <use style=\"stroke:#000000;\" x=\"25.918182\" xlink:href=\"#mdf5f7ba9d3\" y=\"107.176358\"/>\n <use style=\"stroke:#000000;\" x=\"25.918182\" xlink:href=\"#mdf5f7ba9d3\" y=\"107.176358\"/>\n <use style=\"stroke:#000000;\" x=\"25.918182\" xlink:href=\"#mdf5f7ba9d3\" y=\"92.566727\"/>\n <use style=\"stroke:#000000;\" x=\"330.281818\" xlink:href=\"#mdf5f7ba9d3\" y=\"192.62056\"/>\n <use style=\"stroke:#000000;\" x=\"25.918182\" xlink:href=\"#mdf5f7ba9d3\" y=\"121.785988\"/>\n <use style=\"stroke:#000000;\" x=\"330.281818\" xlink:href=\"#mdf5f7ba9d3\" y=\"192.62056\"/>\n <use style=\"stroke:#000000;\" x=\"330.281818\" xlink:href=\"#mdf5f7ba9d3\" y=\"199.261301\"/>\n <use style=\"stroke:#000000;\" x=\"330.281818\" xlink:href=\"#mdf5f7ba9d3\" y=\"214.756364\"/>\n <use style=\"stroke:#000000;\" x=\"330.281818\" xlink:href=\"#mdf5f7ba9d3\" y=\"199.261301\"/>\n <use style=\"stroke:#000000;\" x=\"25.918182\" xlink:href=\"#mdf5f7ba9d3\" y=\"102.306481\"/>\n <use style=\"stroke:#000000;\" x=\"330.281818\" xlink:href=\"#mdf5f7ba9d3\" y=\"175.354633\"/>\n <use style=\"stroke:#0
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAWAAAADrCAYAAABXYUzjAAAABHNCSVQICAgIfAhkiAAAAAlw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MYgEZPPslhEOwfryQCNNDZrvjlF/3rw3QigAEDBsNY9WFAR49yMa8saEEAhj33HO0KjMYOGJgwne2K337Tvz5MDgEMWDYYxu+9pzb20CHaFUVGAANT5NQpfbvjalX/+qAXAQxMsf20K7a3s4F865aZNSI/AhgokMEwfuMNtbEHD9KumDYEMFBQZ87oa1fcf7/+9c0C0y+U4DY0YEYMh7DKLvfq1ez3t7ZEaiVPh50XSnS7XRGR/gslRETb82zYAQMzanB33GqpjZ2bo10RhmE/fHd0u10Jw1DbHAQwUALvvquvXfH44/rXN41Gvc3nbvU8Sv6PDKCcBkM4TUUqCluxc+eyu+Kduy2gjh0wUHKOk90dP/us2vhKhXZFXgQwgIzPP9fXrnjxRf3rm5TqmJMs4+p5EMAA7mowjHs9tbGffFLc3fHOHQ97redBAAPYs0olG8iqF+SK9N68S5cuKdXzIIAB5PbNN/raFa++qn99+7G6uqpUz4MABqDNYBhvbamNfeed4uyOdSGAUWimj4oiv1otG8iqx52L1K7IiwBGYe0cFW2325Kmaf+oKCE8na5c0deueOst/esbtrCwoFTPg9fSo7B83x95KsnzPLl8+fLkF4Tc/vhDxHXzjzf1mqbFxcVMz3dhYUFWVlaUf4fX0mPmdDodpTqm18GD0/nevDxhq4IWBAqrXq8r1VEcOt+b9/77+tenCwGMwoqiSNyhf7e6ritRFFlaEUwZDOMbN9T
},
"metadata": {}
}
],
"source": [
"\n",
"plt.scatter(X_test, y_test, color='black')\n",
"plt.plot(X_test, pred, color='blue', linewidth=3)\n",
"\n",
"plt.xticks(())\n",
"plt.yticks(())\n",
"\n",
"plt.show()\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
]
}