88 lines
2.1 KiB
Plaintext
88 lines
2.1 KiB
Plaintext
{
|
|
"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
|
|
}
|