Skip to content

Instantly share code, notes, and snippets.

@vihar
Created January 28, 2018 14:00
Show Gist options
  • Save vihar/221b19c003f4c2e6b87d7320b5669d88 to your computer and use it in GitHub Desktop.
Save vihar/221b19c003f4c2e6b87d7320b5669d88 to your computer and use it in GitHub Desktop.
from sklearn import datasets, linear_model
import matplotlib.pyplot as plt
import numpy as np
# Load the diabetes dataset
diabetes = datasets.load_diabetes()
# Use only one feature for training
diabetes_X = diabetes.data[:, np.newaxis, 2]
# Split the data into training/testing sets
diabetes_X_train = diabetes_X[:-20]
diabetes_X_test = diabetes_X[-20:]
# Split the targets into training/testing sets
diabetes_y_train = diabetes.target[:-20]
diabetes_y_test = diabetes.target[-20:]
# Create linear regression object
regr = linear_model.LinearRegression()
# Train the model using the training sets
regr.fit(diabetes_X_train, diabetes_y_train)
# Input data
print('Input Values')
print(diabetes_X_test)
# Make predictions using the testing set
diabetes_y_pred = regr.predict(diabetes_X_test)
# Predicted Data
print("Predicted Output Values")
print(diabetes_y_pred)
# Plot outputs
plt.scatter(diabetes_X_test, diabetes_y_test, color='black')
plt.plot(diabetes_X_test, diabetes_y_pred, color='red', linewidth=1)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment