# This simple program to demonstrate logistic regression was adapted 
# from the teaching materials of Natasha Jaques; specifically from 
# the demo Jupiter notebook: Classification and Logistic Regression colab at:
# https://courses.cs.washington.edu/courses/cse446/25sp/schedule/

from mpl_toolkits import mplot3d

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn import neighbors, datasets, svm

#%matplotlib inline


# (1) Generate and plot training data

N = 50
x1 = np.zeros((2,N))
x2 = np.zeros((2,N))
X = np.zeros((2,2*N))
x1[0] = np.random.uniform(-3,0,N)
x1[1] = 3.+2.*x1[0] + np.random.uniform(-2.5,1.5,N)
x2[0] = np.random.uniform(-4,4,N)
x2[1] = -2.+0.125*x2[0] + np.random.uniform(-1.5,1.5,N)
X[0] = np.r_[x1[0],x2[0]]
X[1] = np.r_[x1[1],x2[1]]
X = X.T
y = np.r_[np.ones(N),-np.ones(N)]

# Create color maps
cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF'])
cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF'])

plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold, edgecolor='k', s=20)
plt.show()


# (2) we create an instance of linear regresor and fit the data.

n_neighbors = 1
h = .02  # step size in the mesh

from sklearn.linear_model import LinearRegression
linreg = LinearRegression()
linreg.fit(X,y)

# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h))
Z = np.sign(linreg.predict(np.c_[xx.ravel(), yy.ravel()]))

# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.figure()
plt.pcolormesh(xx, yy, Z, cmap=cmap_light)

# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold, edgecolor='k', s=20)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.title("2-Class classification (k = %i)" % (n_neighbors))

plt.show()





