# -*- coding: utf-8 -*-
"""School Dropout Model.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/19P9e5w16HYcrICdvNrXPjEKyLnks7NgR
"""

import numpy as np
import pandas as pd
pd.set_option('display.max_columns', None)

import pandas as pd
from sklearn import preprocessing
from pandas.plotting import scatter_matrix
from matplotlib import pyplot
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier
import joblib
# import numpy as np

df=pd.read_csv("data/data500.csv")
df.head(30)

df.columns=["Name","gender","marks_1","caste","marks_2","Disabled","Attendance","marks_3","marks_4","marks_5","marks_6","Dropout"]

df

df_final=df[["Name","gender","caste","Disabled","Attendance","marks_1","marks_2","marks_3","marks_4","marks_5","marks_6","Dropout"]]

df_final

class MultiColumnLabelEncoder:
    def __init__(self, columns=None):
        self.columns = columns  # array of column names to encode

    def fit(self, X, y=None):
        return self # not relevant here

    def transform(self, X):
     
        output = X.copy()
        if self.columns is not None:
            for col in self.columns:
                output[col] = LabelEncoder().fit_transform(output[col])
        else:
            for colname, col in output.iteritems():
                output[colname] = LabelEncoder().fit_transform(col)
        return output

    def fit_transform(self, X, y=None):
        return self.fit(X, y).transform(X)

def generateModel(file, _id):

    dataset = MultiColumnLabelEncoder(columns=['gender', 'caste']).fit_transform(file)

    array = dataset.values
    X = array[:, 1:10]
    Y = array[:, 11]
    # print(Y)
    Y = Y.astype('int')

    model = DecisionTreeClassifier()
    model.fit(X, Y)
    filename = 'schoolModels/' + _id + '.pkl'
    joblib.dump(model, filename)

    return True

def get_prediction(data, _id):

    filename = 'schoolModels/' + _id + '.pkl'
    predict_from_joblib = joblib.load(filename)

    X_predict = {}
    for key, value in data.items():
        X_predict[key] = [value]

    X_predict = pd.DataFrame(data, index=[0])

    le = preprocessing.LabelEncoder()

    X_predict['gender'] = le.fit_transform(X_predict['gender'])
    X_predict['caste'] = le.fit_transform(X_predict['caste'])

    X_predict.to_numpy()
    prediction = predict_from_joblib.predict(X_predict)

    return prediction

df_final = MultiColumnLabelEncoder(columns=['gender', 'caste']).fit_transform(df_final)

array = df_final.values
X = array[:, 1:10]
Y = array[:, 11]
# print(Y)
Y = Y.astype('int')

X

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, random_state=0)

X_train

y_test,len(y_test)

model = DecisionTreeClassifier()
model.fit(X_train, y_train)

preds=model.predict(X_test)
preds

y_test

df_pred=pd.DataFrame({"original":y_test,"predictions":preds})
df_pred

from sklearn.metrics import accuracy_score

print("Accuracy Score : ",round(accuracy_score(y_test,preds),2)*100)

filename = 'schoolModels/' + 'try1.pkl'
joblib.dump(model, filename)

X_train[0].shape

X_train[0]

"""## Custom Input :"""

filename = 'schoolModels/' + 'try1.pkl'
model=joblib.load(filename)

input_val=[1,1,0,3,20,20,10,20,10]

model.predict(np.array(input_val).reshape(1,-1))

