from sklearn.datasets import make_blobs
import numpy as np
X, y = make_blobs(n_samples=30, centers=2, n_features=2, random_state=20)
import matplotlib.pyplot as plt
plt.scatter(X[:,0], X[:,1], c = y)
plt.show()
# learning the separating plane through LS
import numpy as np
from sklearn.linear_model import LinearRegression
reg = LinearRegression().fit(X, y)
x1min = np.min(X[:,0])
x1max = np.max(X[:,0])
x2min = np.min(X[:,1])
x2max = np.max(X[:,1])
xx1, xx2 = np.meshgrid(np.linspace(x1min, x1max, 100), np.linspace(x2min, x2max, 100))
Xpredict = np.vstack((xx1.flatten(), xx2.flatten()))
prediction = reg.predict(Xpredict.T)
plt.scatter(X[:,0], X[:,1], c = y)
plt.contourf(xx1, xx2, np.reshape(prediction>0.5,np.shape(xx1)), levels=1, alpha=0.2)
plt.show()
# adding an outlier
outlier = np.array([-5, 7])
outlier = np.expand_dims(outlier, axis=1)
youtlier = 0
X2 = np.vstack((outlier.T, X))
y2 = np.append(0,y)
# learning the separating plane through LS
import numpy as np
from sklearn.linear_model import LinearRegression
reg = LinearRegression().fit(X2, y2)
x1min = np.min(X2[:,0])
x1max = np.max(X2[:,0])
x2min = np.min(X2[:,1])
x2max = np.max(X2[:,1])
xx1, xx2 = np.meshgrid(np.linspace(x1min, x1max, 100), np.linspace(x2min, x2max, 100))
Xpredict = np.vstack((xx1.flatten(), xx2.flatten()))
prediction = reg.predict(Xpredict.T)
plt.scatter(X2[:,0], X2[:,1], c = y2)
plt.contourf(xx1, xx2, np.reshape(prediction>0.5,np.shape(xx1)), levels=1, alpha=0.2)
plt.show()
# adding an outlier
outlier = np.array([-10, 7])
outlier = np.expand_dims(outlier, axis=1)
youtlier = 0
X2 = np.vstack((outlier.T, X))
y2 = np.append(0,y)
# learning the separating plane through LS
import numpy as np
from sklearn.linear_model import LinearRegression
reg = LinearRegression().fit(X2, y2)
x1min = np.min(X2[:,0])
x1max = np.max(X2[:,0])
x2min = np.min(X2[:,1])
x2max = np.max(X2[:,1])
xx1, xx2 = np.meshgrid(np.linspace(x1min, x1max, 100), np.linspace(x2min, x2max, 100))
Xpredict = np.vstack((xx1.flatten(), xx2.flatten()))
prediction = reg.predict(Xpredict.T)
plt.scatter(X2[:,0], X2[:,1], c = y2)
plt.contourf(xx1, xx2, np.reshape(prediction>0.5,np.shape(xx1)), levels=1, alpha=0.2)
plt.show()
outlier = np.array([-20, 7])
outlier = np.expand_dims(outlier, axis=1)
youtlier = 0
X2 = np.vstack((outlier.T, X))
y2 = np.append(0,y)
# learning the separating plane through LS
import numpy as np
from sklearn.linear_model import LinearRegression
reg = LinearRegression().fit(X2, y2)
x1min = np.min(X2[:,0])
x1max = np.max(X2[:,0])
x2min = np.min(X2[:,1])
x2max = np.max(X2[:,1])
xx1, xx2 = np.meshgrid(np.linspace(x1min, x1max, 100), np.linspace(x2min, x2max, 100))
Xpredict = np.vstack((xx1.flatten(), xx2.flatten()))
prediction = reg.predict(Xpredict.T)
plt.scatter(X2[:,0], X2[:,1], c = y2)
plt.contourf(xx1, xx2, np.reshape(prediction>0.5,np.shape(xx1)), levels=1, alpha=0.2)
plt.show()
outlier = np.array([-50, 7])
outlier = np.expand_dims(outlier, axis=1)
youtlier = 0
X2 = np.vstack((outlier.T, X))
y2 = np.append(0,y)
# learning the separating plane through LS
import numpy as np
from sklearn.linear_model import LinearRegression
reg = LinearRegression().fit(X2, y2)
x1min = np.min(X2[:,0])
x1max = np.max(X2[:,0])
x2min = np.min(X2[:,1])
x2max = np.max(X2[:,1])
xx1, xx2 = np.meshgrid(np.linspace(x1min, x1max, 100), np.linspace(x2min, x2max, 100))
Xpredict = np.vstack((xx1.flatten(), xx2.flatten()))
prediction = reg.predict(Xpredict.T)
plt.scatter(X2[:,0], X2[:,1], c = y2)
plt.contourf(xx1, xx2, np.reshape(prediction>0.5,np.shape(xx1)), levels=1, alpha=0.2)
plt.show()
outlier = np.array([-50, 7])
outlier = np.expand_dims(outlier, axis=1)
youtlier = 0
X2 = np.vstack((outlier.T, X))
y2 = np.append(0,y)
# learning the separating plane through LS
import numpy as np
from sklearn.linear_model import LogisticRegression
reg = LogisticRegression().fit(X2, y2)
x1min = np.min(X2[:,0])
x1max = np.max(X2[:,0])
x2min = np.min(X2[:,1])
x2max = np.max(X2[:,1])
xx1, xx2 = np.meshgrid(np.linspace(x1min, x1max, 100), np.linspace(x2min, x2max, 100))
Xpredict = np.vstack((xx1.flatten(), xx2.flatten()))
prediction = reg.predict(Xpredict.T)
plt.scatter(X2[:,0], X2[:,1], c = y2)
plt.contourf(xx1, xx2, np.reshape(prediction>0.5,np.shape(xx1)), levels=1, alpha=0.2)
plt.show()