Python - Regression - Linear


Scikit (diabetes) 442 rows


How to predict the probability of someone will get diabetes from given weight data.

Library used:

  1. Matplotlib
  2. Numpy
  3. Scikit


import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score

diabetes_X, diabetes_y = datasets.load_diabetes(return_X_y=True)

pd.options.display.float_format = '{:,.4f}'.format
data = pd.DataFrame(data=diabetes_X)

diabetes_X = diabetes_X[:, np.newaxis, 2]

data = pd.DataFrame(data=diabetes_X)

diabetes_X_train = diabetes_X[:-20]

diabetes_X_test = diabetes_X[-20:]

data = pd.DataFrame(data=diabetes_X_train)

data = pd.DataFrame(data=diabetes_X_test)

diabetes_y_train = diabetes_y[:-20]
diabetes_y_test = diabetes_y[-20:]

regr = linear_model.LinearRegression(), diabetes_y_train)

diabetes_y_pred = regr.predict(diabetes_X_test)

print('Koef. Determinasi (r2): %.2f' % r2_score(diabetes_y_test, diabetes_y_pred))

plt.scatter(diabetes_X_train, diabetes_y_train,  color='green')

plt.scatter(diabetes_X_test, diabetes_y_test,  color='black')
plt.plot(diabetes_X_test, diabetes_y_pred, color='blue', linewidth=3)

I wrapped the scenario in a Youtube video below.

 Click this link (, if you want to check out for other algorithms. Thank you for for visiting this blog & subs my channel.

Labels: ,

PS: If you've benefit from this blog,
you can support it by making a small contribution.

Enter your email address to receive feed update from this blog:

Post a Comment


Post a Comment

Leave comments here...