notebook
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import numpy as np
y=ax+b a disebut slope, dan b is dikenal sebagai intercept.
x=np.array([1,2,3,4,5,6])
y=np.array([6,4,3,5,4,2])
plt.xlim(0, 10)
plt.ylim(0, 16)
plt.scatter(x, y);
import pandas as pd
df1 = df = pd.DataFrame({"x": x,"y":y})
print (df1)
x y
0 1 6
1 2 4
2 3 3
3 4 5
4 5 4
5 6 2
from sklearn.linear_model import LinearRegression
model = LinearRegression(fit_intercept=True)
import numpy as np
model.fit(x[:, np.newaxis], y)
xfit = np.linspace(0, 10, 1000)
yfit = model.predict(xfit[:, np.newaxis])
plt.scatter(x, y)
plt.plot(xfit, yfit);
print("Model slope (a): ", model.coef_[0])
print("Model intercept (b):", model.intercept_)
print ("Persamaan adalah =",round(model.coef_[0],2),"X","+",round(model.intercept_,2))
Model slope (a): -0.5142857142857145
Model intercept (b): 5.800000000000001
Persamaan adalah = -0.51 X + 5.8