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

525 lines
213 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\n",
4 years ago
"\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": 47,
4 years ago
"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": 47
4 years ago
}
],
"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": 48,
4 years ago
"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": 48
4 years ago
}
],
"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",
"\n",
"new_pumpkins.head()\n"
]
},
{
"source": [
"A 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."
4 years ago
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 49,
4 years ago
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<matplotlib.collections.PathCollection at 0x7ffe31f74640>"
4 years ago
]
},
"metadata": {},
"execution_count": 49
4 years ago
},
{
"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=\"m44c2a297c2\" style=\"stroke:#1f77b4;\"/>\n </defs>\n <g clip-path=\"url(#pc99b83412c)\">\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"118.234091\" xlink:href=\"#m44c2a297c2\" y=\"201.474881\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"118.234091\" xlink:href=\"#m44c2a297c2\" y=\"188.193399\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"194.325\" xlink:href=\"#m44c2a297c2\" y=\"188.193399\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"194.325\" xlink:href=\"#m44c2a297c2\" y=\"192.62056\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"194.325\" xlink:href=\"#m44c2a297c2\" y=\"201.474881\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"194.325\" xlink:href=\"#m44c2a297c2\" y=\"188.193399\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"194.325\" xlink:href=\"#m44c2a297c2\" y=\"192.62056\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"194.325\" xlink:href=\"#m44c2a297c2\" y=\"189.300189\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"194.325\" xlink:href=\"#m44c2a297c2\" y=\"201.474881\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"194.325\" xlink:href=\"#m44c2a297c2\" y=\"192.62056\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"194.325\" xlink:href=\"#m44c2a297c2\" y=\"189.300189\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"194.325\" xlink:href=\"#m44c2a297c2\" y=\"201.474881\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"194.325\" xlink:href=\"#m44c2a297c2\" y=\"192.62056\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"194.325\" xlink:href=\"#m44c2a297c2\" y=\"189.300189\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"194.325\" xlink:href=\"#m44c2a297c2\" y=\"201.474881\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"194.325\" xlink:href=\"#m44c2a297c2\" y=\"194.83414\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"194.325\" xlink:href=\"#m44c2a297c2\" y=\"192.62056\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"118.234091\" xlink:href=\"#m44c2a297c2\" y=\"188.193399\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"118.234091\" xlink:href=\"#m44c2a297c2\" y=\"197.047721\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"118.234091\" xlink:href=\"#m44c2a297c2\" y=\"188.193399\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"118.234091\" xlink:href=\"#m44c2a297c2\" y=\"197.047721\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"118.234091\" xlink:href=\"#m44c2a297c2\" y=\"197.047721\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"118.234091\" xlink:href=\"#m44c2a297c2\" y=\"197.047721\"/>\n <use style=\"fill:#1f77b4;stroke:#1f77b4;\" x=\"118.234091\" xlink:href=\"#m44c2a297c2\" y=\"92.566727\"/>\n <u
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAZ+0lEQVR4nO3df2yc9X0H8Pc7V6dxIZ3JcNLEiWuaRa42Ipz2lARZmjyoZwQIDBobEWHZVBE2CQmUzm3SRC2VgojmEtCmiikpqNlCaZGaGQR0nhWIqiLi9kICDgpeoE0CThS7UJdATQmXz/64x65zfh7f89j33PPj3i/Juns+95zvo6+cT577Pt8fNDOIiEjyzIk6ARERmRkVcBGRhFIBFxFJKBVwEZGEUgEXEUmoT1Xywy6//HJramqq5EeKiCTeoUOHfmNm9cXxihbwpqYm5HK5Sn6kiEjikTzpFlcXiohIQqmAi4gklAq4iEhCqYCLiCSUCriISEJVdBSKhK/n8BC6ewdxenQMS+pq0dXRjM5VDVGnJSIhUAFPkZ7DQ9iybwBj5/MAgKHRMWzZNwAAKuIiKaQulBTp7h2cKN7jxs7n0d07GFFGIhImFfAUOT06FiguIsmmAp4iS+pqA8VFJNlUwFOkq6MZNXN4UaxmDtHV0RxRRiISJhXwtGGJYxFJDV8FnOQJkgMkj5DMObEFJPtIHnceLws3V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YRqEEo5mrwaxbsyxQXMLn5wr8DwCuMbOrUNiB/jqSawFsBrDfzFYA2O8cS4S8vsjqC6479YEHs71
4 years ago
},
"metadata": {
"needs_background": "light"
}
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"plt.scatter('Month','Price',data=new_pumpkins)"
4 years ago
]
},
{
"source": [
"Try some different correlations"
],
"cell_type": "markdown",
"metadata": {}
},
4 years ago
{
"cell_type": "code",
"execution_count": 50,
4 years ago
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"0.32363971816089226\n0.6061712937226021\n"
4 years ago
]
}
],
"source": [
"print(new_pumpkins['City'].corr(new_pumpkins['Price']))\n",
"\n",
"print(new_pumpkins['Package'].corr(new_pumpkins['Price']))\n"
4 years ago
]
},
{
"source": [
"Drop unused columns"
],
"cell_type": "markdown",
"metadata": {}
},
4 years ago
{
"cell_type": "code",
"execution_count": 51,
4 years ago
"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"
]
},
{
"source": [
"Create a new dataframe"
],
"cell_type": "markdown",
"metadata": {}
},
4 years ago
{
"cell_type": "code",
"execution_count": 52,
4 years ago
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" Package Price\n",
"70 0 13.636364\n",
"71 0 16.363636\n",
"72 0 16.363636\n",
"73 0 15.454545\n",
"74 0 13.636364\n",
4 years ago
"... ... ...\n",
"1738 2 30.000000\n",
"1739 2 28.750000\n",
"1740 2 25.750000\n",
"1741 2 24.000000\n",
"1742 2 24.000000\n",
4 years ago
"\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>Package</th>\n <th>Price</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>70</th>\n <td>0</td>\n <td>13.636364</td>\n </tr>\n <tr>\n <th>71</th>\n <td>0</td>\n <td>16.363636</td>\n </tr>\n <tr>\n <th>72</th>\n <td>0</td>\n <td>16.363636</td>\n </tr>\n <tr>\n <th>73</th>\n <td>0</td>\n <td>15.454545</td>\n </tr>\n <tr>\n <th>74</th>\n <td>0</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>2</td>\n <td>30.000000</td>\n </tr>\n <tr>\n <th>1739</th>\n <td>2</td>\n <td>28.750000</td>\n </tr>\n <tr>\n <th>1740</th>\n <td>2</td>\n <td>25.750000</td>\n </tr>\n <tr>\n <th>1741</th>\n <td>2</td>\n <td>24.000000</td>\n </tr>\n <tr>\n <th>1742</th>\n <td>2</td>\n <td>24.000000</td>\n </tr>\n </tbody>\n</table>\n<p>415 rows × 2 columns</p>\n</div>"
4 years ago
},
"metadata": {},
"execution_count": 52
4 years ago
}
],
"source": [
"new_columns = ['Package', 'Price']\n",
"lin_pumpkins = new_pumpkins.drop([c for c in new_pumpkins.columns if c not in new_columns], axis='columns')\n",
4 years ago
"\n",
"lin_pumpkins\n"
4 years ago
]
},
{
"source": [
"Set X and y arrays to correspond to Package and Price"
],
"cell_type": "markdown",
"metadata": {}
},
4 years ago
{
"cell_type": "code",
"execution_count": 53,
4 years ago
"metadata": {},
"outputs": [],
"source": [
"X = lin_pumpkins.values[:, :1]\n",
"y = lin_pumpkins.values[:, 1:2]\n"
4 years ago
]
},
{
"cell_type": "code",
"execution_count": 54,
4 years ago
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Model Accuracy: 0.3470558343642912\nCoefficients: [[4.88720357]]\nMean squared error: 62.582140520848164\nCoefficient of determination: 0.4133978531557345\n"
4 years ago
]
}
],
"source": [
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error\n",
4 years ago
"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": 55,
4 years ago
"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=\"262.19625pt\" version=\"1.1\" viewBox=\"0 0 382.603125 262.19625\" width=\"382.603125pt\" 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 262.19625 \nL 382.603125 262.19625 \nL 382.603125 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 40.603125 224.64 \nL 375.403125 224.64 \nL 375.403125 7.2 \nL 40.603125 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=\"m6ae6aaec88\" style=\"stroke:#000000;\"/>\n </defs>\n <g clip-path=\"url(#p9c2f50fa15)\">\n <use style=\"stroke:#000000;\" x=\"55.821307\" xlink:href=\"#m6ae6aaec88\" y=\"202.581672\"/>\n <use style=\"stroke:#000000;\" x=\"55.821307\" xlink:href=\"#m6ae6aaec88\" y=\"192.62056\"/>\n <use style=\"stroke:#000000;\" x=\"208.003125\" xlink:href=\"#m6ae6aaec88\" y=\"82.826973\"/>\n <use style=\"stroke:#000000;\" x=\"55.821307\" xlink:href=\"#m6ae6aaec88\" y=\"202.581672\"/>\n <use style=\"stroke:#000000;\" x=\"208.003125\" xlink:href=\"#m6ae6aaec88\" y=\"63.347466\"/>\n <use style=\"stroke:#000000;\" x=\"208.003125\" xlink:href=\"#m6ae6aaec88\" y=\"92.566727\"/>\n <use style=\"stroke:#000000;\" x=\"55.821307\" xlink:href=\"#m6ae6aaec88\" y=\"188.193399\"/>\n <use style=\"stroke:#000000;\" x=\"208.003125\" xlink:href=\"#m6ae6aaec88\" y=\"151.005249\"/>\n <use style=\"stroke:#000000;\" x=\"208.003125\" xlink:href=\"#m6ae6aaec88\" y=\"92.566727\"/>\n <use style=\"stroke:#000000;\" x=\"55.821307\" xlink:href=\"#m6ae6aaec88\" y=\"179.339078\"/>\n <use style=\"stroke:#000000;\" x=\"208.003125\" xlink:href=\"#m6ae6aaec88\" y=\"121.785988\"/>\n <use style=\"stroke:#000000;\" x=\"55.821307\" xlink:href=\"#m6ae6aaec88\" y=\"192.62056\"/>\n <use style=\"stroke:#000000;\" x=\"208.003125\" xlink:href=\"#m6ae6aaec88\" y=\"92.566727\"/>\n <use style=\"stroke:#000000;\" x=\"208.003125\" xlink:href=\"#m6ae6aaec88\" y=\"107.176358\"/>\n <use style=\"stroke:#000000;\" x=\"55.821307\" xlink:href=\"#m6ae6aaec88\" y=\"199.261301\"/>\n <use style=\"stroke:#000000;\" x=\"208.003125\" xlink:href=\"#m6ae6aaec88\" y=\"102.306481\"/>\n <use style=\"stroke:#000000;\" x=\"208.003125\" xlink:href=\"#m6ae6aaec88\" y=\"107.176358\"/>\n <use style=\"stroke:#000000;\" x=\"208.003125\" xlink:href=\"#m6ae6aaec88\" y=\"107.176358\"/>\n <use style=\"stroke:#000000;\" x=\"208.003125\" xlink:href=\"#m6ae6aaec88\" y=\"92.566727\"/>\n <use style=\"stroke:#000000;\" x=\"55.821307\" xlink:href=\"#m6ae6aaec88\" y=\"192.62056\"/>\n <use style=\"stroke:#000000;\" x=\"208.003125\" xlink:href=\"#m6ae6aaec88\" y=\"121.785988\"/>\n <use style=\"stroke:#000000;\" x=\"55.821307\" xlink:href=\"#m6ae6aaec88\" y=\"192.62056\"/>\n <use style=\"stroke:#000000;\" x=\"131.912216\" xlink:href=\"#m6ae6aaec88\" y=\"199.261301\"/>\n <use style=\"stroke:#000000;\" x=\"55.821307\" xlink:href=\"#m6ae6aaec88\" y=\"214.756364\"/>\n <use style=\"stroke:#000000;\" x=\"131.912216\" xlink:href=\"#m6ae6aaec88\" y=\"199.261301\"/>\n <use style=\"stroke:#000000;\" x=\"208.003125\" xlink:href=\"#m6ae6aaec88\" y=\"102.306481\"/>\n <use style=\"stroke:#000000;\" x=\"360.184943\" xlink:href=\"#
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEGCAYAAABiq/5QAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAevUlEQVR4nO3df3RkZZ3n8fcnP1CCKN0S2h6aJIooCygtZDgqnD1Ih11AV3QGZ3GC20ddg4mcgzp7FM2KeDTrjzOK4+ymd6PAtJqZUVdn5SCodPghOjNgNyK/WhQ1aYGWboQW2uBAJ9/9o2461elUp2513apU3c/rnDqpeureut++hE8//dxbz6OIwMzM8qOl3gWYmVltOfjNzHLGwW9mljMOfjOznHHwm5nlTFu9CyjHkUceGT09PfUuw8ysoWzZsuWxiOhc2N4Qwd/T08PmzZvrXYaZWUORNLVYu4d6zMxyxsFvZpYzDn4zs5xx8JuZ5YyD38wsZxz8Zpa58fFxenp6aGlpoaenh/Hx8XqXlGsNcTunmTWu8fFxBgYGmJ6eBmBqaoqBgQEA+vv761labrnHb2aZGh4e3hv6c6anpxkeHq5TRebgN7NMbdu2LVW7Zc/Bb2aZ6urqStVu2XPwm1mmRkZG6Ojo2Keto6ODkZGROlVkDn4zy1R/fz9jY2N0d3cjie7ubsbGxnxht46U5Zq7kiaBp4AZYE9E9EpaCXwN6AEmgb+IiCcO9Dm9vb3hSdrMzNKRtCUiehe216LH/7qIWFt08MuAiYg4DphIXpuZWY3UY6jnfGBj8nwj8KY61GBmlltZB38A35e0RdJA0rYqIrYDJD+PyrgGMzMrkvU3d0+PiEckHQXcKOln5e6Y/E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
}
4 years ago
}
],
"source": [
"\n",
"plt.scatter(X_test, y_test, color='black')\n",
"plt.plot(X_test, pred, color='blue', linewidth=3)\n",
"\n",
"plt.xlabel('Package')\n",
"plt.ylabel('Price')\n",
4 years ago
"\n",
"plt.show()\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[34.78078482]])"
]
},
"metadata": {},
"execution_count": 56
}
],
"source": [
"lin_reg.predict( np.array([ [3] ]) )"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" Month Variety City Package Price\n",
"70 1 3 1 0 13.636364\n",
"71 1 3 1 0 16.363636\n",
"72 2 3 1 0 16.363636\n",
"73 2 3 1 0 15.454545\n",
"74 2 3 1 0 13.636364\n",
"... ... ... ... ... ...\n",
"1738 1 1 9 2 30.000000\n",
"1739 1 1 9 2 28.750000\n",
"1740 1 1 9 2 25.750000\n",
"1741 1 1 9 2 24.000000\n",
"1742 1 1 9 2 24.000000\n",
"\n",
"[415 rows x 5 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>Month</th>\n <th>Variety</th>\n <th>City</th>\n <th>Package</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>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>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>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>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>13.636364</td>\n </tr>\n <tr>\n <th>...</th>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n </tr>\n <tr>\n <th>1738</th>\n <td>1</td>\n <td>1</td>\n <td>9</td>\n <td>2</td>\n <td>30.000000</td>\n </tr>\n <tr>\n <th>1739</th>\n <td>1</td>\n <td>1</td>\n <td>9</td>\n <td>2</td>\n <td>28.750000</td>\n </tr>\n <tr>\n <th>1740</th>\n <td>1</td>\n <td>1</td>\n <td>9</td>\n <td>2</td>\n <td>25.750000</td>\n </tr>\n <tr>\n <th>1741</th>\n <td>1</td>\n <td>1</td>\n <td>9</td>\n <td>2</td>\n <td>24.000000</td>\n </tr>\n <tr>\n <th>1742</th>\n <td>1</td>\n <td>1</td>\n <td>9</td>\n <td>2</td>\n <td>24.000000</td>\n </tr>\n </tbody>\n</table>\n<p>415 rows × 5 columns</p>\n</div>"
},
"metadata": {},
"execution_count": 57
}
],
"source": [
"new_columns = ['Variety', 'Package', 'City', 'Month', 'Price']\n",
"poly_pumpkins = new_pumpkins.drop([c for c in new_pumpkins.columns if c not in new_columns], axis='columns')\n",
"\n",
"poly_pumpkins"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<pandas.io.formats.style.Styler at 0x7ffe1145eb20>"
],
"text/html": "<style type=\"text/css\" >\n #T_527ee678_875b_11eb_9f69_acde48001122row0_col0 {\n background-color: #b40426;\n color: #f1f1f1;\n } #T_527ee678_875b_11eb_9f69_acde48001122row0_col1 {\n background-color: #ead4c8;\n color: #000000;\n } #T_527ee678_875b_11eb_9f69_acde48001122row0_col2 {\n background-color: #4961d2;\n color: #f1f1f1;\n } #T_527ee678_875b_11eb_9f69_acde48001122row0_col3 {\n background-color: #9bbcff;\n color: #000000;\n } #T_527ee678_875b_11eb_9f69_acde48001122row0_col4 {\n background-color: #bbd1f8;\n color: #000000;\n } #T_527ee678_875b_11eb_9f69_acde48001122row1_col0 {\n background-color: #9fbfff;\n color: #000000;\n } #T_527ee678_875b_11eb_9f69_acde48001122row1_col1 {\n background-color: #b40426;\n color: #f1f1f1;\n } #T_527ee678_875b_11eb_9f69_acde48001122row1_col2 {\n background-color: #3b4cc0;\n color: #f1f1f1;\n } #T_527ee678_875b_11eb_9f69_acde48001122row1_col3 {\n background-color: #3b4cc0;\n color: #f1f1f1;\n } #T_527ee678_875b_11eb_9f69_acde48001122row1_col4 {\n background-color: #3b4cc0;\n color: #f1f1f1;\n } #T_527ee678_875b_11eb_9f69_acde48001122row2_col0 {\n background-color: #3b4cc0;\n color: #f1f1f1;\n } #T_527ee678_875b_11eb_9f69_acde48001122row2_col1 {\n background-color: #a9c6fd;\n color: #000000;\n } #T_527ee678_875b_11eb_9f69_acde48001122row2_col2 {\n background-color: #b40426;\n color: #f1f1f1;\n } #T_527ee678_875b_11eb_9f69_acde48001122row2_col3 {\n background-color: #edd2c3;\n color: #000000;\n } #T_527ee678_875b_11eb_9f69_acde48001122row2_col4 {\n background-color: #f5c0a7;\n color: #000000;\n } #T_527ee678_875b_11eb_9f69_acde48001122row3_col0 {\n background-color: #455cce;\n color: #f1f1f1;\n } #T_527ee678_875b_11eb_9f69_acde48001122row3_col1 {\n background-color: #6485ec;\n color: #000000;\n } #T_527ee678_875b_11eb_9f69_acde48001122row3_col2 {\n background-color: #ccd9ed;\n color: #000000;\n } #T_527ee678_875b_11eb_9f69_acde48001122row3_col3 {\n background-color: #b40426;\n color: #f1f1f1;\n } #T_527ee678_875b_11eb_9f69_acde48001122row3_col4 {\n background-color: #f08a6c;\n color: #000000;\n } #T_527ee678_875b_11eb_9f69_acde48001122row4_col0 {\n background-color: #445acc;\n color: #f1f1f1;\n } #T_527ee678_875b_11eb_9f69_acde48001122row4_col1 {\n background-color: #3b4cc0;\n color: #f1f1f1;\n } #T_527ee678_875b_11eb_9f69_acde48001122row4_col2 {\n background-color: #d2dbe8;\n color: #000000;\n } #T_527ee678_875b_11eb_9f69_acde48001122row4_col3 {\n background-color: #f39778;\n color: #000000;\n } #T_527ee678_875b_11eb_9f69_acde48001122row4_col4 {\n background-color: #b40426;\n color: #f1f1f1;\n }</style><table id=\"T_527ee678_875b_11eb_9f69_acde48001122\" ><thead> <tr> <th class=\"blank level0\" ></th> <th class=\"col_heading level0 col0\" >Month</th> <th class=\"col_heading level0 col1\" >Variety</th> <th class=\"col_heading level0 col2\" >City</th> <th class=\"col_heading level0 col3\" >Package</th> <th class=\"col_heading level0 col4\" >Price</th> </tr></thead><tbody>\n <tr>\n <th id=\"T_527ee678_875b_11eb_9f69_acde48001122level0_row0\" class=\"row_heading level0 row0\" >Month</th>\n <td id=\"T_527ee678_875b_11eb_9f69_a
},
"metadata": {},
"execution_count": 58
}
],
"source": [
"corr = poly_pumpkins.corr()\n",
"corr.style.background_gradient(cmap='coolwarm')"
]
},
{
"source": [
"Select the Package/Price columns"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [],
"source": [
"X=poly_pumpkins.iloc[:,3:4].values\n",
"y=poly_pumpkins.iloc[:,4:5].values\n"
]
},
{
"source": [
"Create Polynomial Regression model"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 60,
"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=\"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=\"m7aa789a087\" style=\"stroke:#000000;\"/>\n </defs>\n <g clip-path=\"url(#pdeb33c423d)\">\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"201.474881\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"188.193399\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"188.193399\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"192.62056\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"201.474881\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"188.193399\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"192.62056\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"189.300189\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"201.474881\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"192.62056\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"189.300189\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"201.474881\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"192.62056\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"189.300189\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"201.474881\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"194.83414\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"192.62056\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"188.193399\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"197.047721\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"188.193399\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"197.047721\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"197.047721\"/>\n <use style=\"stroke:#000000;\" x=\"42.143182\" xlink:href=\"#m7aa789a087\" y=\"197.047721\"/>\n <use style=\"stroke:#000000;\" x=\"194.325\" xlink:href=\"#m7aa789a087\" y=\"92.566727\"/>\n <use style=\"stroke:#000000;\" x=\"194.325\" xlink:href=\"#m7aa789a087\" y=\"121.785988\"/>\n <use style=\"stroke:#000000;\" x=\"194.325\" xlink:href=\"#m7aa789a087\" y=\"92.566727\"/>\n <use style=\"stroke:#000000;\" x=\"194.325\" xlink:href=\"#m7aa789a087\" y=\"121.785988\"/>\n <u
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nO3deXyU5bn/8c+VBMSICgpaFkmO1lrjhorLr1p3u6gVbK1Lo6JQo6A97lZJ3WqDR+u+gMblFEuse0VptSrgUU/rElq3iIq1BA0cwQpuUYHk+v1xTyB7ZpKZeWb5vl+veWXmzsw8l4/DlWfu7TJ3R0REsk9B1AGIiEjvKIGLiGQpJXARkSylBC4ikqWUwEVEslRROg82ZMgQLy0tTechRUSy3vz58z9y96Ht29OawEtLS6mtrU3nIUVEsp6Z1XfWri4UEZEspQQuIpKllMBFRLKUEriISJZSAhcRyVJK4CISt5qaGkpLSykoKKC0tJSampqoQ8praZ1GKCLZq6amhoqKChobGwGor6+noqICgPLy8ihDy1u6AheRuFRWVq5N3i0aGxuprKyMKCJRAheRuCxevDihdkk9JXARicuoUaMSapfUUwIXkbhUVVXRr1+/Nm39+vWjqqoqoohECVxE4mZm3T6W9IorgZvZIjN73cxeMbPaWNsmZvaUmS2M/Ryc2lBFJEqVlZWsWrWqTduqVas0iBmhRK7A93f30e4+Jvb4AmCOu28NzIk9FpEcpUHMzNOXLpSxwIzY/RnAuL6HIyKZSoOYmSfeBO7Ak2Y238wqYm2bu/tSgNjPzVIRoIhkhqqqKoqLi9u0FRcX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yVwyVtdJetsSOKLFoWCw088AdOnh4RYVBR1VPG58sowpXHixLA3i/SeErhIlvnrX8Oy+A8+CAn81FOjjigxG20Et94aVme220ZFEqQELpJFZs4MBYc33jh0QRx0UNQR9c4hh8Bxx4Vpjq++GnU02UsJXCQLNDeHFY3HHx/2F3nhBdhmm6ij6pvrr4dNNgldKWvWRB1NdlICF8lwX3wBRx0FU6fCz38Of/lLx4LD2WjTTeHmm8MOiddeG3U02UkJXPJWVztxJrJDZ6o1NMA++4TyZ9deG+Z7d1VwOBsdeSSMGweXXALvvBN1NNlHCVzyWmeFYjPF/PlhsPKdd8IUwbPOyr0ak2YwbVooLPHzn4euIomfErhIBnroIfjud6FfvzDr5NBDo44odYYNC98unnsuzE6R+MWdwM2s0Mz+YWazY483MbOnzGxh7GenRY1FMlmmLeRxD1PrjjwSRo8Oe3jvsEOkIaXFiSfCwQfDL38JnRS+ly4kcgV+BrCg1eMLgDnuvjUwJ/ZYJGtk2kKer76CE06AX/0KysvDpk+bbRZJKGlnFvr33cO89gzqycpocSVwMxsJHArc0ap5LDAjdn8GMC65oYnkj2XLQsHhmTPhN7+B3/8+sYLDuaC0NMwLf+KJ8N8vPYv3Cvx64Hyg9RDD5i1l1GI/O71WMLMKM6s1s9rly5f3KViRXPT662Gw8h//gAceCPO9c22wMl6nnRbmuZ95Jvzf/0UdTebrMYGb2WHAMnef35sDuHu1u49x9zFDVdVUpI0//SkkrNWrQxGGI4+MOqJoFRSEOpqNjfCLX0QdTeaL5wp8L+BwM1sE3AscYGYzgQ/NbBhA7OeylEUpkmPcwy6Chx8O3/pWGKwc06HmeH769rfDvPAHHwzz36VrPSZwd7/Q3Ue6eylwDDDX3Y8DHgVaan6MB2alLEqRFIhqIc/q1WH/7rPPDotYnn0WRoxI6SGzzrnnhlk4kyeHCkPSub7MA/8v4GAzWwgcHHssklXSvZDn449D5Zzbb4cpU0Kfd5QFhzN
},
"metadata": {
"needs_background": "light"
}
}
],
"source": [
"from sklearn.preprocessing import PolynomialFeatures\n",
"from sklearn.pipeline import make_pipeline\n",
"\n",
"pipeline = make_pipeline(PolynomialFeatures(4), LinearRegression())\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)\n",
"\n",
"pipeline.fit(np.array(X_train), y_train)\n",
"\n",
"y_pred=pipeline.predict(X_test)\n",
"\n",
"df = pd.DataFrame({'x': X_test[:,0], 'y': y_pred[:,0]})\n",
"df.sort_values(by='x',inplace = True)\n",
"points = pd.DataFrame(df).to_numpy()\n",
"\n",
"plt.plot(points[:, 0], points[:, 1],color=\"blue\")\n",
"\n",
"plt.scatter(X,y, color=\"black\")\n",
"\n",
"plt.show()\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Model Accuracy: 0.8537946517073784\n"
]
}
],
"source": [
"accuracy_score = pipeline.score(X_train,y_train)\n",
"print('Model Accuracy: ', accuracy_score)\n",
"\n"
]
},
4 years ago
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
]
}