import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import classification_report
from sklearn import preprocessing
from sklearn import linear_model
scale = False
data = np.genfromtxt('data1.txt',delimiter=',')
x_data = data[:,:-1]
y_data = data[:,-1]


def plot():
    x0 = []
    x1 = []
    y0 = []
    y1 = []

    for i in range(len(x_data)):
        if y_data[i] == 0:
            x0.append(x_data[i,0])
            y0.append(x_data[i,1])
        else:
            x1.append(x_data[i, 0])
            y1.append(x_data[i, 1])

    scatter0 = plt.scatter(x0,y0,c = 'b', marker='o')
    scatter1 = plt.scatter(x1,y1,c = 'r', marker='x')
    plt.legend(handles=[scatter0,scatter1],labels=['label0','label1'],loc = 'best')
plot()
# plt.show()

logistic1 = linear_model.LogisticRegression()
logistic1.fit(x_data,y_data)

if scale == False:
    plot()
    x_test = np.array([[-2.5],[1]])
    y_test = (-logistic1.intercept_ - x_test*logistic1.coef_[0][0])/logistic1.coef_[0][1]
    plt.plot(x_test,y_test,'k')
    plt.show()

predictions = logistic1.predict(x_data)
print(classification_report(y_data,predictions))