# ---
# 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] id="e1hsulTtnq4o" colab_type="text"
# # first we are importing the modules and libraries neccessary for running the **dynamic** python code
#
#

# + id="VAvkwfQEkPqn" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 306} outputId="38f950ae-b808-4a3d-e32d-0007911abbc0"
import pandas as pd
# !pip install ctrl4ai
from ctrl4ai import preprocessing
from ctrl4ai import automl
from ctrl4ai import datasets
from ctrl4ai import helper
import numpy as np


# + id="XD2qY92qknTG" colab_type="code" colab={}
dataset = pd.read_csv('titanic.csv')

# + id="gGVL-76GOprM" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 697} outputId="a6c169b9-0f49-4dfe-b4a8-ba679296c132"
y = dataset['Survived']

# + id="DJDbgE3Q7Boi" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="d0427951-2350-43e8-f191-9638b3d027e0"
len(y.index)

# + id="VeC3gExJOpu7" colab_type="code" colab={}
dataset = dataset.drop('Survived', axis=1)

# + id="0oGYAOz1Opzg" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="42cdde43-955c-49b2-967a-79713e3d8377"
dataset.head()

# + [markdown] id="nvrtL3QsoAxN" colab_type="text"
#
# # here we are declaring for the x and y which our client will give to us

# + [markdown] id="FQPB5PVpoqxt" colab_type="text"
# # **droping the single valued columns**

# + id="O7uo8-MAmKTp" colab_type="code" colab={}
#for droping the single valued columns
#it checks for the single valued column and store as list in e........
from ctrl4ai import helper
single_valued_column = [ i for i in dataset.columns
            if helper.single_valued_col(dataset[i]) == True ]
# the columns stored in e is then removed from the dataset columns using loop
for e in single_valued_column:
    DataFrame.drop(labels=single_valued_column[e], axis=1)  
# single valued columns are removed from the dataset...............

# + [markdown] id="LF7VcElRpE3X" colab_type="text"
# # **Dropping non-categorical non-numeric columns**

# + id="ztZDxu9pmKd7" colab_type="code" colab={}
# list of item which are categorical variable in our dataset
from ctrl4ai import helper
categorical_variable = [ i for i in dataset.columns
            if helper.check_categorical_col(dataset[i]) == True ]

# + id="3IayWGFGmKg8" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="4b87cca9-0b15-4825-b070-ae3609b8e429"
t = preprocessing.drop_non_numeric(dataset)
li1 = dataset.columns
li2 = t.columns
temp3 = [item for item in li1 if item not in li2]
li3 = categorical_variable
li4 = temp3
temp4 =  [item for item in temp3 if item in li3]
to_be_removed = [item for item in temp3 if item not in temp4]
new_data_set = dataset.drop(to_be_removed, axis=1)

# + id="ltIh1x1VmKjX" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="d84c6254-cde5-41ed-98e0-51a537478814"
new_data_set.head()

# + id="LnipkGoKt4zJ" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="f334234c-c5ca-4a18-cd06-b7461a41a758"
new_data_set_1 = preprocessing.drop_null_fields(new_data_set, dropna_threshold=0.3)

# + id="o5f_e7Vqs60g" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="4c3d1b8c-d6ff-4415-dfc1-fde0ef63597b"
new_data_set_1.head()

# + id="msZQhANyzVRp" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 453} outputId="34289b85-957e-436e-db12-29dd16984d92"
# imputing th values in the null fields odf the dataframe 
# for continuos data we do mean and for categorical data we do mode over here....


preprocessing.impute_nulls(new_data_set_1, method='central_tendency')

# + id="9mZWSxSUzVxV" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="c3a3d2db-db30-417e-fc8f-2e501aeaf476"
new_data_set_2.isnull().values.any()

# + id="NIXFVy7NzWGq" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="708deb71-d448-4bc2-8768-4a389e833210"
len(new_data_set_2.index)

# + id="qYvAs65W5xn_" colab_type="code" colab={}


# + id="PxIgxpri5xrS" colab_type="code" colab={}


# + id="LPWtinxBpyxN" colab_type="code" colab={}
from ctrl4ai import helper
categorical_variable = [ i for i in new_data_set_1.columns
            if helper.check_categorical_col(new_data_set_1[i]) == True ]



# + id="oVS8ud6i8vD2" colab_type="code" colab={}
 # create dummy variables for multiple categories
# drop_first=True handles k - 1 

data_set = pd.get_dummies(new_data_set_1, columns=categorical_variable, drop_first=True)

 # this drops original Sex and Embarked columns
# and creates dummy variables

# + id="ihP2P_96HCNG" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 439} outputId="4c4cb055-e103-4449-d23e-c476ccb10325"
data_set

# + id="y8YnDnRNHCQJ" colab_type="code" colab={}
final_data_set = data_set.drop('PassengerId', axis=1)

# + id="f6HIMR5O6CmF" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="e5146d85-2f05-440e-f49f-e25a3ef316e6"


# + id="lSYTmNyZJEk-" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 439} outputId="0e2c0040-6105-4d45-b0fd-350f9491efe0"
final_data_set

# + id="OlE0hj-kJEoc" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="99428176-fbaf-411d-bef9-55f44d81b2d2"
len(y.index)

# + id="p-V8mqDfXyC0" colab_type="code" colab={}
#converting the integer of the pandaas dataframe to float.....
final_data_set = final_data_set.astype(float)

# + id="RXE3bXCpYuQu" colab_type="code" colab={}
X = final_data_set

# + id="bRFjnSNE6KYa" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="4b25153a-018e-4046-9064-e8a4af69d9ad"
len(X.index)

# + id="Cm79RW5Y6Kpf" colab_type="code" colab={}


# + id="9FkA4l1fJEtI" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 439} outputId="b4d5ffd7-6e00-4436-f541-86a5bab7183b"
X

# + id="36AZ8H89dV3N" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="a1e665ab-cf88-46c7-e441-110a27883a3b"
len(X.index)

# + id="wDZ29b1rdFsF" colab_type="code" 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,random_state=42)

# + id="XLx_8LUxeB6s" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="db3c4fd1-9cd6-4adc-dca3-569fd30cef0b"
# null values are still present

final_data_set.isnull().values.any()

# + id="O6jMffrHisxj" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="526d7ce3-eddf-40e2-b1a5-758ca17db946"
final_data_set.isnull().sum().sum()

# + id="7xsgAdtbis9i" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="9bd87e2e-37d7-491d-c6ac-53aa6df68767"
y_train.shape

# + id="5eQcYn7iIUNU" colab_type="code" colab={}
from  sklearn import preprocessing

# + id="4hpxT5TDHCSG" colab_type="code" colab={}
X_num = X_train.to_numpy()

# + id="WPuwTEFdbgTg" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="e37ea52e-5b89-41e0-93ec-3cd7d3a08a4d"
X_num.shape

# + id="U93kz5oyG99y" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 238} outputId="bf7f20bb-7a9c-4627-ee1c-c27cb28aab7d"
min_max_scaler = preprocessing.MinMaxScaler()
X_train_scaled = min_max_scaler.fit_transform(X_num)
X_train_scaled

# + id="qRhBACOuZnL7" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 85} outputId="68974f82-a305-4051-dd99-8e1457618195"
X_test_scaled= min_max_scaler.transform(X_test)
X_test_scaled
min_max_scaler.min_
min_max_scaler.scale_

# + id="FS9dQ_iscdVm" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 238} outputId="c59d23c1-d4fb-453b-995c-112682cc982c"
X_train_scaled

# + id="R1ZCGHTqcdYd" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 238} outputId="5e59b9e7-5917-4439-84a9-b6db2df376e4"
X_test_scaled

# + id="5vCQTP9thdzp" colab_type="code" colab={}
X_train_scaled_roundoff = np.around(X_train_scaled, decimals = 4) 

# + id="ndoNFyvVhd3c" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 136} outputId="a2c77ad2-9cc3-4b7f-a960-872240c0a0e5"
X_train_scaled_roundoff

# + id="0LRy6oyHhd58" colab_type="code" colab={}
X_test_scaled_roundoff = np.around(X_test_scaled, decimals = 4) 

# + id="wlvHOVcacdbZ" colab_type="code" colab={}
# import the class
from sklearn.linear_model import LogisticRegression

# instantiate the model (using the default parameters)
logreg = LogisticRegression()

# fit the model with data
logreg.fit(X_train_scaled,y_train)

y_pred=logreg.predict(X_test_scaled)

# + id="C6r3t1s8cdeA" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="73f16ffb-8693-4b45-8eff-6030a2e2e7b5"
X_train_scaled.shape

# + id="KYhbdo6764_G" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="bbbba7b8-6b69-439c-e057-f098c9830416"
y_train.shape

# + id="uXPy_xhA68vO" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 51} outputId="63e71b67-2cbd-4597-c520-1c24edae2cfb"
# import the metrics class
from sklearn import metrics
cnf_matrix = metrics.confusion_matrix(y_test, y_pred)
cnf_matrix

# + id="6nJ162L771u5" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 71} outputId="1703c099-83e5-4ed9-8d65-d2114ede00fb"
# import required modules
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib inline

# + id="vjQKbeC3774C" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 342} outputId="9a1bc3b6-c4a8-42ff-e944-6c00108ec51d"
class_names=[0,1] # name  of classes
fig, ax = plt.subplots()
tick_marks = np.arange(len(class_names))
plt.xticks(tick_marks, class_names)
plt.yticks(tick_marks, class_names)
# create heatmap
sns.heatmap(pd.DataFrame(cnf_matrix), annot=True, cmap="YlGnBu" ,fmt='g')
ax.xaxis.set_label_position("top")
plt.tight_layout()
plt.title('Confusion matrix', y=1.1)
plt.ylabel('Actual label')
plt.xlabel('Predicted label')

# + id="-whH6_MW777F" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 68} outputId="d822f000-464e-4b1d-898c-cdb97b28996e"
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
print("Precision:",metrics.precision_score(y_test, y_pred))
print("Recall:",metrics.recall_score(y_test, y_pred))

# + id="gBDFXeVb8A60" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 265} outputId="b364e0dd-605e-46c3-9fbe-6dde77521ccb"
y_pred_proba = logreg.predict_proba(X_test)[::,1]
fpr, tpr, _ = metrics.roc_curve(y_test,  y_pred_proba)
auc = metrics.roc_auc_score(y_test, y_pred_proba)
plt.plot(fpr,tpr,label="data 1, auc="+str(auc))
plt.legend(loc=4)
plt.show()

# + id="LNbOlIKJ8EMM" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered