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.
777 lines
121 KiB
777 lines
121 KiB
2 years ago
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"___\n",
|
||
|
"\n",
|
||
|
"<a href='http://www.pieriandata.com'><img src='../Pierian_Data_Logo.png'/></a>\n",
|
||
|
"___\n",
|
||
|
"<center><em>Copyright by Pierian Data Inc.</em></center>\n",
|
||
|
"<center><em>For more information, visit us at <a href='http://www.pieriandata.com'>www.pieriandata.com</a></em></center>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Introduction to Simple Linear Regression\n",
|
||
|
"\n",
|
||
|
"In this very simple example, we'll explore how to create a very simple fit line, the classic case of y=mx+b. We'll go carefully through each step, so you can see what type of question a simple fit line can answer. Keep in mind, this case is very simplified and is not the approach we'll take later on, its just here to get you thinking about linear regression in perhaps the same way [Galton](https://en.wikipedia.org/wiki/Francis_Galton) did."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Imports"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import numpy as np\n",
|
||
|
"import pandas as pd\n",
|
||
|
"import matplotlib.pyplot as plt\n",
|
||
|
"import seaborn as sns"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Sample Data\n",
|
||
|
"\n",
|
||
|
"This sample data is from ISLR. It displays sales (in thousands of units) for a particular product as a function of advertising budgets (in thousands of dollars) for TV, radio, and newspaper media."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 9,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"df = pd.read_csv(\"Advertising.csv\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 10,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"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>9.3</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>18.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>12.9</td>\n",
|
||
|
" </tr>\n",
|
||
|
" </tbody>\n",
|
||
|
"</table>\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 9.3\n",
|
||
|
"3 151.5 41.3 58.5 18.5\n",
|
||
|
"4 180.8 10.8 58.4 12.9"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 10,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"df.head()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"**Is there a relationship between *total* advertising spend and *sales*?**"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 11,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"df['total_spend'] = df['TV'] + df['radio'] + df['newspaper']"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 12,
|
||
|
"metadata": {
|
||
|
"scrolled": true
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"<AxesSubplot:xlabel='total_spend', ylabel='sales'>"
|
||
|
]
|
||
|
},
|
||
|
"exec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2ttz6Hz2MTScbyOX0QaAfwEwH2qullE+lS1Oennvarakuk5mOOnYiuXzVh2O9I7hPlrXgMAzJ3WjG9c0zGuX893b7oIjbVe/LZ7EPV+L4ZGw/hIaz1mtEYPa4mniAJ+L0IRxVgoknc+nbX3zitJHb+I+ABsAvADVd0cu/m4iExW1WMiMhlAt5NjIMqHWZdHtwel5F42uw714YGX92H1wtk4v70RAZ8XXg/w2YfNg3F8A5gdb4qsvS8dx1I9IiIAvg/gXVX9dtKPXgRwc+zrmwG84NQYiPLlZFAqZQopteyxZ2AE50ysw9TmANqaahEcNf69I5FIYswfnB7Gg1v3Fdz6mLX3pePkjH8+gCUAfikiv4jd9jcAvgXgWRG5DcDvAXzBwTEQ5cWpLo+lTiFlO/HK6Pe+elY7TgyOYtnTZ45pXLNoDnr6R7HrUB+A/N4Us/UNIuewVw+RAacCtFFe++pZ7bj3s7Nt63lvVM8OwFKNey61/asWzMKyp7sS3+eTBmPtvbPYq4coB4WeBWsmNYU0d1ozbp53XmJzVKFvMGZvWLU1Htz02FuWXqO2xoPVC2cnFncjgGH6J/6GUshMnbX3pcHAT2TCiaCUmkpZfsX5iaoaoPBFZLNF6dULZ1t6jZODo4k3iLjHb7nYMO01pTmA7Suv5Ezdhdirh6iIUhdXWxv8hrPp4Fg4r0Vfs0Xper837TajnLzR4x96dT/W35he23/OhDqc21KPtqZaBn2X4YyfqIhSU0giYjibPtoXxOBIKOeUj9mi9NDo+CBvtlBt9PiegRFMbq6zPe1FpcMZP1GRxVNI57bU45wJdWk7YtcunoM6nwcPbt2XsUTSqCzUrEvlR1rrLe26NXt8c8CfGDNn+O7Hqh6iEvtwcATvHDqFer8XfcExrNt2AD0DI1i1YBZ
|
||
|
"text/plain": [
|
||
|
"<Figure size 432x288 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"sns.scatterplot(x='total_spend',y='sales',data=df)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Least Squares Line\n",
|
||
|
"\n",
|
||
|
"Full formulas available on Wikipedia: https://en.wikipedia.org/wiki/Linear_regression ,as well as in ISLR reading."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"**Understanding what a line of best fit answers.**\n",
|
||
|
"If someone was to spend a total of $200 , what would the expected sales be? We have simplified this quite a bit by combining all the features into \"total spend\", but we will come back to individual features later on. For now, let's focus on understanding what a linear regression line can help answer.\n",
|
||
|
"\n",
|
||
|
"**Our next ad campaign will have a total spend of $200, how many units do we expect to sell as a result of this?**"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 13,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"<AxesSubplot:xlabel='total_spend', ylabel='sales'>"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 13,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEHCAYAAACp9y31AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy86wFpkAAAACXBIWXMAAAsTAAALEwEAmpwYAABNS0lEQVR4nO29e3wc9Xnv//7O7OxNq6sl+SYJYwMxl3CzMaQh1CFJU0JKkoYEDO0v/TWt6Wl60stpmpy2oSk9/f3CaZo2aWiDSdomJ2CSQFJo0pAmIY4hBQwGDBgbDLKx5Jus6660t9mZ7/ljdlcrebXalbTSSnrer5dfkkYzO8+OvJ/5zvN9vp9Haa0RBEEQl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PK1NIX9FZ270nLM+UCEXxCWCDOdfC31pNAzFCfgM8g4mpxmKQUpx52XSd3Zpq+01sRSGX744kk+/+NX8RmKhqCPgbEUX3j0EL/PuXnxPzIwxs49PfzkwKn8KtvGkMWNm9byvkvXEglUJpdhv7e6djblmNVChF8QlggzNSMr9aTQ2Rwm47gMjKXB9UTf0RqfYczLpO5s0leFnvj3PnUUn+E1n8+9v4TtcP/TPTTVWdz71FEeO9SfP7YtEuCmKzp4z5tXF7VFmApDKSJBr/5+Jqtz5wsRfkFYIszUjKzUk0LuZrKizk8smSGVcTENxXsuWsndu7v584deqqoN80zSV8U88U9EEzQEx9+f1hqtNS+fGOF3vvFsfntHs5cCetcFKyuqo7dMg4agRX1wfsoxZ4sIvyAsIWaykOoML55YCtvR+LPCd8cNF064mbxlfQsPPHtsXmyYK0lflWqCsrohxMB
|
||
|
"text/plain": [
|
||
|
"<Figure size 432x288 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Basically, we want to figure out how to create this line\n",
|
||
|
"sns.regplot(x='total_spend',y='sales',data=df)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Let's go ahead and start solving: $$y=mx+b$$"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Simply solve for m and b, remember, that as shown in the video, we are solving in a generalized form:\n",
|
||
|
"\n",
|
||
|
"$$ \\hat{y} = \\beta_0 + \\beta_1X$$\n",
|
||
|
"\n",
|
||
|
"Capitalized to signal that we are dealing with a matrix of values, we have a known matrix of labels (sales numbers) Y and a known matrix of total_spend (X). We are going to solve for the *beta* coefficients, which as we expand to more than just a single feature, will be important to build an understanding of what features have the most predictive power. We use y hat to indicate that y hat is a prediction or estimation, y would be a true label/known value.\n",
|
||
|
"\n",
|
||
|
"We can use NumPy for this (if you really wanted to, you could solve this by [hand](https://towardsdatascience.com/linear-regression-by-hand-ee7fe5a751bf))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 14,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"X = df['total_spend']\n",
|
||
|
"y = df['sales']"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 15,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Help on function polyfit in module numpy:\n",
|
||
|
"\n",
|
||
|
"polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False)\n",
|
||
|
" Least squares polynomial fit.\n",
|
||
|
" \n",
|
||
|
" Fit a polynomial ``p(x) = p[0] * x**deg + ... + p[deg]`` of degree `deg`\n",
|
||
|
" to points `(x, y)`. Returns a vector of coefficients `p` that minimises\n",
|
||
|
" the squared error in the order `deg`, `deg-1`, ... `0`.\n",
|
||
|
" \n",
|
||
|
" The `Polynomial.fit <numpy.polynomial.polynomial.Polynomial.fit>` class\n",
|
||
|
" method is recommended for new code as it is more stable numerically. See\n",
|
||
|
" the documentation of the method for more information.\n",
|
||
|
" \n",
|
||
|
" Parameters\n",
|
||
|
" ----------\n",
|
||
|
" x : array_like, shape (M,)\n",
|
||
|
" x-coordinates of the M sample points ``(x[i], y[i])``.\n",
|
||
|
" y : array_like, shape (M,) or (M, K)\n",
|
||
|
" y-coordinates of the sample points. Several data sets of sample\n",
|
||
|
" points sharing the same x-coordinates can be fitted at once by\n",
|
||
|
" passing in a 2D-array that contains one dataset per column.\n",
|
||
|
" deg : int\n",
|
||
|
" Degree of the fitting polynomial\n",
|
||
|
" rcond : float, optional\n",
|
||
|
" Relative condition number of the fit. Singular values smaller than\n",
|
||
|
" this relative to the largest singular value will be ignored. The\n",
|
||
|
" default value is len(x)*eps, where eps is the relative precision of\n",
|
||
|
" the float type, about 2e-16 in most cases.\n",
|
||
|
" full : bool, optional\n",
|
||
|
" Switch determining nature of return value. When it is False (the\n",
|
||
|
" default) just the coefficients are returned, when True diagnostic\n",
|
||
|
" information from the singular value decomposition is also returned.\n",
|
||
|
" w : array_like, shape (M,), optional\n",
|
||
|
" Weights to apply to the y-coordinates of the sample points. For\n",
|
||
|
" gaussian uncertainties, use 1/sigma (not 1/sigma**2).\n",
|
||
|
" cov : bool or str, optional\n",
|
||
|
" If given and not `False`, return not just the estimate but also its\n",
|
||
|
" covariance matrix. By default, the covariance are scaled by\n",
|
||
|
" chi2/sqrt(N-dof), i.e., the weights are presumed to be unreliable\n",
|
||
|
" except in a relative sense and everything is scaled such that the\n",
|
||
|
" reduced chi2 is unity. This scaling is omitted if ``cov='unscaled'``,\n",
|
||
|
" as is relevant for the case that the weights are 1/sigma**2, with\n",
|
||
|
" sigma known to be a reliable estimate of the uncertainty.\n",
|
||
|
" \n",
|
||
|
" Returns\n",
|
||
|
" -------\n",
|
||
|
" p : ndarray, shape (deg + 1,) or (deg + 1, K)\n",
|
||
|
" Polynomial coefficients, highest power first. If `y` was 2-D, the\n",
|
||
|
" coefficients for `k`-th data set are in ``p[:,k]``.\n",
|
||
|
" \n",
|
||
|
" residuals, rank, singular_values, rcond\n",
|
||
|
" Present only if `full` = True. Residuals is sum of squared residuals\n",
|
||
|
" of the least-squares fit, the effective rank of the scaled Vandermonde\n",
|
||
|
" coefficient matrix, its singular values, and the specified value of\n",
|
||
|
" `rcond`. For more details, see `linalg.lstsq`.\n",
|
||
|
" \n",
|
||
|
" V : ndarray, shape (M,M) or (M,M,K)\n",
|
||
|
" Present only if `full` = False and `cov`=True. The covariance\n",
|
||
|
" matrix of the polynomial coefficient estimates. The diagonal of\n",
|
||
|
" this matrix are the variance estimates for each coefficient. If y\n",
|
||
|
" is a 2-D array, then the covariance matrix for the `k`-th data set\n",
|
||
|
" are in ``V[:,:,k]``\n",
|
||
|
" \n",
|
||
|
" \n",
|
||
|
" Warns\n",
|
||
|
" -----\n",
|
||
|
" RankWarning\n",
|
||
|
" The rank of the coefficient matrix in the least-squares fit is\n",
|
||
|
" deficient. The warning is only raised if `full` = False.\n",
|
||
|
" \n",
|
||
|
" The warnings can be turned off by\n",
|
||
|
" \n",
|
||
|
" >>> import warnings\n",
|
||
|
" >>> warnings.simplefilter('ignore', np.RankWarning)\n",
|
||
|
" \n",
|
||
|
" See Also\n",
|
||
|
" --------\n",
|
||
|
" polyval : Compute polynomial values.\n",
|
||
|
" linalg.lstsq : Computes a least-squares fit.\n",
|
||
|
" scipy.interpolate.UnivariateSpline : Computes spline fits.\n",
|
||
|
" \n",
|
||
|
" Notes\n",
|
||
|
" -----\n",
|
||
|
" The solution minimizes the squared error\n",
|
||
|
" \n",
|
||
|
" .. math ::\n",
|
||
|
" E = \\sum_{j=0}^k |p(x_j) - y_j|^2\n",
|
||
|
" \n",
|
||
|
" in the equations::\n",
|
||
|
" \n",
|
||
|
" x[0]**n * p[0] + ... + x[0] * p[n-1] + p[n] = y[0]\n",
|
||
|
" x[1]**n * p[0] + ... + x[1] * p[n-1] + p[n] = y[1]\n",
|
||
|
" ...\n",
|
||
|
" x[k]**n * p[0] + ... + x[k] * p[n-1] + p[n] = y[k]\n",
|
||
|
" \n",
|
||
|
" The coefficient matrix of the coefficients `p` is a Vandermonde matrix.\n",
|
||
|
" \n",
|
||
|
" `polyfit` issues a `RankWarning` when the least-squares fit is badly\n",
|
||
|
" conditioned. This implies that the best fit is not well-defined due\n",
|
||
|
" to numerical error. The results may be improved by lowering the polynomial\n",
|
||
|
" degree or by replacing `x` by `x` - `x`.mean(). The `rcond` parameter\n",
|
||
|
" can also be set to a value smaller than its default, but the resulting\n",
|
||
|
" fit may be spurious: including contributions from the small singular\n",
|
||
|
" values can add numerical noise to the result.\n",
|
||
|
" \n",
|
||
|
" Note that fitting polynomial coefficients is inherently badly conditioned\n",
|
||
|
" when the degree of the polynomial is large or the interval of sample points\n",
|
||
|
" is badly centered. The quality of the fit should always be checked in these\n",
|
||
|
" cases. When polynomial fits are not satisfactory, splines may be a good\n",
|
||
|
" alternative.\n",
|
||
|
" \n",
|
||
|
" References\n",
|
||
|
" ----------\n",
|
||
|
" .. [1] Wikipedia, \"Curve fitting\",\n",
|
||
|
" https://en.wikipedia.org/wiki/Curve_fitting\n",
|
||
|
" .. [2] Wikipedia, \"Polynomial interpolation\",\n",
|
||
|
" https://en.wikipedia.org/wiki/Polynomial_interpolation\n",
|
||
|
" \n",
|
||
|
" Examples\n",
|
||
|
" --------\n",
|
||
|
" >>> import warnings\n",
|
||
|
" >>> x = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0])\n",
|
||
|
" >>> y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])\n",
|
||
|
" >>> z = np.polyfit(x, y, 3)\n",
|
||
|
" >>> z\n",
|
||
|
" array([ 0.08703704, -0.81349206, 1.69312169, -0.03968254]) # may vary\n",
|
||
|
" \n",
|
||
|
" It is convenient to use `poly1d` objects for dealing with polynomials:\n",
|
||
|
" \n",
|
||
|
" >>> p = np.poly1d(z)\n",
|
||
|
" >>> p(0.5)\n",
|
||
|
" 0.6143849206349179 # may vary\n",
|
||
|
" >>> p(3.5)\n",
|
||
|
" -0.34732142857143039 # may vary\n",
|
||
|
" >>> p(10)\n",
|
||
|
" 22.579365079365115 # may vary\n",
|
||
|
" \n",
|
||
|
" High-order polynomials may oscillate wildly:\n",
|
||
|
" \n",
|
||
|
" >>> with warnings.catch_warnings():\n",
|
||
|
" ... warnings.simplefilter('ignore', np.RankWarning)\n",
|
||
|
" ... p30 = np.poly1d(np.polyfit(x, y, 30))\n",
|
||
|
" ...\n",
|
||
|
" >>> p30(4)\n",
|
||
|
" -0.80000000000000204 # may vary\n",
|
||
|
" >>> p30(5)\n",
|
||
|
" -0.99999999999999445 # may vary\n",
|
||
|
" >>> p30(4.5)\n",
|
||
|
" -0.10547061179440398 # may vary\n",
|
||
|
" \n",
|
||
|
" Illustration:\n",
|
||
|
" \n",
|
||
|
" >>> import matplotlib.pyplot as plt\n",
|
||
|
" >>> xp = np.linspace(-2, 6, 100)\n",
|
||
|
" >>> _ = plt.plot(x, y, '.', xp, p(xp), '-', xp, p30(xp), '--')\n",
|
||
|
" >>> plt.ylim(-2,2)\n",
|
||
|
" (-2, 2)\n",
|
||
|
" >>> plt.show()\n",
|
||
|
"\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"help(np.polyfit)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 16,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"array([0.04868788, 4.24302822])"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 16,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Returns highest order coef first!\n",
|
||
|
"np.polyfit(X,y,1)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 17,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Potential Future Spend Budgets\n",
|
||
|
"potential_spend = np.linspace(0,500,100)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 21,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"predicted_sales = 0.04868788*potential_spend + 4.24302822"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 22,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"[<matplotlib.lines.Line2D at 0x1a948c95408>]"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 22,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy86wFpkAAAACXBIWXMAAAsTAAALEwEAmpwYAAAfgElEQVR4nO3deXhU5fn/8fcDBMISlgCBsISENYQkbGETF1TqAooi2mqtuGPtz29b2wpBXEBEca+ttYo7LWotSWQRFUQQdwWVbCQQIIQlJKwhJIQs8/z+yGgpZQnJTE5m5vO6rlwz8+SEc98j+XA8OeeOsdYiIiK+p5HTBYiISO0owEVEfJQCXETERynARUR8lAJcRMRHNanPnXXo0MFGRkbW5y5FRHzeunXr9lprOx6/Xq8BHhkZydq1a+tzlyIiPs8Ys+1E6zqFIiLioxTgIiI+SgEuIuKjFOAiIj5KAS4i4qMU4CIiPkoBLiLioxTgIiJedKCknFlLMjhUVuHxP7teb+QREQkU1lqWpe3mwcXpHCytYHSvDoyN6eTRfSjARUQ8rPBQGfcvSufDjALiurZh/i0jiOnS2uP7UYCLiHiItZZ/r93B7PcyKa90Mf3SaG49O4omjb1ztloBLiLiAXn7SpmeksrnOfsYHhXKY5PiierQ0qv7VICLiNRBlcvy+he5PPlhNo0bGR6+MpZfDo+gUSPj9X0rwEVEamlTQTFTk1L5Pu8g5/fryJyJcXRp27ze9q8AFxE5Q+WVLl74ZDN//XgTrZo14c+/GMQVg7pgjPePuo+lABcROQPrtx9kWlIqWbuLuXxgFx68PIYOrZo5UosCXESkBo6UV/HMRxt5+dMtdAxpxkuTE/iZh6/rPlMKcBGR0/hqyz4Sk1LJ3VfKdcO7M31cf1oHBzldlgJcRORkDpVVMPf9LN78Oo+I0Ba8edsIzurdwemyfqIAFxE5gZUbCpiRkk5hc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beKlT7fQvmVTXrxhKBcP6Ox0WXKGFOAiAeabrfuZlpTK1r0l/CKhO/eO70+b5ho+5YsU4CIBorisgsc+yOKfX+XRPbQ5C24bwejeHZwuS+pAAS4SAFZlFzIjOY38Q2XcenYUf7yoLy2a6tvf1+m/oIgf219SzuylmaR8v5M+Ya1IuvMshkS0c7os8RAFuIgfstayNDWfmYszKDpSwW8v7MP/O78XzZpo+JQ/UYCL+JmCQ2XMSEnnow0FxHdrw4LbRxDdubXTZYkXKMBF/IS1ln99u505yzZQXulixrj+3Dw6UsOn/JgCXMQP5O0rJTE5lS8272NEVCiPTYonskNLp8sSLzttgBtjugPzgc6AC5hnrX3WGBMK/AuIBHKBn1trD3ivVBE5XpXL8voXuTz5YTaNGxkemRjHtcO6a/hUgKjJEXgl8Edr7XfGmBBgnTFmBXATsNJaO9cYkwgkAtO8V6qIHGtjQfXwqR+2H+SC6DDmTIwlvI2GTwWS0wa4tTYfyHc/LzbGbAC6AlcAY9ybvQGsRgEu4nXllS7+vnozz63aRKtmTXj22kFMGNhFw6cC0BmdAzfGRAKDga+BTu5wx1qbb4wJO8nXTAGmAERERNSpWJFAt377QaYlpZK1u5jLB3Zh5uUxtG+l4VOBqsYBboxpBSQBv7fWHqrpv/bW2nnAPICEhARbmyJFAt2R8iqe+WgjL3+6hbCQYF6enMDYmE5OlyUOq1GAG2OCqA7vBdbaZPdygTEm3H30HQ4UeqtIkUD25eZ9TE9OJXdfKdcN7870cf1pHazhU1Kzq1AM8AqwwVr79DGfWgzcCMx1Py7ySoUiAepQWQVz38/iza/z6NG+BW/ePoKzemn4lPxHTY7ARwM3AGnGmB/ca/dSHdzvGGNuBfKAa7xSoUgAWrmhgBkp6RQWl3H
|
||
|
"text/plain": [
|
||
|
"<Figure size 432x288 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"plt.plot(potential_spend,predicted_sales)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 24,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"[<matplotlib.lines.Line2D at 0x1a948dc6bc8>]"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 24,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEHCAYAAACp9y31AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy86wFpkAAAACXBIWXMAAAsTAAALEwEAmpwYAAA+tklEQVR4nO3deXjTVdbA8e9N27RpC7SUFtkUZACtiGIRWVxwQx0ZkcWdRREBK4obgqMovsjIJi7jAqgIqIwbMCJuKDPIiApSEEQWERHZpKW0ULqlbe77xy8JaZukaZs0bXI+z8PTNk1+S2c8uTn33HOV1hohhBDhwxTsCxBCCFG3JPALIUSYkcAvhBBhRgK/EEKEGQn8QggRZiKDfQG+aNasmW7btm2wL0MIIRqUjIyMI1rr5IqPN4jA37ZtWzZs2BDsyxBCiAZFKbXX3eOS6hFCiDAjgV8IIcKMBH4hhAgzEviFECLMSOAXQogwI4FfCCHCjAR+IYQIMxL4hRCiPsrPh2nToKDA74eWwC+EEPWJ1vDuu3DGGfDoo/DZZ34/hQR+IYSoLzZvhj594JZbICUFvvkGBg3y+2kk8AshRLBlZ0N6Opx3HmzbBvPmwfr10Lt3QE7XIHr1CCFESCotNYL8pElw7BiMHQuTJ0NiYkBPK4FfCCGC4euv4b77YMsWuOwyeOEF6Ny5Tk4tqR4hhKhLf/wBN91k5PJzc+HDD+Grr+os6IOM+IUQom4UFsKsWfDMM0blzpNPwiOPQGxsnV+KBH4hhAgkreHf/4Y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IUJS7Y4R88zB3dhxuc7Adi0L5eF3+7h/dE90Vr
|
||
|
"text/plain": [
|
||
|
"<Figure size 432x288 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"sns.scatterplot(x='total_spend',y='sales',data=df)\n",
|
||
|
"plt.plot(potential_spend,predicted_sales,color='red')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"**Our next ad campaign will have a total spend of $200, how many units do we expect to sell as a result of this?**"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 25,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"spend = 200\n",
|
||
|
"predicted_sales = 0.04868788*spend + 4.24302822"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 26,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"13.98060422"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 26,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"predicted_sales"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Further considerations...which we will explore in much more depth!"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Overfitting, Underfitting, and Measuring Performance\n",
|
||
|
"\n",
|
||
|
"Notice we fit to order=1 , essentially a straight line, we can begin to explore higher orders, but does higher order mean an overall better fit? Is it possible to fit too much? Too little? How would we know and how do we even define a good fit?"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 33,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"array([ 3.07615033e-07, -1.89392449e-04, 8.20886302e-02, 2.70495053e+00])"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 33,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"np.polyfit(X,y,3)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 34,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Potential Future Spend Budgets\n",
|
||
|
"potential_spend = np.linspace(0,500,100)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 35,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"predicted_sales = 3.07615033e-07*potential_spend**3 + -1.89392449e-04*potential_spend**2 + 8.20886302e-02*potential_spend**1 + 2.70495053e+00"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 40,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"[<matplotlib.lines.Line2D at 0x1a945c52908>]"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 40,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEHCAYAAACp9y31AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy86wFpkAAAACXBIWXMAAAsTAAALEwEAmpwYAABAhklEQVR4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tW8THqVQk1VYevT7n+L0VO263m725RYx8bU254wGlq3oKsrFdO9zaE/eqq2D6dC3VDFGwHH/EA7+INAU+AR4zxswTkTbAXsAA44G2xpjrKnoPvbmrokFmdiEDpq4MeOMyuVlcWI8VqCSysgBenddUdUz+F5d+3VJ44MJu2G0S/HhffGFV6mRkwJNPws03a5O1KggW+CNaxy8iscBc4HVjzDwAY8wev9+/ACyK5BiUqi9qM4/uvREa6ddA6BeMsjd0l2zMYOPu7MAXPrfbCvT33gsdOsDKldCrXPxS1RSxwC8iArwEbDLGPOX3eFtjzG7PjwOADZEag1L1Sb3pWhlGVUkRhXzhy8iw+ud/+KG1MOullyAxMcJn0rhEcgFXX2AocGaZ0s2JIvK9iKwHzgBuj+AYlKo3wtG1sr5taViVMtCQbiAvWwbHH2911Jw6Fd55JyJBv779HWtbxGb8xpjPgUDJuPcidUyl6rOadq2saHYNRDQ/H0xV0lfeC1+5RVsJDqvB2oMPwsSJ0LWrNdvv3j0iY66XW0PWMu3Vo1Qtqm4eHYLPrueN7sO+nKI6CWRVSV8FvfBt/RmuvhpWr4YRI+CppyAhIWJjDvZ3jMRN9vpKe/U
|
||
|
"text/plain": [
|
||
|
"<Figure size 432x288 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"sns.scatterplot(x='total_spend',y='sales',data=df)\n",
|
||
|
"plt.plot(potential_spend,predicted_sales,color='red')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"**Is this better than our straight line fit? What are good ways of measuring this?**"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Multiple Features\n",
|
||
|
"\n",
|
||
|
"The real data had 3 features, not everything in total spend, this would allow us to repeat the process and maybe get a more accurate result?"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 37,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"X = df[['TV','radio','newspaper']]\n",
|
||
|
"y = df['sales']"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 41,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"ename": "TypeError",
|
||
|
"evalue": "expected 1D vector for x",
|
||
|
"output_type": "error",
|
||
|
"traceback": [
|
||
|
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
||
|
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
|
||
|
"\u001b[1;32m<ipython-input-41-f24479bbc916>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Note here we're passing in 3 which matches up with 3 unique features, so we're not polynomial yet\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpolyfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
|
||
|
"\u001b[1;32m<__array_function__ internals>\u001b[0m in \u001b[0;36mpolyfit\u001b[1;34m(*args, **kwargs)\u001b[0m\n",
|
||
|
"\u001b[1;32mc:\\users\\marcial\\anaconda3\\envs\\ml_master\\lib\\site-packages\\numpy\\lib\\polynomial.py\u001b[0m in \u001b[0;36mpolyfit\u001b[1;34m(x, y, deg, rcond, full, w, cov)\u001b[0m\n\u001b[0;32m 597\u001b[0m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"expected deg >= 0\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 598\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mndim\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 599\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"expected 1D vector for x\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 600\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msize\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 601\u001b[0m \u001b[1;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"expected non-empty vector for x\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
|
||
|
"\u001b[1;31mTypeError\u001b[0m: expected 1D vector for x"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Note here we're passing in 3 which matches up with 3 unique features, so we're not polynomial yet\n",
|
||
|
"np.polyfit(X,y,1)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"**Uh oh! Polyfit only works with a 1D X array! We'll need to move on to a more powerful library...**"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"-------\n",
|
||
|
"--------"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"anaconda-cloud": {},
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3",
|
||
|
"language": "python",
|
||
|
"name": "python3"
|
||
|
},
|
||
|
"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.7.6"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 1
|
||
|
}
|