Created
June 14, 2023 11:26
-
-
Save qjatn0120/0cb540cf342ef182b76f804a61b9a331 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from sklearn.linear_model import LinearRegression | |
sample = 200 | |
X = np.random.uniform(-1, 1, sample) | |
N = np.random.uniform(-1, 1, sample) | |
Y = X + N | |
line_fitter = LinearRegression() | |
line_fitter.fit(np.expand_dims(X, axis = 1), Y) | |
reverse_fitter = LinearRegression() | |
reverse_fitter.fit(np.expand_dims(Y, axis = 1), X) | |
plt.plot(X, Y, 'o') | |
plt.plot(X, line_fitter.predict(np.expand_dims(X, axis = 1))) | |
plt.show() | |
plt.plot(X, Y - line_fitter.predict(np.expand_dims(X, axis = 1)), 'o') | |
plt.plot([-1, 1], [0, 0]) | |
plt.show() | |
plt.plot(X, Y, 'o') | |
plt.plot(reverse_fitter.predict(np.expand_dims(Y, axis = 1)), Y) | |
plt.show() | |
plt.plot(X - reverse_fitter.predict(np.expand_dims(Y, axis = 1)), Y, 'o') | |
plt.plot([-1, 1], [0, 0]) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment