from sklearn.datasets import make_blobs
import numpy as np
import matplotlib.pyplot as plt
X, y = make_blobs(n_samples=20, centers=2, n_features=2,random_state=42)
plt.scatter(X[:,0], X[:,1], c = y)
plt.show()
from sklearn.linear_model import LinearRegression
my_regression = LinearRegression()
my_regression.fit(X, y)
x1_min = np.min(X[:,0])
x1_max = np.max(X[:,0])
x2_min = np.min(X[:,1])
x2_max = np.max(X[:,1])
xx1, xx2 = np.meshgrid(np.linspace(x1_min, x1_max,100), np.linspace(x2_min, x2_max, 100))
Xprediction = np.vstack((xx1.flatten(), xx2.flatten()))
targets_prediction = my_regression.predict(Xprediction.T)
plt.scatter(X[:,0], X[:,1], c = y)
plt.contourf(xx1,xx2, np.reshape(targets_prediction>.5, np.shape(xx1)), levels=1, alpha=.2)
plt.show()
# adding an outlier
outlier = np.array([-100, 100])
y_outlier = 0
X2 = np.vstack((outlier, X))
y2 = np.append(y_outlier, y)
#### We first learn the
from sklearn.linear_model import LinearRegression
my_regression = LinearRegression()
my_regression.fit(X2, y2)
x1_min = np.min(X[:,0])
x1_max = np.max(X[:,0])
x2_min = np.min(X[:,1])
x2_max = np.max(X[:,1])
xx1, xx2 = np.meshgrid(np.linspace(x1_min, x1_max,100), np.linspace(x2_min, x2_max, 100))
Xprediction = np.vstack((xx1.flatten(), xx2.flatten()))
targets_prediction = my_regression.predict(Xprediction.T)
plt.scatter(X[:,0], X[:,1], c = y)
plt.contourf(xx1,xx2, np.reshape(targets_prediction>.5, np.shape(xx1)), levels=1, alpha=.2)
plt.show()
x1_min_0 = np.min(X[:,0])
x1_max_0 = np.max(X[:,0])
x2_min_0 = np.min(X[:,1])
x2_max_0 = np.max(X[:,1])
plt.scatter(X2[:,0], X2[:,1], c = y2)
plt.contourf(xx1,xx2, np.reshape(targets_prediction>.5, np.shape(xx1)), levels=1, alpha=.2)
axes = plt.gca()
axes.set_xlim([x1_min_0,x1_max_0])
axes.set_ylim([x2_min_0,x2_max_0])
plt.show()
from sklearn.linear_model import LogisticRegression
myLogistic = LogisticRegression().fit(X, y)
x1_min = np.min(X[:,0])
x1_max = np.max(X[:,0])
x2_min = np.min(X[:,1])
x2_max = np.max(X[:,1])
xx1, xx2 = np.meshgrid(np.linspace(x1_min, x1_max,100), np.linspace(x2_min, x2_max, 100))
Xprediction = np.vstack((xx1.flatten(), xx2.flatten()))
targets_prediction = myLogistic.predict(Xprediction.T)
plt.scatter(X[:,0], X[:,1], c = y)
plt.contourf(xx1,xx2, np.reshape(targets_prediction>.5, np.shape(xx1)), levels=1, alpha=.2)
plt.show()
# adding an outlier
outlier = np.array([-100, 100])
y_outlier = 0
X2 = np.vstack((outlier, X))
y2 = np.append(y_outlier, y)
myLogistic = LogisticRegression().fit(X2, y2)
x1_min = np.min(X[:,0])
x1_max = np.max(X[:,0])
x2_min = np.min(X[:,1])
x2_max = np.max(X[:,1])
xx1, xx2 = np.meshgrid(np.linspace(x1_min, x1_max,100), np.linspace(x2_min, x2_max, 100))
Xprediction = np.vstack((xx1.flatten(), xx2.flatten()))
targets_prediction = myLogistic.predict(Xprediction.T)
plt.scatter(X2[:,0], X2[:,1], c = y2)
plt.contourf(xx1,xx2, np.reshape(targets_prediction>0.5, np.shape(xx1)), levels=1, alpha=.2)
axes = plt.gca()
axes.set_xlim([x1_min_0,x1_max_0])
axes.set_ylim([x2_min_0,x2_max_0])
plt.show()