Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132928 views
License: OTHER
1
import numpy as np
2
import matplotlib.pyplot as plt
3
from sklearn.linear_model import LinearRegression
4
5
6
def plot_linear_regression():
7
a = 0.5
8
b = 1.0
9
10
# x from 0 to 10
11
x = 30 * np.random.random(20)
12
13
# y = a*x + b with noise
14
y = a * x + b + np.random.normal(size=x.shape)
15
16
# create a linear regression classifier
17
clf = LinearRegression()
18
clf.fit(x[:, None], y)
19
20
# predict y from the data
21
x_new = np.linspace(0, 30, 100)
22
y_new = clf.predict(x_new[:, None])
23
24
# plot the results
25
ax = plt.axes()
26
ax.scatter(x, y)
27
ax.plot(x_new, y_new)
28
29
ax.set_xlabel('x')
30
ax.set_ylabel('y')
31
32
ax.axis('tight')
33
34
35
if __name__ == '__main__':
36
plot_linear_regression()
37
plt.show()
38
39