{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Multiple Regression from scratch"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: plotly in /usr/local/lib/python3.6/dist-packages (4.5.0)\n",
"Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from plotly) (1.14.0)\n",
"Requirement already satisfied: retrying>=1.3.3 in /usr/local/lib/python3.6/dist-packages (from plotly) (1.3.3)\n"
]
}
],
"source": [
"!pip3 install plotly"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from random import random\n",
"from typing import Union, List\n",
"from plotly import express as px\n",
"from plotly import graph_objects as go"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# The data set we're using (source: https://miabellaai.net/)\n",
"data = [\n",
" [1.15, 0.59, 4.18],\n",
" [-1.23, 1.65, -1],\n",
" [-4.77, 4.02, -1.77],\n",
" [0.57, 0.13, 6.06],\n",
" [3.29, 3.14, 2.43],\n",
" [2.22, 3.58, 4.49],\n",
" [-3.98, 1.15, -2.64],\n",
" [-0.98, 1.01, 1.87],\n",
" [-2.26, 1.09, 3.43],\n",
" [-0.96, 0.24, 1.03],\n",
" [-2.92, 1.62, 1.8],\n",
" [3.88, -1.13, 5.59],\n",
" [0.01, 2.66, 4.42],\n",
" [3.3, 1.04, 3.7],\n",
" [0.44, 0.14, 1.2],\n",
" [4.7, -0.73, 6.95],\n",
" [-0.05, 1.3, 0.93],\n",
" [3.74, -1.46, 3.97],\n",
" [-3.69, 2.85, -2.07],\n",
" [-4.39, 7.78, -2.8],\n",
" [2.95, -1.02, 2.7],\n",
" [1.19, -0.35, 4.24],\n",
" [3.83, -1.72, 3.25],\n",
" [-4.57, 2.72, -0.6],\n",
" [-2.07, 5.79, 0.4],\n",
" [-1.56, 1.34, -0.61],\n",
" [0.85, 0.07, 1.06],\n",
" [3.13, -0.98, 2.88],\n",
" [-2.22, 0.6, 1.53],\n",
" [-2.98, 2.43, 2.04],\n",
" [2.59, 4.8, 1.8],\n",
" [1.43, -0.91, 2.92],\n",
" [-3.48, 2.24, 2.44],\n",
" [2.69, 2.38, 7.48],\n",
" [0.42, 4.33, 4.32],\n",
" [1.75, -0.23, 3.57],\n",
" [-4.17, 2.25, -0.3],\n",
" [1.35, 0.13, 3.63],\n",
" [-3.68, 1.77, -1.43],\n",
" [-3.34, 4.32, 3.05],\n",
" [-0.79, 0.62, 1.33],\n",
" [4.56, -1.85, 3.36],\n",
" [-4.25, 6.17, 0.95],\n",
" [-2.96, 1.8, 4.44],\n",
" [3.36, -1.06, 2.76],\n",
" [1.13, 1.79, 4.03],\n",
" [0.07, 0.72, 3.46],\n",
" [3.94, 4.01, 7.62],\n",
" [-0.81, 6.04, 0.31],\n",
" [2.21, 4.37, 5.33],\n",
" [-3.11, 6.65, -0.5],\n",
" [3.88, -1.07, 7.86],\n",
" [0.82, -0.46, -0.07],\n",
" [4.27, -1.21, 3.77],\n",
" [-3.98, 8.22, -2.81],\n",
" [-0.54, 0.34, 2.92],\n",
" [-1.34, 2.23, 3.63],\n",
" [-4.96, 2.03, -2.55],\n",
" [3.2, -1.22, 3.18],\n",
" [-2.17, 5.18, 1.87],\n",
" [-4.13, 7.58, -1.77],\n",
" [2.82, 3.2, 7.1],\n",
" [-1.16, 1.14, 0.71],\n",
" [-4.22, 1.29, 1.58],\n",
" [-1.21, 0.9, 0.16],\n",
" [-2.53, 1.82, -1.66],\n",
" [-3.56, 5.63, -2.12],\n",
" [3.39, -0.33, 7.96],\n",
" [4.2, -0.8, 3.76],\n",
" [0.52, 2.22, 0.51],\n",
" [3.86, -0.22, 3.88],\n",
" [2.05, 5.4, 1.56],\n",
" [1.27, 3.06, 1.48],\n",
" [4.81, 0.65, 3.43],\n",
" [4.58, -0.91, 7.02],\n",
" [3.16, -0.23, 4.17],\n",
" [2.51, 0.19, 2.9],\n",
" [-4.09, 5.52, -2.09],\n",
" [2.61, -0.66, 1.98],\n",
" [4.86, 1.16, 5.41],\n",
" [4.24, 2.87, 5.67],\n",
" [-3.27, 3.01, 1.81],\n",
" [-2.43, 3.56, 4.22],\n",
" [1.34, 0.17, 3.5],\n",
" [-0.74, 1.17, 1.41],\n",
" [4.38, -2.08, 4.16],\n",
" [4.42, -0.21, 4.72],\n",
" [4.87, 2.71, 7.01],\n",
" [-1.69, 4.08, -0.38],\n",
" [0.34, 0.65, 1.18],\n",
" [1.4, 4.44, 0.79],\n",
" [4.28, 0.77, 7.04],\n",
" [1.36, 3.11, 0.87],\n",
" [0.42, 5.54, 2.76],\n",
" [0.61, 1.6, 2.93],\n",
" [-1.12, 2.63, 1.65],\n",
" [0.49, 2.54, -0.23],\n",
" [-3.19, 6.53, 2.05],\n",
" [-2.45, 4.7, 1.29],\n",
" [4.07, -1.54, 2.2]\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# Turn the data into a list of x vectors (one for every pair of x items) and a vector containing all the y items\n",
"xs: List[List[float]] = []\n",
"ys: List[float] = []\n",
"\n",
"for item in data:\n",
" x1: float = item[0]\n",
" x2: float = item[1]\n",
" y: float = item[2]\n",
" xs.append([x1, x2])\n",
" ys.append(y)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# A convenience function which creates a scatter plot with an optional hyperplane\n",
"def plot(xs: List[List[float]], ys: List[float], ys_pred: Union[List[float], None] = None) -> None:\n",
" # Translate our `xs` and `ys` into data Plotly understands\n",
" x: List[float] = [item[0] for item in xs] # x1\n",
" y: List[float] = [item[1] for item in xs] # x2\n",
" z: List[float] = ys\n",
" fig = px.scatter_3d(x=x, y=y, z=z, labels={'x': 'x1', 'y': 'x2', 'z': 'y'})\n",
" # If present, add the hyperplane\n",
" if ys_pred:\n",
" fig.add_trace(\n",
" go.Scatter3d(\n",
" x=x, y=y, z=ys_pred, name='Guess', surfaceaxis=1\n",
" )\n",
" )\n",
" fig.show()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"hoverlabel": {
"namelength": 0
},
"hovertemplate": "x1=%{x}
x2=%{y}
y=%{z}",
"legendgroup": "",
"marker": {
"color": "#636efa",
"symbol": "circle"
},
"mode": "markers",
"name": "",
"scene": "scene",
"showlegend": false,
"type": "scatter3d",
"x": [
1.15,
-1.23,
-4.77,
0.57,
3.29,
2.22,
-3.98,
-0.98,
-2.26,
-0.96,
-2.92,
3.88,
0.01,
3.3,
0.44,
4.7,
-0.05,
3.74,
-3.69,
-4.39,
2.95,
1.19,
3.83,
-4.57,
-2.07,
-1.56,
0.85,
3.13,
-2.22,
-2.98,
2.59,
1.43,
-3.48,
2.69,
0.42,
1.75,
-4.17,
1.35,
-3.68,
-3.34,
-0.79,
4.56,
-4.25,
-2.96,
3.36,
1.13,
0.07,
3.94,
-0.81,
2.21,
-3.11,
3.88,
0.82,
4.27,
-3.98,
-0.54,
-1.34,
-4.96,
3.2,
-2.17,
-4.13,
2.82,
-1.16,
-4.22,
-1.21,
-2.53,
-3.56,
3.39,
4.2,
0.52,
3.86,
2.05,
1.27,
4.81,
4.58,
3.16,
2.51,
-4.09,
2.61,
4.86,
4.24,
-3.27,
-2.43,
1.34,
-0.74,
4.38,
4.42,
4.87,
-1.69,
0.34,
1.4,
4.28,
1.36,
0.42,
0.61,
-1.12,
0.49,
-3.19,
-2.45,
4.07
],
"y": [
0.59,
1.65,
4.02,
0.13,
3.14,
3.58,
1.15,
1.01,
1.09,
0.24,
1.62,
-1.13,
2.66,
1.04,
0.14,
-0.73,
1.3,
-1.46,
2.85,
7.78,
-1.02,
-0.35,
-1.72,
2.72,
5.79,
1.34,
0.07,
-0.98,
0.6,
2.43,
4.8,
-0.91,
2.24,
2.38,
4.33,
-0.23,
2.25,
0.13,
1.77,
4.32,
0.62,
-1.85,
6.17,
1.8,
-1.06,
1.79,
0.72,
4.01,
6.04,
4.37,
6.65,
-1.07,
-0.46,
-1.21,
8.22,
0.34,
2.23,
2.03,
-1.22,
5.18,
7.58,
3.2,
1.14,
1.29,
0.9,
1.82,
5.63,
-0.33,
-0.8,
2.22,
-0.22,
5.4,
3.06,
0.65,
-0.91,
-0.23,
0.19,
5.52,
-0.66,
1.16,
2.87,
3.01,
3.56,
0.17,
1.17,
-2.08,
-0.21,
2.71,
4.08,
0.65,
4.44,
0.77,
3.11,
5.54,
1.6,
2.63,
2.54,
6.53,
4.7,
-1.54
],
"z": [
4.18,
-1,
-1.77,
6.06,
2.43,
4.49,
-2.64,
1.87,
3.43,
1.03,
1.8,
5.59,
4.42,
3.7,
1.2,
6.95,
0.93,
3.97,
-2.07,
-2.8,
2.7,
4.24,
3.25,
-0.6,
0.4,
-0.61,
1.06,
2.88,
1.53,
2.04,
1.8,
2.92,
2.44,
7.48,
4.32,
3.57,
-0.3,
3.63,
-1.43,
3.05,
1.33,
3.36,
0.95,
4.44,
2.76,
4.03,
3.46,
7.62,
0.31,
5.33,
-0.5,
7.86,
-0.07,
3.77,
-2.81,
2.92,
3.63,
-2.55,
3.18,
1.87,
-1.77,
7.1,
0.71,
1.58,
0.16,
-1.66,
-2.12,
7.96,
3.76,
0.51,
3.88,
1.56,
1.48,
3.43,
7.02,
4.17,
2.9,
-2.09,
1.98,
5.41,
5.67,
1.81,
4.22,
3.5,
1.41,
4.16,
4.72,
7.01,
-0.38,
1.18,
0.79,
7.04,
0.87,
2.76,
2.93,
1.65,
-0.23,
2.05,
1.29,
2.2
]
}
],
"layout": {
"legend": {
"tracegroupgap": 0
},
"margin": {
"t": 60
},
"scene": {
"domain": {
"x": [
0,
1
],
"y": [
0,
1
]
},
"xaxis": {
"title": {
"text": "x1"
}
},
"yaxis": {
"title": {
"text": "x2"
}
},
"zaxis": {
"title": {
"text": "y"
}
}
},
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "#E5ECF6",
"showlakes": true,
"showland": true,
"subunitcolor": "white"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "#E5ECF6",
"polar": {
"angularaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"radialaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"yaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"zaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"baxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"caxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
}
}
}
}
},
"text/html": [
"