# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] colab_type="text" id="n6JMkNPuIgT1"
# # Enhanced-Analytic-System-for-Smart-University-Assistance
# Name - <NAME>                                         
# Roll No - 1729048                                           
# Project Name - Be-Friend      
# Github Repo -  [Link](https://github.com/rahulbordoloi/Enhanced-Analytic-System-for-Smart-University-Assistance/)                  
# Email - <EMAIL>, <EMAIL>                          
# Language - Python                      
# Project is Done on Google Colab.                      

# + [markdown] colab_type="text" id="jA9RNdbzIjZL"
# *   Libraries Pre-requisites -  [requirements.txt](https://drive.google.com/file/d/1RmyCxSOJBOnDc-I3Xn8a_laz58f1pi4b/view?usp=sharing)        
#
# *   Download Pre-loaded Model -  [Pickle Link](https://drive.google.com/file/d/1jRVVnEVPGb2_6UXqfTjOg-MpzqolknZz/view?usp=sharing)
#
#
# To install , download the file and run -
# ```
# # # !pip install -r requirements.txt
# ```
# *   RAM of 8GB is preferred if run on Local.
#
#
#
#
#
#

# + [markdown] colab_type="text" id="YtMOtMtBIjZL"
# # 1. Import Dataset and Libraries

# + colab_type="code" id="jFU_GbOQIjZL" colab={}
from google.colab import drive
drive.mount('/content/drive')

# + id="CwiSRcAMJ1_f" colab_type="code" colab={}
import warnings
warnings.filterwarnings("ignore")

# + colab_type="code" id="T_ytc8G8IjZM" colab={}
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# + colab_type="code" id="aW1Sbm7GIjZN" colab={}
train = pd.read_csv('/content/drive/My Drive/Minor Final/train.csv', error_bad_lines=False, header=0, skiprows=[0], encoding='ISO-8859-1')

# + id="TrZ5XButOXbo" colab_type="code" colab={}
test = pd.read_csv('/content/drive/My Drive/Minor Final/test.csv', error_bad_lines=False, header=0, skiprows=[0], encoding='ISO-8859-1') 

# + id="DhCBu341OBx4" colab_type="code" colab={}
train.head(2)

# + colab_type="code" id="U2c0Mgc4IjZQ" colab={}
print(train.shape,test.shape)

# + [markdown] colab_type="text" id="nHHUf5-MIjZR"
# # 2. Working on Train [Feature Engg and Selection]

# + [markdown] colab_type="text" id="Vh6YmqBuIjZS"
# Visualising and Dropping off the Completely Null Columns

# + colab_type="code" id="Q0eEaiulIjZS" colab={}
#how many values are missing in each column.
train.isnull().sum()

# + [markdown] id="1SlbKp3PO7Mh" colab_type="text"
# Result : There's not even a single null value in the whole dataset. <br>
# Therefore we can except a plain heatmap.

# + colab_type="code" id="lOSUTdUqIjZV" colab={}
#visualizing and observing the null elements in the dataset
plt.figure(figsize=(10,10))
sns.heatmap(train.isnull(), cbar = False, cmap = 'YlGnBu')   #ploting missing data #cbar, cmap = colour bar, colour map

# + colab_type="code" id="iATWgb4QIjZX" colab={}
train

# + [markdown] id="aaVSX2a4Pfom" colab_type="text"
# To check for duplicate columns

# + colab_type="code" id="WQ09xoqGIjZj" colab={}
x = set()                                                    # set as to store only the unique values
for i in range(train.shape[1]):
        c1 = train.iloc[:, i]
        for j in range(i + 1, train.shape[1]):
            c2 = train.iloc[:, j]
            if c1.equals(c2):
                x.add(train.columns.values[j])
for col in x:
        print(col)

# + [markdown] id="9wMNtLqdPk15" colab_type="text"
# Result : All the columns are unique in nature

# + colab_type="code" id="S9UX1IPIIjZk" colab={}
#gives out no of unqiue elements per column
train.nunique()

# + colab_type="code" id="t8ZeGJJ3IjZl" colab={}
train.info()

# + colab_type="code" id="a7uUXCz0IjZm" colab={}
train.describe()

# + colab_type="code" id="EkUUztUNIjZn" colab={}
train.columns

# + colab_type="code" id="_CDfH0XpIjZo" colab={}
train.drop(['Candidate ID','Name','Number of characters in Original Name','Month of Birth','Year of Birth'],      # dropping unnecessary columns
           inplace = True, axis = 1)                                                                                        

# + id="iXkmLrm_dO46" colab_type="code" colab={}
train.drop(['State (Location)'], inplace = True, axis = 1)            # because it isn't necessary and cause more ambiguity in our data.

# + colab_type="code" id="4-2W0kWtIjZp" colab={}
train.head(5)

# + id="Eq7CcP5yf85F" colab_type="code" colab={}
train[train['Degree of study'] == 'X'].shape[0]

# + [markdown] id="tFShHFStg5hu" colab_type="text"
# Therefore, Degree of Study is a Quasi-Constant Column

# + id="Mo5mCJSBhO7c" colab_type="code" colab={}
number_of_occ_per = 653/train.shape[0] * 100
print(str(number_of_occ_per) + '%') 

# + colab_type="code" id="V5gXM5obiicn" colab={}
#encoding categorical data Gender
from sklearn.preprocessing import LabelEncoder
l = LabelEncoder()
x=pd.DataFrame()
x.loc[:,'Degree'] = l.fit_transform(train.loc[:,'Degree of study'])

# + id="AVJGxlmlkAMI" colab_type="code" colab={}
x

# + id="qisOUQXJiuUp" colab_type="code" colab={}
from sklearn.feature_selection import VarianceThreshold
qconstant_filter = VarianceThreshold(threshold=0.01)
qconstant_filter.fit(x)

len(x.columns[qconstant_filter.get_support()])

# len(train.columns[constant_filter.get_support()])

# + [markdown] id="WwPAU0bhkmSp" colab_type="text"
# Therefore, Degree of study must be dropped.

# + id="439P7qxmklRI" colab_type="code" colab={}
train.drop(['Degree of study'], inplace = True, axis = 1)

# + colab_type="code" id="LMFgyNVOIjZs" colab={}
train.shape

# + id="TELOHqrRSL_z" colab_type="code" colab={}
train['10th Completion Year'].unique(), train['Year of Completion of college'].unique(), train['Quantitative Ability 1'].unique(), train['Domain Skills 1'].unique(), train['Analytical Skills 1'].unique()

# + id="KQtuzErZFYx3" colab_type="code" colab={}
train.replace(to_replace='MD', value = float(0.0), inplace = True)

# + id="EtMjOT0mVBib" colab_type="code" colab={}
train['Quantitative Ability 1'] = pd.to_numeric(train['Quantitative Ability 1'])
train['Domain Skills 1'] = pd.to_numeric(train['Domain Skills 1'])
train['Analytical Skills 1'] = pd.to_numeric(train['Analytical Skills 1'])

# + id="2cWYqjDBUe0M" colab_type="code" colab={}
train['Performance'].unique(), train['Quantitative Ability 1'].unique()

# + id="iIttxZXK1TdU" colab_type="code" colab={}
train['Performance'].replace(to_replace=0.0, value = 'MD', inplace = True)

# + id="kQVSbdKg1aUr" colab_type="code" colab={}
train['Performance'].unique()

# + id="0LkQg6E1Ui_T" colab_type="code" colab={}
train.info()

# + [markdown] colab_type="text" id="x3-_EQa5IjZ0"
# Checking and learning about the train set's skewness.

# + colab_type="code" id="l7JWJrekIjZ1" colab={}
#checking the skewness of the train set
train.skew(axis = 0, skipna = True)


# + [markdown] colab_type="text" id="wogFfIpeIjZ2"
# Inference : some columns seems to be a lightly left-skewed. <br>
# They are : 10th percentage, English 4, Quantitative Ability 2, Quantitative Ability 3, Domain Skills 2, Domain Test 4, Analytical Skills 1, Analytical Skills 2 <br>
# These columns need to be later transformed.

# + [markdown] colab_type="text" id="yYVQ_uJEIjZ4"
# Checking for Correlation

# + id="MO33cDdv21X3" colab_type="code" colab={}
# gives out the columns which are highly correlated amongst each other

def correlation(train, threshold = 0.90):
    corr_col = set() # Set of all the names of deleted columns
    corr_m = train.corr()
    for i in range(len(corr_m.columns)):
        for j in range(i):
            if (corr_m.iloc[i, j] >= threshold) and (corr_m.columns[j] not in corr_col):
                col = corr_m.columns[i] # getting the name of column
                corr_col.add(col)
    return corr_col

print(correlation(train,0.9))

# + [markdown] id="z0X4GSIN4HBV" colab_type="text"
# Inference : There is no correlated columns

# + colab_type="code" id="kvw4VSOpyEmr" colab={}
train.columns

# + [markdown] colab_type="text" id="t4GIAUOAIjaJ"
# # 3. Visualizations

# + colab_type="code" id="EhmTh0jzIjaJ" colab={}
#plotting pairwise relationships in train
sns.pairplot(train)

# + colab_type="code" id="8dPq-7UxIjaK" colab={}
# Distribution Plot and ‘Boxplot of payment_amount’ to learn about its distribution and also, to know about outliers if present.
plt.figure(figsize=(8,6))

plt.subplot(1,2,1)
plt.title('English Marks Distribution Plot')
sns.distplot(train['English 1'])

plt.subplot(1,2,2)
plt.title('English Marks Spread')
sns.boxplot(y=train['English 1'])

plt.show()
plt.tight_layout()

plt.figure(figsize=(8,6))

plt.subplot(1,2,1)
plt.title('Quantitative Ability Distribution Plot')
sns.distplot(train['Quantitative Ability 1'])

plt.subplot(1,2,2)
plt.title('Quantitative Ability Spread')
sns.boxplot(y=train['Quantitative Ability 1'])

plt.show()
plt.tight_layout()

plt.figure(figsize=(8,6))

plt.subplot(1,2,1)
plt.title('Domain Skills Distribution Plot')
sns.distplot(train['Domain Skills 1'])

plt.subplot(1,2,2)
plt.title('Domain Skills Spread')
sns.boxplot(y=train['Domain Skills 1'])

plt.show()
plt.tight_layout()

plt.figure(figsize=(8,6))

plt.subplot(1,2,1)
plt.title('Analytical Skills Distribution Plot')
sns.distplot(train['Analytical Skills 1'])

plt.subplot(1,2,2)
plt.title('Analytical Skills Spread')
sns.boxplot(y=train['Analytical Skills 1'])

plt.show()
plt.tight_layout()

# + colab_type="code" id="csCUUCe0IjaL" colab={}
# Checking out the distribution of variables across different variables in train set.
plt.figure(figsize=(25, 6))

df = pd.DataFrame(train.groupby(['Performance'])['English 1'].mean().sort_values(ascending = False))
df.plot.bar()
plt.title('Performance vs English')
plt.show()

df = pd.DataFrame(train.groupby(['Performance'])['Quantitative Ability 1'].mean().sort_values(ascending = False))
df.plot.bar()
plt.title('Performance vs Quantitative Ability')
plt.show()

df = pd.DataFrame(train.groupby(['Performance'])['Domain Skills 1'].mean().sort_values(ascending = False))
df.plot.bar()
plt.title('Performance vs Domain Skills')
plt.show() 

df = pd.DataFrame(train.groupby(['Performance'])['Analytical Skills 1'].mean().sort_values(ascending = False))
df.plot.bar()
plt.title('Performance vs Analytical Skills')
plt.show()

# + colab_type="code" id="Nc4ixJivIjaM" colab={}
train.columns

# + [markdown] colab_type="text" id="RSUNWrGMIjaO"
# # 4.  Mapping Test and Train

# + colab_type="code" id="e2IHOxL3IjaO" colab={}
#copying the columns from train
cols  = train.columns.to_list()
cols

# + colab_type="code" id="Ihvo3eo_IjaP" colab={}
print(test.shape , train.shape)

# + colab_type="code" id="67iIGXxJIjaP" colab={}
#mapping the features
test = test.reindex(columns=cols)
test.shape

# + [markdown] colab_type="text" id="DrcY9dxqIjaQ"
# # 5. Data Preprocessing

# + colab_type="code" id="X2HPnoIyIjaQ" colab={}
train.columns

# + [markdown] colab_type="text" id="xGg2gyazIjaR"
# Encoding categorical variables

# + colab_type="code" id="7Ynt8xo4IjaS" colab={}
# !pip install --upgrade category_encoders

# + id="hpe76GDOfUN2" colab_type="code" colab={}
#encoding categorical data Gender
from sklearn.preprocessing import LabelEncoder
l = LabelEncoder()
train.loc[:,'Gender'] = l.fit_transform(train.loc[:,'Gender'])
# train.loc[:, '12th Completion year'] = l.fit_transform(train.loc[:, '12th Completion year'])
# train.loc[:, '10th Completion Year'] = l.fit_transform(train.loc[:, '10th Completion Year'])

# + id="6qfokRjISkmh" colab_type="code" colab={}
train.loc[:,'Performance']=l.fit_transform(train.loc[:,'Performance'])

# + colab_type="code" id="q6hzqje8IjaT" colab={}
from category_encoders import TargetEncoder
encoder = TargetEncoder()
train['Specialization in study'] = encoder.fit_transform(train['Specialization in study'], train['Performance'])
# train['10Y'] = encoder.fit_transform(train['10th Completion Year'], train['Performance'])
# train['12Y'] = encoder.fit_transform(train['12th Completion year'], train['Performance'])

# + id="NomyNERLTUqE" colab_type="code" colab={}
encoder = TargetEncoder()
train['Year of Completion of college'] = encoder.fit_transform(train['Year of Completion of college'], train['Performance'])

# + colab_type="code" id="f27cRCKuTxLL" colab={}
encoder = TargetEncoder()
train['12th Completion year'] = encoder.fit_transform(train['12th Completion year'], train['Performance'])

# + colab_type="code" id="1oOotmgeTxRU" colab={}
encoder = TargetEncoder()
train['10th Completion Year'] = encoder.fit_transform(train['10th Completion Year'], train['Performance'])

# + id="3xR3rpNZ6e_K" colab_type="code" colab={}
train.head(5)

# + colab_type="code" id="WflyGKP3IjaV" colab={}
train.describe()

# + colab_type="code" id="AfuGVaQ5IjaW" colab={}
#Correlation using heatmap
plt.figure(figsize = (10, 10))
hm = train.corr().where(np.tril(np.ones(train.corr().shape)).astype(np.bool))
sns.heatmap(hm, annot = True, cmap="YlGnBu")
plt.show()

# + id="TMnQR1Yhzthz" colab_type="code" colab={}
train.columns

# + [markdown] colab_type="text" id="y_SnH-C0IjaW"
# Splitting train set into x and y i.e independent variable vector and dependent variable vector.

# + colab_type="code" id="WLdb3tAkIjaX" colab={}
x = train.drop(['Performance'], axis = 1)
y = train.loc[:,'Performance']

# + colab_type="code" id="FJL9O2MDIjaX" colab={}
print(x.shape,y.shape)

# + colab_type="code" id="1m4zU_JrIjaY" colab={}
y = y.values.reshape(-1,1)

# + colab_type="code" id="MNq1g_KiIjaZ" colab={}
#plotting distribution plot of newly encoded variables
plt.figure(figsize=(8,6))

plt.subplot(1,2,1)
sns.distplot(x['Year of Completion of college'])

plt.subplot(1,2,2)
sns.distplot(x['10th Completion Year'])

plt.figure(figsize=(8,6))

plt.subplot(1,2,1)
sns.distplot(x['12th Completion year'])

plt.subplot(1,2,2)
sns.distplot(x['Specialization in study'])

# + [markdown] id="lIKgSadxVhTQ" colab_type="text"
# Inference : It is highly skewed.

# + colab_type="code" id="KyWNQCTGVaaD" colab={}
#checking the skewness of the train set
x.skew(axis = 0, skipna = True)

# + [markdown] colab_type="text" id="GDaVbRCgIjaZ"
# Reducing skewness of the features according to their skewness amount.

# + colab_type="code" id="LzpgJjU1IjaZ" colab={}
#trying square-root and log transformations
crim = np.log(x['Year of Completion of college'])
crim_s = np.sqrt(x['Year of Completion of college'])
print(crim.skew(),crim_s.skew())

# + colab_type="code" id="bcX-ll8vIjaa" colab={}
#Observing the distribution plot of ‘Year of Completion of college’ after boxcox transformation.
from scipy import stats
crim_b = stats.boxcox(x['Year of Completion of college'])[0]
pd.Series(crim_b).skew()
sns.distplot(crim_b);

# + [markdown] id="MV6I-tuHcNzW" colab_type="text"
# Boxcox will be the best transformation for 'Year of Completion of college'

# + colab_type="code" id="CYt2QyhRIjab" colab={}
x.skew()

# + colab_type="code" id="c0G9OFAvIjac" colab={}
a = np.sqrt(x['12th Completion year'])                                          #square-root transformation 
b = np.log(x['12th Completion year'])                                           #logarithimic transformation
print(a.skew(),b.skew())

a = np.sqrt(x['10th Completion Year'])                                          #square-root transformation 
b = np.log(x['10th Completion Year'])                                           #logarithimic transformation
print(a.skew(),b.skew())

# + colab_type="code" id="MHdfa4rCIjac" colab={}
#Updating the required pandas series.
a = pd.Series(stats.boxcox(x['10th Completion Year'])[0]) 
b = pd.Series(stats.boxcox(x['12th Completion year'])[0])
c = pd.Series(stats.boxcox(x['Specialization in study'])[0])  
d = pd.Series(stats.boxcox(x['Year of Completion of college'])[0])
print(a.skew(), b.skew(), c.skew(), d.skew())

# + colab_type="code" id="tgyAtwtHIjad" colab={}
x['10th Completion Year'] = a
x['12th Completion year'] = b
x['Specialization in study'] = c
x['Year of Completion of college'] = d

# + id="k4MMLJsub1qP" colab_type="code" colab={}
x.skew()

# + colab_type="code" id="uS9zahNRIjae" colab={}
x.head(1)

# + colab_type="code" id="_Zvx6cWaIjam" colab={}
x.shape

# + colab_type="code" id="gx50pUc9Ijan" colab={}
x['12th Completion year'].describe()

# + [markdown] colab_type="text" id="yupaiGfYIjap"
# Standard Scaling all the features to come under a common range.

# + colab_type="code" id="Mh4ZkFaQIjap" colab={}
from sklearn.preprocessing import StandardScaler
sc= StandardScaler()
x = sc.fit_transform(x)

# + colab_type="code" id="6rDGYsH5Ijaq" colab={}
x

# + colab_type="code" id="x6Y81NDvIjar" colab={}
y

# + [markdown] colab_type="text" id="oHC_vkk8Ijas"
# # 6. Splitting Train into x_train/y_train 

# + id="NdvokqCynLaL" colab_type="code" colab={}
y.shape

# + colab_type="code" id="DvoefmDkIjas" colab={}
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.25)

# + colab_type="code" id="NppjVrXpIjat" colab={}
print(x_train.shape,y_train.shape)

# + colab_type="code" id="VpWH3RH9Ijat" colab={}
print(x_test.shape,y_test.shape)

# + [markdown] colab_type="text" id="5_5eu-zZIjau"
# #7. Now, Model Testing!

# + [markdown] colab_type="text" id="ojQj8NWVIjau"
# **1. Logistic Regression**

# + colab_type="code" id="nR58bQIWIjau" colab={}
# fitting simple linear regression to the training set
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state=0)
classifier.fit(x_train, y_train)

# + colab_type="code" id="j86KN2kiSCfw" colab={}
# predicting the test set results
y_pred=classifier.predict(x_test)

# + [markdown] id="-xEECPYGgT_4" colab_type="text"
# Checking Accuracies

# + colab_type="code" id="RhO8K5LbSCfy" colab={}
from sklearn.metrics import confusion_matrix, classification_report

cm=confusion_matrix(y_test, y_pred)
plt.figure(figsize = (5,5))
sns.heatmap(cm, annot=True)
plt.xlabel('Predicted')
plt.ylabel('Truth')

# + id="FPVzN0GMjcNZ" colab_type="code" colab={}
print(classification_report(y_test, y_pred))

# + colab_type="code" id="NUrz_GT6Yjdq" colab={}
#applying k-fold cross validation
from sklearn.model_selection import cross_val_score as cvs
accuracies = cvs(estimator=classifier,X=x_train,y=y_train,cv=10)
print(accuracies.mean())
print(accuracies.std())

# + [markdown] colab_type="text" id="GzFEKzNQkmw_"
# **2. Random Forest**

# + colab_type="code" id="jJWyjA-ikmxB" colab={}
#fitting random forest classifier to the training set
from sklearn.ensemble import RandomForestClassifier as rfc
classifier = rfc(n_estimators=100,criterion='entropy',random_state=0)
classifier.fit(x_train, y_train)

# + colab_type="code" id="_j5JOYXJpNgv" colab={}
#predicting the test set results
y_pred=classifier.predict(x_test)

# + [markdown] colab_type="text" id="LWU0cg5RkmxC"
# Checking Accuracies

# + colab_type="code" id="pCnklzoZkmxD" colab={}
classifier.score(x_test, y_test)

# + colab_type="code" id="hWcw1CCKkmxF" colab={}
from sklearn.metrics import confusion_matrix, classification_report

cm=confusion_matrix(y_test, y_pred)
plt.figure(figsize = (5,5))
sns.heatmap(cm, annot=True)
plt.xlabel('Predicted')
plt.ylabel('Truth')

# + colab_type="code" id="KXBkFlWDkmxK" colab={}
print(classification_report(y_test, y_pred))

# + colab_type="code" id="QQr6JMLukmxO" colab={}
#applying k-fold cross validation
from sklearn.model_selection import cross_val_score as cvs
accuracies = cvs(estimator=classifier,X=x_train,y=y_train,cv=10)
print(accuracies.mean())
print(accuracies.std())

# + [markdown] colab_type="text" id="Lv7eOVZFpldL"
# **3. Kernel - SVM**

# + colab_type="code" id="vcA9hLg8pldM" colab={}
#fitting kernel SVM to the training set
from sklearn.svm import SVC
classifier = SVC(kernel='rbf', random_state=0)
classifier.fit(x_train, y_train)

# + colab_type="code" id="iGGEmqS0pldN" colab={}
#predicting the test set results
y_pred=classifier.predict(x_test)

# + [markdown] colab_type="text" id="Lk1H1kNspldR"
# Checking Accuracies

# + colab_type="code" id="TNwIfxn9pldR" colab={}
from sklearn.metrics import confusion_matrix, classification_report

cm=confusion_matrix(y_test, y_pred)
plt.figure(figsize = (5,5))
sns.heatmap(cm, annot=True)
plt.xlabel('Predicted')
plt.ylabel('Truth')

# + colab_type="code" id="KBxh_yLipldU" colab={}
print(classification_report(y_test, y_pred))

# + colab_type="code" id="A3d4HegxpldX" colab={}
#applying k-fold cross validation
from sklearn.model_selection import cross_val_score as cvs
accuracies = cvs(estimator=classifier,X=x_train,y=y_train,cv=10)
print(accuracies.mean())
print(accuracies.std())

# + [markdown] colab_type="text" id="vVNB972jqJez"
# **4. Linear - SVM**

# + colab_type="code" id="qyv_nvg5qKCD" colab={}
# fitting kernel SVM to the training set
from sklearn.svm import SVC
classifier = SVC(kernel='linear', random_state=0)
classifier.fit(x_train, y_train)

# + colab_type="code" id="BPnFj_ptqKCE" colab={}
#predicting the test set results
y_pred=classifier.predict(x_test)

# + [markdown] colab_type="text" id="Tu4JBCKCqKCG"
# Checking Accuracies

# + colab_type="code" id="cQh-nVWLqKCG" colab={}
from sklearn.metrics import confusion_matrix, classification_report

cm=confusion_matrix(y_test, y_pred)
plt.figure(figsize = (5,5))
sns.heatmap(cm, annot=True)
plt.xlabel('Predicted')
plt.ylabel('Truth')

# + colab_type="code" id="flNGrZkGqKCI" colab={}
print(classification_report(y_test, y_pred))

# + colab_type="code" id="jNDZHrVmqKCK" colab={}
#applying k-fold cross validation
from sklearn.model_selection import cross_val_score as cvs
accuracies = cvs(estimator=classifier,X=x_train,y=y_train,cv=10)
print(accuracies.mean())
print(accuracies.std())

# + [markdown] colab_type="text" id="a-OIVjqqqJtd"
# **5. K-NN**

# + colab_type="code" id="P9YbA9F2qJte" colab={}
#fitting knn to the training set
from sklearn.neighbors import KNeighborsClassifier as knc
classifier=knc(n_neighbors=10,metric='minkowski', p = 2)
classifier.fit(x_train, y_train)

# + colab_type="code" id="kzXoK6TkqJtf" colab={}
#predicting the test set results
y_pred=classifier.predict(x_test)

# + [markdown] colab_type="text" id="qwffZTkZqJtg"
# Checking Accuracies

# + colab_type="code" id="OtzUfJ4aqJtg" colab={}
from sklearn.metrics import confusion_matrix, classification_report

cm=confusion_matrix(y_test, y_pred)
plt.figure(figsize = (5,5))
sns.heatmap(cm, annot=True)
plt.xlabel('Predicted')
plt.ylabel('Truth')

# + colab_type="code" id="iTcx2WA9qJti" colab={}
print(classification_report(y_test, y_pred))

# + colab_type="code" id="qHDJG2-HqJtk" colab={}
#applying k-fold cross validation
from sklearn.model_selection import cross_val_score as cvs
accuracies = cvs(estimator=classifier,X=x_train,y=y_train,cv=10)
print(accuracies.mean())
print(accuracies.std())

# + [markdown] colab_type="text" id="YR3IiUvEqKCC"
# **6. Decision Tree**

# + colab_type="code" id="SUAluYI2qJe0" colab={}
#fitting decision tree classifier to the training set
from sklearn.tree import DecisionTreeClassifier as dtc
classifier = dtc(criterion='entropy' , random_state=0)
classifier.fit(x_train, y_train)

# + colab_type="code" id="SZmJSmWnqJe6" colab={}
#predicting the test set results
y_pred=classifier.predict(x_test)

# + [markdown] colab_type="text" id="3QpF3xYKqJe8"
# Checking Accuracies

# + colab_type="code" id="RKs-uwlXqJe8" colab={}
from sklearn.metrics import confusion_matrix, classification_report

cm=confusion_matrix(y_test, y_pred)
plt.figure(figsize = (5,5))
sns.heatmap(cm, annot=True)
plt.xlabel('Predicted')
plt.ylabel('Truth')

# + colab_type="code" id="Syc4YY4hqJe-" colab={}
print(classification_report(y_test, y_pred))

# + colab_type="code" id="pzd1R0k0qJfA" colab={}
#applying k-fold cross validation
from sklearn.model_selection import cross_val_score as cvs
accuracies = cvs(estimator=classifier,X=x_train,y=y_train,cv=10)
print(accuracies.mean())
print(accuracies.std())

# + [markdown] colab_type="text" id="UrSGd67DrgEK"
# **7. Naive Bayes**

# + colab_type="code" id="_0nk7euMrgEL" colab={}
#fitting naive bayes to the training set
from sklearn.naive_bayes import GaussianNB
classifier = GaussianNB()
classifier.fit(x_train, y_train)

# + colab_type="code" id="vAq6XGmqrgEM" colab={}
#predicting the test set results
y_pred=classifier.predict(x_test)

# + [markdown] colab_type="text" id="0CkWQEDfrgEP"
# Checking Accuracies

# + colab_type="code" id="cZYPyK3VrgEP" colab={}
from sklearn.metrics import confusion_matrix, classification_report

cm=confusion_matrix(y_test, y_pred)
plt.figure(figsize = (5,5))
sns.heatmap(cm, annot=True)
plt.xlabel('Predicted')
plt.ylabel('Truth')

# + colab_type="code" id="Euv8VFXxrgER" colab={}
print(classification_report(y_test, y_pred))

# + colab_type="code" id="xNbEdu2ergEU" colab={}
#applying k-fold cross validation
from sklearn.model_selection import cross_val_score as cvs
accuracies = cvs(estimator=classifier,X=x_train,y=y_train,cv=10)
print(accuracies.mean())
print(accuracies.std())

# + [markdown] colab_type="text" id="hzJFtHsZsUyb"
# **8. XGBoost Classifier**

# + colab_type="code" id="mgZ61NhmsUyc" colab={}
#fitting XGBoost to the Training Set
from xgboost import XGBClassifier
classifier=XGBClassifier()
classifier.fit(x_train,y_train)

# + colab_type="code" id="rp0VUMK-sUyi" colab={}
#predicting the test set results
y_pred=classifier.predict(x_test)

# + [markdown] colab_type="text" id="SuMgmVuksUyk"
# Checking Accuracies

# + colab_type="code" id="amoRMRDSsUyk" colab={}
from sklearn.metrics import confusion_matrix, classification_report

cm=confusion_matrix(y_test, y_pred)
plt.figure(figsize = (5,5))
sns.heatmap(cm, annot=True)
plt.xlabel('Predicted')
plt.ylabel('Truth')

# + colab_type="code" id="2_fNRIA7sUyo" colab={}
print(classification_report(y_test, y_pred))

# + colab_type="code" id="nly8fw_KsUyr" colab={}
#applying k-fold cross validation
from sklearn.model_selection import cross_val_score as cvs
accuracies = cvs(estimator=classifier,X=x_train,y=y_train,cv=10)
print(accuracies.mean())
print(accuracies.std())

# + [markdown] colab_type="text" id="GKnpiryhu-F2"
# **9. GradientBoosting Classifier**

# + colab_type="code" id="UZek8h6Su-F2" colab={}
#fitting XGBoost to the Training Set
from sklearn.ensemble import GradientBoostingClassifier
classifier = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1)
classifier.fit(x_train, y_train)

# + colab_type="code" id="mrttc1IRu-F4" colab={}
#predicting the test set results
y_pred=classifier.predict(x_test)

# + [markdown] colab_type="text" id="A-ltyB2Qu-F7"
# Checking Accuracies

# + colab_type="code" id="_PJxZHTeu-F8" colab={}
from sklearn.metrics import confusion_matrix, classification_report

cm=confusion_matrix(y_test, y_pred)
plt.figure(figsize = (5,5))
sns.heatmap(cm, annot=True)
plt.xlabel('Predicted')
plt.ylabel('Truth')

# + colab_type="code" id="y0a8X3vAu-F9" colab={}
print(classification_report(y_test, y_pred))

# + colab_type="code" id="P8OLxso9u-F_" colab={}
#applying k-fold cross validation
from sklearn.model_selection import cross_val_score as cvs
accuracies = cvs(estimator=classifier,X=x_train,y=y_train,cv=10)
print(accuracies.mean())
print(accuracies.std())

# + [markdown] colab_type="text" id="1zNRqf29vA4U"
# **10. AdaBoost Classifier**

# + colab_type="code" id="8Lovw1dcvA4V" colab={}
#fitting XGBoost to the Training Set
from sklearn.ensemble import AdaBoostClassifier
dt = dtc()
classifier = AdaBoostClassifier(n_estimators = 100, base_estimator = dt, learning_rate = 1)
classifier.fit(x_train,y_train)

# + colab_type="code" id="NuV0xAWYvA4X" colab={}
#predicting the test set results
y_pred=classifier.predict(x_test)

# + [markdown] colab_type="text" id="p2wBVObmvA4Y"
# Checking Accuracies

# + colab_type="code" id="Rv6s4RBvvA4Y" colab={}
from sklearn.metrics import confusion_matrix, classification_report

cm=confusion_matrix(y_test, y_pred)
plt.figure(figsize = (5,5))
sns.heatmap(cm, annot=True)
plt.xlabel('Predicted')
plt.ylabel('Truth')

# + colab_type="code" id="qCSxXgy3vA4c" colab={}
print(classification_report(y_test, y_pred))

# + colab_type="code" id="Hq1wipf3vA4e" colab={}
#applying k-fold cross validation
from sklearn.model_selection import cross_val_score as cvs
accuracies = cvs(estimator=classifier,X=x_train,y=y_train,cv=10)
print(accuracies.mean())
print(accuracies.std())

# + [markdown] colab_type="text" id="7ORgTR_wxF25"
# **11. CatBoost Classifier**

# + id="VATZrvaLxKuo" colab_type="code" colab={}
# !pip install catboost

# + colab_type="code" id="VmQcpYovxF26" colab={}
#fitting CatBoost to the Training Set
from catboost import CatBoostClassifier
classifier = CatBoostClassifier(iterations=100, learning_rate=0.01)
classifier.fit(x_train,y_train, eval_set = (x_test, y_test))

# + colab_type="code" id="Z9dpYysixF2-" colab={}
#predicting the test set results
y_pred=classifier.predict(x_test)

# + [markdown] colab_type="text" id="DJFEn_uPxF3A"
# Checking Accuracies

# + colab_type="code" id="KtKGYjDPxF3A" colab={}
from sklearn.metrics import confusion_matrix, classification_report

cm=confusion_matrix(y_test, y_pred)
plt.figure(figsize = (5,5))
sns.heatmap(cm, annot=True)
plt.xlabel('Predicted')
plt.ylabel('Truth')

# + colab_type="code" id="Q3GS2R7pxF3D" colab={}
print(classification_report(y_test, y_pred))

# + colab_type="code" id="bo_znuiOxF3F" colab={}
#applying k-fold cross validation
from sklearn.model_selection import cross_val_score as cvs
accuracies = cvs(estimator=classifier,X=x_train,y=y_train,cv=10)
print(accuracies.mean())
print(accuracies.std())

# + [markdown] colab_type="text" id="eDoNlbY3IjbD"
# **12. Light GBM**

# + colab_type="code" id="rbooI6RhIjbD" colab={}
# !pip install lightgbm

# + colab_type="code" id="WpOPh3A1IjbE" colab={}
import lightgbm as lgbm
# from sklearn import preprocessing

# + colab_type="code" id="veOuOZS7IjbG" colab={}
# kfold = KFold(n_splits=5, random_state = 0, shuffle = True)

model_lgb = lgbm.LGBMClassifier(n_iterations =50, silent = False)
model_lgb.fit(x_train, y_train)
model_lgb.score(x_test, y_test)

# + [markdown] id="wTSSaAezfgUF" colab_type="text"
# Therefore, the accuracies are - <br>
# logistic regression 0.67 <br>
# random forest 0.68 <br>
# kernel svm 0.69 <br>
# linear svm 0.71 <br>
# knn 0.67 <br>
# decision tree 0.50 <br>
# naive bayes 0.43 <br>
# xgboost 0.66 <br>
# gradient boosting 0.60 <br>
# adaboost 0.50 <br>
# lightgbm 0.65 <br>
# catboost 0.65 <br>

# + [markdown] colab_type="text" id="jFMye-RjIjbP"
# # So, our Best Model Selected according to its performance is Linear-SVM !

# + [markdown] colab_type="text" id="DaBQMxZ3IjbP"
# # 8. Hyperparameter Tuning and Model Optimization

# + [markdown] colab_type="text" id="kYiW7PxNIjbQ"
# Using GridSearch for searching best hyperparameter.   
# Model: Linear-SVM

# + colab_type="code" id="0tYW6WVsIjbQ" colab={}
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import StratifiedKFold

# + id="Wr0-r7Al4BXp" colab_type="code" colab={}
print(SVC().get_params().keys())

# + _cell_guid="c7496af3-31ed-4195-8797-7888f4410c3e" _uuid="31a5e026eac04f2edee67e09918ceddaf27ff629" colab_type="code" id="qdUq8aX-IjbQ" colab={}
#using grid search method to find out the best groups of hyperparameters
clas_cv = GridSearchCV(SVC(), {'C' : [1.0, 10, 100], "gamma" : ['scale'],
                              'kernel': ['linear'], 'kernel': ['linear'], 'random_state': [0], 
                               'cache_size' : [200], 'degree': [3], 'coef0' : [0.0],
                               'decision_function_shape' : ['ovr'], 'tol' : [0.001], 'gamma' : ['scale'], 
                               'max_iter' : [-1] }, scoring = 'accuracy', cv = 10, verbose=1)
clas_cv.fit(x_train,y_train)

# + id="_stSV4NOkms9" colab_type="code" colab={}
best_parameters=clas_cv.best_params_

# + _cell_guid="cc978005-bea6-4ae8-a9ad-e7e7faef8f50" _uuid="2981044867820f15b04dc054b0d5a27acc76179b" colab_type="code" id="HoZ9bEzzIjbS" colab={}
#dictionary of the best parameters
clas_cv.best_params_

# + [markdown] _cell_guid="88574b66-b3c6-45f2-bf87-abb18714d34e" _uuid="85de70c233d62f08fe560b169f7465806c2dde54" colab_type="text" id="KY9yL4ZgIjbT"
# Train data using Linear-SVM with best parameters

# + _cell_guid="d7917d17-2f9a-48d3-97f6-4f579b24fb87" _uuid="dba01178f5fec3076f113d85f132828e59d6004e" colab_type="code" id="PYDnh4s_IjbT" colab={}
c_svm = SVC(**clas_cv.best_params_)
c_svm.fit(x_train,y_train)

# + [markdown] _cell_guid="e19d3903-b989-4920-89d7-991573c6bf4a" _uuid="9917f23302d627e44ad847df8a7bf1cd27d75d4d" colab_type="text" id="mWXWwOfmIjbU"
# Predicting the Results.

# + _cell_guid="2c1f987b-4b3e-46fb-9e97-82aad2058b44" _uuid="a5660ec00c22ff2f55d51c26566b8490f3f248ed" colab_type="code" id="x8IsmZ0VIjbU" colab={}
y_pred = c_svm.predict(x_test)
y_pred

# + [markdown] _cell_guid="6b7fd0fa-b746-4205-ac75-f5c6d6b28cf0" _uuid="5ec47c743cb6aed59901df918a26564fb0e74050" colab_type="text" id="5JgfK0QXIjbV"
# Evaluating its Score

# + colab_type="code" id="JEN3t_pVOStv" colab={}
from sklearn.metrics import confusion_matrix, classification_report

cm=confusion_matrix(y_test, y_pred)
plt.figure(figsize = (5,5))
sns.heatmap(cm, annot=True)
plt.xlabel('Predicted')
plt.ylabel('Truth')

print(confusion_matrix(y_test, y_pred)) ; print("\n\n")

# + id="QniwlIZhifrA" colab_type="code" colab={}
print(classification_report(y_test, y_pred))

# + id="9u5PcPRgIfH1" colab_type="code" colab={}
c_svm.score(x_test, y_test), c_svm.score(x_train, y_train)

# + [markdown] colab_type="text" id="6axCZo0JIjbY"
# # 9. Saving the Model

# + colab_type="code" id="kMxOR0fqIjbZ" colab={}
import pickle
filename = 'amcat.sav'
pickle.dump(c_svm, open(filename, 'wb'))

# + id="1GipEsIZK5Up" colab_type="code" colab={}
# to check the integrity
loaded_model = pickle.load(open(filename,'rb'))
result = loaded_model.score(x_test, y_test)
print(result)

# + [markdown] colab_type="text" id="OFP1s3ZuIjbZ"
# # 10. Creating a ML Pipeline

# + colab_type="code" id="sF-QHf1VIjbZ" colab={}
from sklearn.pipeline import Pipeline

# + [markdown] id="jT-Iehsi15uA" colab_type="text"
# Pre-Processing

# + id="rzZy8NrATGUL" colab_type="code" colab={}
## ingore

from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import FunctionTransformer

label_enc = ['Gender']
target_enc = ['Specialization in study', 'Year of Completion of college', '12th Completion year', '10th Completion Year']
boxcox_f = ['10th Completion Year', '12th Completion year', 'Specialization in study', 'Year of Completion of college']

preprocessor = ColumnTransformer(
    transformers=[
                  ('boxcox', FunctionTransformer(stats.boxcox, validate=False), boxcox_f),
                  ('label', FunctionTransformer(LabelEncoder, validate=False), label_enc),
                  ('target', FunctionTransformer(TargetEncoder, validate=False), target_enc),
                  ('scale', StandardScaler())], 
                  remainder = 'passthrough')

# + [markdown] id="Ciwm30Q72R7U" colab_type="text"
# Modeling

# + id="3rVGiYkcUeMR" colab_type="code" colab={}
## ingore

log_reg = Pipeline(steps=[('preprocess', preprocessor),
                          ('linear_svm', SVC())
                          ]).fit(x_train, y_train);

# + colab_type="code" id="-P3xheFLIjba" colab={}
pipe = Pipeline([('standard', StandardScaler()),
                    #('boxcox'), stats.boxcox()),
                    ('l-svm', SVC())])

# + colab_type="code" id="fETU3ZceIjbb" colab={}
pipe.fit(x_train, y_train)

# + colab_type="code" id="_iHX2DIAIjbb" colab={}
score = pipe.score(x_test, y_test)
print('Linear-SVM pipeline test accuracy: %.3f' % score)

# + [markdown] id="iTpv_qcB1nZj" colab_type="text"
# # 11. Generating Requirements File

# + id="tvpVDLDj0BWB" colab_type="code" colab={}
# !pip freeze > requirements.txt

# + [markdown] colab_type="text" id="yBVggzRoIjbc"
# # End.

StarcoderdataJupyterScriptsDedupFiltered