import os
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from sklearn.linear_model import LinearRegression
sns.set()
os.chdir(r'D:\projects\wordpress\ex66')
os.getcwd()
rng = np.random.RandomState(1)
x = 10 * rng.rand(50)
y = 2 * x - 5 + rng.randn(50)
plt.scatter(x, y)
model = LinearRegression(fit_intercept=True)
model.fit(x[:, np.newaxis], y)
xfit = np.linspace(0, 10, 1000)
yfit = model.predict(xfit[:, np.newaxis])
plt.scatter(x,y,color='blue')
plt.plot(xfit,yfit,color='red')
print("Model slope: ", model.coef_[0])
print("Model intercept:", model.intercept_)
plt.savefig("example66.png", dpi=100)
plt.show()
plt.close()
Like this:
Like Loading...
Related
Recent Comments