Skip to content
Snippets Groups Projects
Commit 65cc8040 authored by Chauhan Chauhan's avatar Chauhan Chauhan
Browse files

Delete 3217-classification-lr-example4.py

parent 116f766e
No related branches found
No related tags found
No related merge requests found
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score
# Load the diabetes dataset
diabetes_X, diabetes_y = datasets.load_diabetes(return_X_y=True)
print (diabetes_X.shape)
# Use only one feature
feature_to_use = 2
diabetes_X = diabetes_X[:, np.newaxis, feature_to_use]
print (diabetes_X.shape)
test_samples = 20
# Split the data into training/testing sets
diabetes_X_train = diabetes_X[:-test_samples]
diabetes_X_test = diabetes_X[-test_samples:]
# Split the targets into training/testing sets
diabetes_y_train = diabetes_y[:-test_samples]
diabetes_y_test = diabetes_y[-test_samples:]
# Create linear regression object
regr = linear_model.LinearRegression()
# Train the model using the training sets
regr.fit(diabetes_X_train, diabetes_y_train)
# Make prediction using the testing set
diabetes_y_pred = regr.predict(diabetes_X_test)
print (diabetes_y_train.shape)
print (diabetes_y_test.shape)
# The coefficients
print("Coefficients: \n", regr.coef_)
# The mean squared error
print("Mean squared error: %.2f" % mean_squared_error(diabetes_y_test, diabetes_y_pred))
# The coefficient of determination: 1 is perfect prediction
print("Coefficient of determination: %.2f" % r2_score(diabetes_y_test, diabetes_y_pred))
# Plot outputs
plt.scatter(diabetes_X_test, diabetes_y_test, color="black") #grond truth actual test labels
plt.scatter(diabetes_X_test, diabetes_y_pred, color="red") #predicted test labels
plt.plot(diabetes_X_test, diabetes_y_pred, color="blue", linewidth=3)#predicted test labels
plt.xticks(())
plt.yticks(())
plt.show()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment