feat: 2109s ps4
91
cs2109s/labs/ps4/housing_data.csv
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
floor_area_sqm,bedrooms,schools,asking_price
|
||||||
|
60,2,3,480000.0
|
||||||
|
63,2,4,450000.0
|
||||||
|
65,2,1,480000.0
|
||||||
|
65,2,4,360000.0
|
||||||
|
66,2,5,350000.0
|
||||||
|
67,2,0,400000.0
|
||||||
|
67,2,1,430000.0
|
||||||
|
67,2,2,450000.0
|
||||||
|
67,2,3,410000.0
|
||||||
|
67,2,4,390000.0
|
||||||
|
67,2,5,390000.0
|
||||||
|
67,2,6,390000.0
|
||||||
|
68,2,1,460000.0
|
||||||
|
68,2,2,400000.0
|
||||||
|
68,2,3,370000.0
|
||||||
|
68,2,4,360000.0
|
||||||
|
68,2,6,370000.0
|
||||||
|
68,2,7,400000.0
|
||||||
|
68,2,8,370000.0
|
||||||
|
69,2,3,420000.0
|
||||||
|
70,2,3,510000.0
|
||||||
|
70,2,8,370000.0
|
||||||
|
73,2,5,370000.0
|
||||||
|
73,2,7,380000.0
|
||||||
|
83,2,1,590000.0
|
||||||
|
83,3,1,650000.0
|
||||||
|
83,3,2,550000.0
|
||||||
|
83,3,3,550000.0
|
||||||
|
83,3,4,580000.0
|
||||||
|
84,3,3,560000.0
|
||||||
|
86,3,2,560000.0
|
||||||
|
88,3,3,510000.0
|
||||||
|
89,3,1,610000.0
|
||||||
|
90,3,3,500000.0
|
||||||
|
90,3,4,520000.0
|
||||||
|
90,3,5,490000.0
|
||||||
|
91,3,0,550000.0
|
||||||
|
91,3,3,620000.0
|
||||||
|
92,3,1,490000.0
|
||||||
|
92,3,2,590000.0
|
||||||
|
92,3,3,490000.0
|
||||||
|
92,3,4,510000.0
|
||||||
|
92,3,5,490000.0
|
||||||
|
92,3,6,490000.0
|
||||||
|
92,3,7,510000.0
|
||||||
|
93,3,0,490000.0
|
||||||
|
93,3,1,660000.0
|
||||||
|
93,3,2,540000.0
|
||||||
|
93,3,3,550000.0
|
||||||
|
93,3,4,490000.0
|
||||||
|
93,3,5,510000.0
|
||||||
|
93,3,6,490000.0
|
||||||
|
93,3,7,490000.0
|
||||||
|
94,3,2,590000.0
|
||||||
|
94,3,3,550000.0
|
||||||
|
94,3,4,490000.0
|
||||||
|
94,3,5,540000.0
|
||||||
|
95,3,1,700000.0
|
||||||
|
95,3,2,620000.0
|
||||||
|
95,3,3,530000.0
|
||||||
|
95,3,4,540000.0
|
||||||
|
96,3,2,600000.0
|
||||||
|
96,3,7,480000.0
|
||||||
|
97,3,1,660000.0
|
||||||
|
97,3,2,600000.0
|
||||||
|
97,3,4,440000.0
|
||||||
|
98,3,5,450000.0
|
||||||
|
104,3,1,720000.0
|
||||||
|
105,3,0,630000.0
|
||||||
|
105,3,1,670000.0
|
||||||
|
105,3,2,670000.0
|
||||||
|
105,4,1,610000.0
|
||||||
|
108,3,1,780000.0
|
||||||
|
109,3,2,700000.0
|
||||||
|
110,3,3,670000.0
|
||||||
|
110,3,4,660000.0
|
||||||
|
111,3,3,640000.0
|
||||||
|
112,3,1,700000.0
|
||||||
|
112,3,2,660000.0
|
||||||
|
112,3,3,670000.0
|
||||||
|
112,3,4,650000.0
|
||||||
|
113,3,1,640000.0
|
||||||
|
113,3,2,590000.0
|
||||||
|
113,3,3,600000.0
|
||||||
|
113,3,4,590000.0
|
||||||
|
113,3,5,590000.0
|
||||||
|
115,3,1,720000.0
|
||||||
|
115,3,2,700000.0
|
||||||
|
119,4,3,690000.0
|
||||||
|
124,3,1,770000.0
|
|
BIN
cs2109s/labs/ps4/imgs/add_bias.jpeg
Normal file
After Width: | Height: | Size: 8.7 KiB |
87
cs2109s/labs/ps4/imgs/bias_scatter.ipynb
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "f64907f4",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# CODE TO GENERATE bias_scatter.png\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from sklearn.linear_model import LinearRegression"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "bf22eb4a",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"X = np.array([1, 2, 3, 4, 5, 6]).reshape((-1, 1))\n",
|
||||||
|
"y = np.array([6, 7, 8, 8, 9, 11])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "b185d8b9",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"model_no_bias = LinearRegression(fit_intercept = False).fit(X, y)\n",
|
||||||
|
"model_with_bias = LinearRegression().fit(X, y)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "f5076cdc",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"X_with_zero = np.vstack([0, X]) # Added to show the lines passing through Feature=0\n",
|
||||||
|
"plt.scatter(X, y)\n",
|
||||||
|
"plt.plot(X_with_zero, model_no_bias.predict(X_with_zero), color = 'b', label=\"Without bias\")\n",
|
||||||
|
"plt.plot(X_with_zero, model_with_bias.predict(X_with_zero), color = 'r', label=\"With bias\")\n",
|
||||||
|
"plt.ylim(ymin=0)\n",
|
||||||
|
"plt.xlim(xmin=0, xmax=8)\n",
|
||||||
|
"plt.xlabel(\"Feature\")\n",
|
||||||
|
"plt.ylabel(\"Target\")\n",
|
||||||
|
"plt.legend(loc=\"center right\")\n",
|
||||||
|
"plt.show()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "e68dd023",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3 (ipykernel)",
|
||||||
|
"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.9.9"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
BIN
cs2109s/labs/ps4/imgs/bias_scatter.png
Normal file
After Width: | Height: | Size: 23 KiB |
BIN
cs2109s/labs/ps4/imgs/feature_scaling.png
Normal file
After Width: | Height: | Size: 6.8 KiB |
BIN
cs2109s/labs/ps4/imgs/grad_desc_algorithm.png
Normal file
After Width: | Height: | Size: 117 KiB |
24
cs2109s/labs/ps4/imgs/grad_desc_algorithm.tex
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
% CODE TO GENERATE grad_desc_algorithm.tex
|
||||||
|
\documentclass{article}
|
||||||
|
\usepackage{algorithm2e}
|
||||||
|
\usepackage{bm}
|
||||||
|
\begin{document}
|
||||||
|
\RestyleAlgo{ruled}
|
||||||
|
\DontPrintSemicolon
|
||||||
|
\begin{algorithm}[hbt!]
|
||||||
|
\caption{Gradient Descent for Linear Regression}
|
||||||
|
$w_0, w_1, \ldots, w_n \gets 0$\;
|
||||||
|
\For{$i\gets 1$ \KwTo $N$}{
|
||||||
|
$w'_0 \gets w_0 - \alpha\frac{\partial J(\boldsymbol{w})}{\partial w_0}$\;
|
||||||
|
$w'_1 \gets w_1 - \alpha\frac{\partial J(\boldsymbol{w})}{\partial w_1}$\;
|
||||||
|
$\vdots$\;
|
||||||
|
$w'_n \gets w_n - \alpha\frac{\partial J(\boldsymbol{w})}{\partial w_n}$\;
|
||||||
|
$w_0 \gets w'_0$\;
|
||||||
|
$w_1 \gets w'_1$\;
|
||||||
|
$\vdots$\;
|
||||||
|
$w_n \gets w'_n$\;
|
||||||
|
$loss \gets J(\boldsymbol{w})$\;
|
||||||
|
}
|
||||||
|
\end{algorithm}
|
||||||
|
|
||||||
|
\end{document}
|
BIN
cs2109s/labs/ps4/imgs/linear_reg.png
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
cs2109s/labs/ps4/imgs/loss.png
Normal file
After Width: | Height: | Size: 52 KiB |
BIN
cs2109s/labs/ps4/imgs/mse_plot.png
Normal file
After Width: | Height: | Size: 376 KiB |
BIN
cs2109s/labs/ps4/imgs/poly_matrix.png
Normal file
After Width: | Height: | Size: 21 KiB |
BIN
cs2109s/labs/ps4/imgs/poly_reg.png
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
cs2109s/labs/ps4/imgs/school_price_rel.png
Normal file
After Width: | Height: | Size: 9.4 KiB |
BIN
cs2109s/labs/ps4/imgs/school_price_rel_cubicfit.png
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
cs2109s/labs/ps4/imgs/school_price_rel_linearfit.png
Normal file
After Width: | Height: | Size: 14 KiB |