ml-schoo-and-maybe-andrew-ng/work2/.ipynb_checkpoints/oldW2_UGL8_Scikit_Learn-che...

127 lines
3.1 KiB
Plaintext
Raw Normal View History

2022-11-15 10:53:25 -05:00
{
"cells": [
{
"cell_type": "markdown",
"id": "expected-characterization",
"metadata": {},
"source": [
"# Ungraded Lab: Linear Regression using Scikit-Learn"
]
},
{
"cell_type": "markdown",
"id": "gorgeous-lincoln",
"metadata": {},
"source": [
"Now that you've implemented linear regression from scratch, let's see you can train a linear regression model using scikit-learn.\n",
"\n",
"## Dataset \n",
"Let's start with the same dataset as before."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "mobile-firmware",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"# X is the input variable (size in square feet)\n",
"# y in the output variable (price in 1000s of dollars)\n",
"X = np.array([1000, 2000])\n",
"y = np.array([200, 400])"
]
},
{
"cell_type": "markdown",
"id": "offshore-lease",
"metadata": {},
"source": [
"## Fit the model\n",
"\n",
"The code below imports the [linear regression model](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html#sklearn.linear_model.LinearRegression) from scikit-learn. You can fit this model on the training data by calling `fit` function."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "monetary-tactics",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.linear_model import LinearRegression\n",
"\n",
"linear_model = LinearRegression()\n",
"# We have to reshape X using .reshape(-1, 1) because our data has a single feature\n",
"# If X has multiple features, you don't need to reshape\n",
"linear_model.fit(X.reshape(-1, 1), y) "
]
},
{
"cell_type": "markdown",
"id": "thick-seven",
"metadata": {},
"source": [
"## Make Predictions\n",
"\n",
"You can see the predictions made by this model by calling the `predict` function."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "norwegian-variety",
"metadata": {},
"outputs": [],
"source": [
"y_pred = linear_model.predict(X.reshape(-1,1))\n",
"\n",
"print(\"Prediction on training set:\", y_pred)"
]
},
{
"cell_type": "markdown",
"id": "geographic-archive",
"metadata": {},
"source": [
"## Calculate accuracy\n",
"\n",
"You can calculate this accuracy of this model by calling the `score` function."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "immune-password",
"metadata": {},
"outputs": [],
"source": [
"print(\"Accuracy on training set:\", linear_model.score(X.reshape(-1,1), y))"
]
}
],
"metadata": {
"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.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}