import numpy as np
import matplotlib.pyplot as plt
np.random.seed(42)
m=1.2; b=0.3
x = np.random.random(10)
x.sort()
y = m*x+b +np.random.random(len(x))*0.1
plt.plot(x,y,'o')
m,b = np.polyfit(x,y,1)
plt.plot(x,x*m+b,'--')
plt.ylim(-1,5)
plt.show()
x2 = np.random.random(3)
y2 = (3*np.random.random(3)-1)*3
x = np.append(x,x2)
xsort = np.argsort(x)
y=np.append(y,y2)
y=y[xsort]
x=x[xsort]
plt.plot(x,y,'o')
plt.plot(x,x*m+b,'--',alpha=0.5)
m2,b2 = np.polyfit(x,y,1)
plt.plot(x,x*m2+b2,'--')
plt.ylim(-1,5)
plt.show()
allPoints = np.arange(len(x))
xfit = np.linspace(min(x),max(x))
delta = 0.5
for i in range(10):
np.random.shuffle(allPoints)
points = allPoints[:2]
xnew = x[points]
ynew = y[points]
mp,bp = np.polyfit(xnew,ynew,1)
plt.plot(x,y,'o')
plt.plot(xnew,ynew,f'oC{(1)%10}')
plt.plot(xfit,xfit*mp+bp,f':C{(1)%10}')
plt.fill_between(xfit, xfit*mp+bp-delta, xfit*mp+bp+delta,alpha=0.2,color='k')
mask=((x*mp+bp+delta)>y)& (y>(x*mp+bp-delta))
print("inliers=",np.sum((mask)))
plt.ylim(-1,5)
plt.show()
inliers= 8
inliers= 10
inliers= 10
inliers= 10
inliers= 3
inliers= 3
inliers= 2
inliers= 10
inliers= 3
inliers= 8
len(x)
13
np.random.seed(42)
m=1.2; b=0.3
x = np.random.random(10)
x.sort()
y = m*x+b +np.random.random(len(x))*0.1
plt.plot(x,y,'o')
m,b = np.polyfit(x,y,1)
plt.plot(x,x*m+b,'--')
plt.ylim(-1,5)
plt.show()
x2 = np.random.random(3)
y2 = (3*np.random.random(3)-1)*3
x = np.append(x,x2)
xsort = np.argsort(x)
y=np.append(y,y2)
y=y[xsort]
x=x[xsort]
plt.plot(x,y,'o')
plt.plot(x,x*m+b,'--',alpha=0.5)
m2,b2 = np.polyfit(x,y,1)
plt.plot(x,x*m2+b2,'--')
plt.ylim(-1,5)
plt.show()
def RANSAC_linear(x,y,delta,nTrials):
best_fit = -1
mbest,bbest = -1,-1
param_set = []
allPoints = np.arange(len(x))
for i in range(nTrials):
# get new points
np.random.shuffle(allPoints)
points = allPoints[:2]
xnew = x[points]
ynew = y[points]
# generate potential model
mp,bp = np.polyfit(xnew,ynew,1)
# calculate cost (inliers)
mask=((x*mp+bp+delta)>y)& (y>(x*mp+bp-delta))
inliers = np.sum(mask)
if inliers > best_fit:
best_fit,mbest,bbest = inliers,mp,bp
param_set = []
elif inliers == best_fit:
param_set.append((mp,bp))
return best_fit,mbest,bbest,param_set
delta = 0.5
best_fit,mbest,bbest,param_set = RANSAC_linear(x,y,delta,100)
print("Best # inliers:",best_fit)
print(f"True (m,b)={1.2,0.3} | guessed (m,b)={round(mbest,2),round(bbest,2)}")
print("other params found:",param_set)
Best # inliers: 11 True (m,b)=(1.2, 0.3) | guessed (m,b)=(1.03, 0.49) other params found: [(1.0335475040063236, 0.4873716832986319)]
plt.plot(x,y,'o')
plt.plot(x,x*mbest+bbest,'--',label="RANSAC")
plt.plot(x,x*m+b,':',label='original')
# plt.plot(x,x*m2+b2,':',label='all points')
plt.fill_between(xfit, xfit*mbest+bbest-delta, xfit*mbest+bbest+delta,alpha=0.2,color='k')
plt.legend()
plt.ylim(-1,5)
plt.show()
np.random.seed(42)
m=1.2; b=0.3
x = np.random.random(70)*10
x.sort()
y = m*x+b +np.random.random(len(x))*1
plt.plot(x,y,'o')
m,b = np.polyfit(x,y,1)
plt.plot(x,x*m+b,'--')
# plt.ylim(-1,5)
plt.show()
x2 = np.random.random(30)*10;x2.sort()
y2 = (2*np.random.random(30)-1)
x = np.append(x,x2)
xsort = np.argsort(x)
y=np.append(y,y2)
y=y[xsort]
x=x[xsort]
plt.plot(x,y,'o')
plt.plot(x,x*m+b,'--',alpha=0.5)
m2,b2 = np.polyfit(x,y,1)
plt.plot(x,x*m2+b2,'--')
# plt.ylim(-1,5)
plt.show()