Python - Regression - Linear
Data:
Scikit (diabetes) 442 rows
Mission:
How to predict the probability of someone will get diabetes from given weight data.
Library used:
- Matplotlib
- Numpy
- Scikit
Code:
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_scorediabetes_X, diabetes_y = datasets.load_diabetes(return_X_y=True)
pd.options.display.float_format = '{:,.4f}'.format
data = pd.DataFrame(data=diabetes_X)
print(data.describe())diabetes_X = diabetes_X[:, np.newaxis, 2]
data = pd.DataFrame(data=diabetes_X)
print(data.describe())diabetes_X_train = diabetes_X[:-20]
diabetes_X_test = diabetes_X[-20:]
data = pd.DataFrame(data=diabetes_X_train)
print(data.describe())
data = pd.DataFrame(data=diabetes_X_test)
print(data.describe())diabetes_y_train = diabetes_y[:-20]
diabetes_y_test = diabetes_y[-20:]regr = linear_model.LinearRegression()
regr.fit(diabetes_X_train, 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)plt.show()
I wrapped the scenario in a Youtube video below.
Click this link (http://paparadit.blogspot.com/2020/11/the-algorithms-of-machine-learning.html), if you want to check out for other algorithms. Thank you for for visiting this blog & subs my channel.
Labels: Programming, Python
PS: If you've benefit from this blog, you can support it by making a small contribution. |
Post a Comment
Leave comments here...