%load_ext autoreload
%autoreload 2

# to use dataframe and load csv file
import pandas as pd

# to use for mathematical operations 
import numpy as np

# split the set in 2 set, common train and test
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# plot different designs
import matplotlib.pyplot as plt
%matplotlib inline  

np.random.seed(123)

data = pd.read_csv("dataset/BackOrders.csv",header=0)

data.shape

data.head()

for col in ['potential_issue',
            'deck_risk',
            'oe_constraint',
            'ppap_risk',
            'stop_auto_buy',
            'rev_stop',
            'went_on_backorder']:    
    data[col]=pd.factorize(data[col])[0]

data.describe(include='all')

data['perf_6_month_avg']=data['perf_6_month_avg'].replace(-99, np.NaN)
data['perf_12_month_avg']=data['perf_12_month_avg'].replace(-99, np.NaN)

varnames=list(data)[1:]    
correlations = data[varnames].corr()
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(correlations, vmin=-1, vmax=1)
fig.colorbar(cax)
ticks = np.arange(0,22,1)
ax.set_xticks(ticks)
ax.set_yticks(ticks)
ax.set_xticklabels(varnames,rotation=90)
ax.set_yticklabels(varnames)
plt.show()

data.drop('rev_stop', axis=1, inplace=True)
data.drop('oe_constraint', axis=1, inplace=True)
data.drop('potential_issue', axis=1, inplace=True)
data.drop('stop_auto_buy', axis=1, inplace=True)
data.drop('deck_risk', axis=1, inplace=True)


def check_missing(data):
    tot = data.isnull().sum().sort_values(ascending=False)
    perc = ( round(100*data.isnull().sum()/data.isnull().count(),1) ).sort_values(ascending=False)
    missing_data = pd.concat([tot, perc], axis=1, keys=['Missing', 'Percent'])
    return missing_data[:3]
    
check_missing(data)

data.fillna(data.median(), inplace=True)
data

check_missing(data)

data.drop('sku', axis=1, inplace=True)
data

X, y = data.loc[:,data.columns!='went_on_backorder'].values, data.loc[:,'went_on_backorder'].values
X,y

X_train_1, X_test, y_train_1, y_test = train_test_split(X, y, test_size=0.1, random_state=123, stratify = data['went_on_backorder'])

print(X_train_1.shape)
print(X_test.shape)

print(pd.value_counts(y_train)/y_train.size * 100)
print(pd.value_counts(y_test)/y_test.size * 100)

def balance_split(X_train_1,y_train_1,flag=""):
    X_train0 = []
    X_train1 = []

    y_train0 = []
    y_train1 = []

    for i in range(len(y_train_1)):
        if y_train_1[i] == 0:
            X_train0.append(X_train_1[i])
            y_train0.append(y_train_1[i])
        else:
            X_train1.append(X_train_1[i])
            y_train1.append(y_train_1[i])
            
    X_train =[]
    y_train = []
    
    if flag == "fair":
        X_train = X_train0[:1000] + X_train1[:1000]
        y_train = y_train0[:1000] + y_train1[:1000]
    else:
        X_train = X_train0[:10000] + X_train1
        y_train = y_train0[:10000] + y_train1
    
    return np.asarray(X_train),np.asarray(y_train)

def save_data(X_train_1,y_train_1,flag="",name=""):
    if flag == "fair":
        X_train,y_train = balance_split(X_train_1,y_train_1,flag)
        df_train = pd.concat([pd.DataFrame(X_train), pd.DataFrame(y_train)], axis=1)
        df_train.to_csv("dataset/fair_"+name+".csv", index=False)
    else:
        X_train,y_train = balance_split(X_train_1,y_train_1)
        df_train = pd.concat([pd.DataFrame(X_train), pd.DataFrame(y_train)], axis=1)
        df_train.to_csv("dataset/classic_"+name+".csv", index=False)

#classic data
save_data(X_train_1,y_train_1,flag="classic",name="train")
#fair data
save_data(X_train_1,y_train_1,flag="fair",name="train")

#classic data
save_data(X_test,y_test,flag="classic",name="test")

data = pd.read_csv("dataset/classic_train.csv")
X,y = data[data.columns[:16]].values, data[data.columns[16]].values
X.shape, y.shape

data = pd.read_csv("dataset/fair_train.csv")
X,y = data[data.columns[:16]].values, data[data.columns[16]].values
X.shape, y.shape

data = pd.read_csv("dataset/classic_test.csv")
X,y = data[data.columns[:16]].values, data[data.columns[16]].values
X.shape, y.shape