Skip to content

Instantly share code, notes, and snippets.

@neerajvashistha
Created November 14, 2018 17:34
Show Gist options
  • Save neerajvashistha/432a4c70de88a6378dceb34a5d345510 to your computer and use it in GitHub Desktop.
Save neerajvashistha/432a4c70de88a6378dceb34a5d345510 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression, RANSACRegressor
# For reproducibility
np.random.seed(1000)
nb_samples = 200
nb_noise_samples = 150
def show_dataset(X, Y):
fig, ax = plt.subplots(1, 1, figsize=(30, 25))
ax.scatter(X, Y)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.grid()
plt.show()
# Create dataset
X = np.arange(-5, 5, 0.05)
Y = X + 2
Y += np.random.uniform(-0.5, 0.5, size=nb_samples)
for i in range(nb_noise_samples, nb_samples):
Y[i] += np.random.uniform(12, 15)
# Show the dataset
show_dataset(X, Y)
from sklearn.linear_model import LinearRegression
lr = LinearRegression(normalize=True)
lr.fit(X.reshape((-1, 1)), Y.reshape((-1, 1)))
lr.intercept_
lr.coef_
from sklearn.linear_model import RANSACRegressor
rs = RANSACRegressor(lr)
rs.fit(X.reshape((-1, 1)), Y.reshape((-1, 1)))
rs.estimator_.intercept_
rs.estimator_.coef_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment