from sklearn.datasets import load_iris, load_wine, load_digits, load_digits
import sklearn
import numpy as np
import matplotlib.pyplot as plt
import sys
import operator
from sklearn.model_selection import train_test_split

class model:
    def __init__(self,lr=0.01):
        self.lr = lr
    def sigmoid_predict(self,X,W,i):
        G = []
        for c in range(len(W)):
            if len(X) == len(W[c]):
                G.append(1/(1+np.exp(-np.dot(X,W[c]))))
            else:
                print("\n##################ERROR#########################\n")
                return 0
        return G

    def loss_function(self,G,Y):
        loss = 0
        loss = (1-G[Y])
        return loss

    def train(self,X,Y,W):
        loss = 0
        for i in range(len(X)):
            G = self.sigmoid_predict(X[i],W,i)
            loss += self.loss_function(G,Y[i])
            for c in range(len(W)):
                if Y[i] == c:
                    for f in range(len(X[i])):
                        W[c][f] = W[c][f]-(self.lr*(-1)*(G[Y[i]])*(1-G[Y[i]])*X[i][f])
        return W, loss*1000
    def accuracy(self,X,Y,W):
        count = 0
        for i in range(len(X)):
            G = self.sigmoid_predict(X[i],W,i)
            pre_index = max(enumerate(G),key=operator.itemgetter(1))
            if pre_index[0] == Y[i]:
                count += 1
                print('G---->',G,'Y --------->',Y[i])
        return count

def main():
    epoch = int(sys.argv[1])
    ml = model(lr=0.001)

    datasets = load_iris()
    trainX, testX, trainY, testY = train_test_split(datasets.data,datasets.target,test_size=0.25)
    W = np.zeros((max(datasets.target)+1, len(trainX[0])))+0.05
    for i in range(epoch):
        W,LOSS = ml.train(trainX,trainY,W)
        train_Acc = ml.accuracy(trainX,trainY,W)
        test_Acc = ml.accuracy(testX,testY,W)

        print(i,'PER LOSS = ', LOSS,'Train ACC = ',len(trainX),'/',train_Acc,'Test Acc = ',len(testX),'/',test_Acc)



if __name__== "__main__":
    main()


