Skip to content
Snippets Groups Projects
Unverified Commit 2276ce14 authored by Jen Looper's avatar Jen Looper Committed by GitHub
Browse files

Merge pull request #259 from ravindranath-sawane/patch-1

notebook.ipynb
parents 17098033 d2750d39
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Linear Regression for North American Pumpkins - Lesson 1 ## Linear Regression for North American Pumpkins - Lesson 1
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Import needed libraries Import needed libraries
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
from sklearn import datasets, linear_model, model_selection from sklearn import datasets, linear_model, model_selection
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Load the diabetes dataset, divided into `X` data and `y` features Load the diabetes dataset, divided into `X` data and `y` features
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
X, y = datasets.load_diabetes(return_X_y=True) X, y = datasets.load_diabetes(return_X_y=True)
print(X.shape) print(X.shape)
print(X[0]) print(X[0])
``` ```
%% Output %% Output
(442, 10)\n[ 0.03807591 0.05068012 0.06169621 0.02187235 -0.0442235 -0.03482076\n -0.04340085 -0.00259226 0.01990842 -0.01764613] (442, 10)\n[ 0.03807591 0.05068012 0.06169621 0.02187235 -0.0442235 -0.03482076\n -0.04340085 -0.00259226 0.01990842 -0.01764613]
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Select just one feature to target for this exercise Select just one feature to target for this exercise
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
X = X[:, np.newaxis, 2] X = X[:, np.newaxis, 2]
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Split the training and test data for both `X` and `y` Split the training and test data for both `X` and `y`
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.33) X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.33)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Select the model and fit it with the training data Select the model and fit it with the training data
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
model = linear_model.LinearRegression() model = linear_model.LinearRegression()
model.fit(X_train, y_train) model.fit(X_train, y_train)
``` ```
%% Output %% Output
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False) LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Use test data to predict a line Use test data to predict a line
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
y_pred = model.predict(X_test) y_pred = model.predict(X_test)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Display the results in a plot Display the results in a plot
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
plt.scatter(X_test, y_test, color='black') plt.scatter(X_test, y_test, color='black')
plt.plot(X_test, y_pred, color='blue', linewidth=3) plt.plot(X_test, y_pred, color='blue', linewidth=3)
plt.show() plt.show()
``` ```
%% Output %% Output
%% Cell type:code id: tags:
```
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment