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

# + [markdown] toc=true
# <h1>Table of Contents<span class="tocSkip"></span></h1>
# <div class="toc"><ul class="toc-item"><li><span><a href="#Loading-Libraries" data-toc-modified-id="Loading-Libraries-1"><span class="toc-item-num">1&nbsp;&nbsp;</span>Loading Libraries</a></span></li><li><span><a href="#Loading-Data" data-toc-modified-id="Loading-Data-2"><span class="toc-item-num">2&nbsp;&nbsp;</span>Loading Data</a></span></li><li><span><a href="#Visualization" data-toc-modified-id="Visualization-3"><span class="toc-item-num">3&nbsp;&nbsp;</span>Visualization</a></span></li><li><span><a href="#Data-Preprocessing" data-toc-modified-id="Data-Preprocessing-4"><span class="toc-item-num">4&nbsp;&nbsp;</span>Data Preprocessing</a></span></li><li><span><a href="#Modelling" data-toc-modified-id="Modelling-5"><span class="toc-item-num">5&nbsp;&nbsp;</span>Modelling</a></span></li><li><span><a href="#Evaluation" data-toc-modified-id="Evaluation-6"><span class="toc-item-num">6&nbsp;&nbsp;</span>Evaluation</a></span></li></ul></div>
# -

# # Loading Libraries

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib inline
import sklearn
import warnings
warnings.filterwarnings("ignore")


# # Loading Data

df = pd.read_csv('Datasets/applicant.csv',names=[
        "Age","Workclass","fnlwgt","Education","Education-Num","Marital Status",
        "Occupation","Relationship","Race","Gender","Capital Gain","Capital Loss",
        "Hours per week","Country","Target"],
    sep=r'\s*,',
    engine='python',
    na_values='?')
df.head()

df.shape

df.info()

df = df.replace('?',np.nan)
df = df.replace(' ?',np.nan)

for i in list(df.columns):
    print(i)
    
    print(df.loc[:,i].unique())
    print('=====================================================')

df.isna().sum()

df.dropna(inplace=True)
df.shape

df.info()

# # Visualization

plt.figure(figsize=(10,7))
plt.subplot(1,2,1)
sns.scatterplot(data = df , x = 'Age' , y = 'Capital Gain',hue = 'Target',palette='flare',alpha=0.3);
plt.subplot(1,2,2)
sns.scatterplot(data = df , x = 'Age' , y = 'Capital Loss',hue = 'Target',palette='flare',alpha = 0.3);

sns.countplot(data = df , x = 'Target');


# <strong> <font color= "green"> The dataset is imbalanced

# # Data Preprocessing

# let's make age  as category
df['Age'] = pd.cut(df['Age'],bins = 20,labels = ["Age_" + str(i) for i in range(20)])


cat_features = []
num_features = []
for i in list(df.columns):
    if (df.dtypes[i]) ==('int64'):
        num_features.append(i)
    else:
        cat_features.append(i)
        

num_features

cat_features

from sklearn.preprocessing import OrdinalEncoder,LabelEncoder,StandardScaler
sc = StandardScaler()
oe = OrdinalEncoder()
le = LabelEncoder()

# +
df[cat_features] = oe.fit_transform(df[cat_features])


# -

X = df.drop('Target',axis = 1)
y = df['Target']

# +
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42)


# -

X_train.shape,y_train.shape

# +

X_train.loc[:,num_features] = sc.fit_transform(X_train.loc[:,num_features])
X_test.loc[:,num_features] = sc.transform(X_test.loc[:,num_features])


# -

# # Modelling

from sklearn.svm import SVC
sv = SVC(kernel = 'rbf',C = 1.0)
sv.fit(X_train,y_train)
y_pred = sv.predict(X_test)

# # Evaluation

from sklearn.metrics import accuracy_score,confusion_matrix
accuracy_score(y_pred , y_test)

sns.heatmap(confusion_matrix(y_test,y_pred),annot=True)
plt.ylabel('True Class')
plt.xlabel('Predicted Class');

StarcoderdataJupyterScriptsDedupFiltered