Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132923 views
License: OTHER
1
import numpy as np
2
import matplotlib.pyplot as plt
3
4
from sklearn.linear_model import LinearRegression
5
from sklearn.model_selection import train_test_split
6
from .datasets import make_wave
7
from .plot_helpers import cm2
8
9
10
def plot_linear_regression_wave():
11
X, y = make_wave(n_samples=60)
12
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
13
14
line = np.linspace(-3, 3, 100).reshape(-1, 1)
15
16
lr = LinearRegression().fit(X_train, y_train)
17
print("w[0]: %f b: %f" % (lr.coef_[0], lr.intercept_))
18
19
plt.figure(figsize=(8, 8))
20
plt.plot(line, lr.predict(line))
21
plt.plot(X, y, 'o', c=cm2(0))
22
ax = plt.gca()
23
ax.spines['left'].set_position('center')
24
ax.spines['right'].set_color('none')
25
ax.spines['bottom'].set_position('center')
26
ax.spines['top'].set_color('none')
27
ax.set_ylim(-3, 3)
28
#ax.set_xlabel("Feature")
29
#ax.set_ylabel("Target")
30
ax.legend(["model", "training data"], loc="best")
31
ax.grid(True)
32
ax.set_aspect('equal')
33
34