Skip to content

Instantly share code, notes, and snippets.

@qjatn0120
Created June 14, 2023 11:26
Show Gist options
  • Save qjatn0120/0cb540cf342ef182b76f804a61b9a331 to your computer and use it in GitHub Desktop.
Save qjatn0120/0cb540cf342ef182b76f804a61b9a331 to your computer and use it in GitHub Desktop.
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