# ---
# 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
# ---

# + _cell_guid="b1076dfc-b9ad-4769-8c92-a6c4dae69d19" _uuid="8f2839f25d086af736a60e9eeb907d3b93b6e0e5"
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

import os

# + _cell_guid="79c7e3d0-c299-4dcb-8224-4455121ee9b0" _uuid="d629ff2d2480ee46fbb7e2d37f6b5fab8052498a"
credit = pd.read_csv("../input/creditcard.csv")

# + _cell_guid="b63c6074-eb3b-4110-b918-08b996e6b1c8" _uuid="f5bab133328b86d20417623a69d545a577ec5aa9"
credit.head()

# + _cell_guid="04ac04bf-7c02-49a8-aa1c-580077b2d5fd" _uuid="49d4a6874e66c0b9f4e416c36d19ab9e535a65d5"
credit.describe()

# + _cell_guid="4bc59dc5-de33-4139-8af1-6eea699d8969" _uuid="2659b878b7abf4c4262ead811f119ae15d4a07b6"
credit.info()

# + _cell_guid="2412442e-5821-4d85-9054-7839e473c95d" _uuid="da30f2171de3b8e12a530cb7ef24966ef6d9d678"
credit.shape

# + _cell_guid="72100678-cb32-46ae-82ed-6b2be15ba93c" _uuid="4f01559186df53acff968f8b1bef4070029535a6"
credit.loc[:, 'Class'].value_counts()

# + _cell_guid="0e289cd4-c30a-4fcd-9f03-7832be28eaa7" _uuid="8d5bdbb31c65cfe54b09a64292bdac7f1a85e63f"
V_col = credit[['V1','V2','V3','V4','V5','V6','V7','V8','V9','V10','V11','V12','V13','V14','V15','V16','V17','V18','V19','V20','V21','V22','V23','V24','V25','V26','V27','V28']]
V_col.head()

# + _cell_guid="63257660-2df9-4c91-8fed-6f8eb4cdb26a" _uuid="0f735912a59ae6808a4d42c030a9c6dd061895cb"
V_col.hist(figsize=(30, 20))
plt.show()

# + _cell_guid="237bda40-513b-4958-9e23-f6a45652673c" _uuid="761b4094d7232e01b141da031517673d5d81148f"
no_of_normal_transcations = len(credit[credit['Class']==1])
no_of_fraud_transcations = len(credit[credit['Class']==0])
print("no_of_normal_transcations:",no_of_normal_transcations)
print("no_of_fraud_transcations:", no_of_fraud_transcations)

# + _cell_guid="5873d0c7-24a7-4742-9ea3-0fb1021097ca" _uuid="3c2c91876060ae6dfb8620ae3122f0bcb03d9581"
X = credit.iloc[:, 1:29].values
y = credit.iloc[:, 30].values

# + _cell_guid="21f9c215-539c-4c79-b525-b442bf8d008a" _uuid="dcb6acbab4b3d56193b69247e65dfe49cf4affdf"
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 = 0)
print ("X_train: ", len(X_train))
print("X_test: ", len(X_test))
print("y_train: ", len(y_train))
print("y_test: ", len(y_test))

# + [markdown] _cell_guid="c4ad6861-93cf-45bf-92d3-65097b1613bc" _uuid="26192e3f9eb7314e1cd56f9e7b24b4533c8596c7"
# ***KNN Classification***

# + _cell_guid="eb28fbd6-6be1-4d62-9550-1d87970ec69a" _uuid="b7c1a64c71fc6ebf12c7b440741537788afebf5a"
from sklearn.neighbors import KNeighborsClassifier
knc = KNeighborsClassifier(n_neighbors = 17)
X,y = credit.loc[:,credit.columns != 'Class'], credit.loc[:,'Class']
knc.fit(X_train,y_train)
y_knc = knc.predict(X_test)
print('accuracy of training set: {:.4f}'.format(knc.score(X_train,y_train)))
print('accuracy of test set: {:.4f}'.format(knc.score(X_test, y_test)))

# + _cell_guid="b3f88fa5-5506-4406-898a-7d50969a9d80" _uuid="65d3ba1902dfa6ab7bddee2de9cbf4e3b9d3152d"
from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score, precision_recall_curve
print('confusion_matrix of KNN: ', confusion_matrix(y_test, y_knc))
print('precision_score of KNN: ', precision_score(y_test, y_knc))
print('recall_score of KNN: ', recall_score(y_test, y_knc))
print('precision_recall_curve: ', precision_recall_curve(y_test, y_knc))

# + [markdown] _cell_guid="2998f019-308e-40a2-8681-09ebdc1e9ae1" _uuid="d4d8cd3b428f3f79e65c30602e5dbb8186ab3168"
# **Random Forest Regression**

# + _cell_guid="a8fd93d4-7199-45f0-a334-5ca9c29a4a75" _kg_hide-input=false _uuid="641188a91da19749127f8b7f0ccb82379ba67302"
from sklearn.ensemble import RandomForestRegressor
reg = RandomForestRegressor(n_estimators = 20, random_state = 0)
reg.fit(X_train,y_train)

# + _cell_guid="c1e4e5d7-4f4d-430a-96ec-5acaf267c1c3" _uuid="64fb5764169e8ecf009ee23452ec7a3524da8203"
y_rfr = reg.predict(X_test)

# + _cell_guid="f56c800d-0881-459a-899e-224a66da334e" _uuid="45bde06ab105d19d179827c3948569c7b0231680"
reg.score(X_test, y_test)
print('accuracy of training set: {:.4f}'.format(reg.score(X_train,y_train)))
print('accuaracy of test set: {:.4f}'.format(reg.score(X_test, y_test)))
# -

print('accuracy_score of decision tree regression: ', accuracy_score( y_dtr , y_test))
print('confusion_matrix of decision tree regression: ', confusion_matrix(y_dtr, y_test))
print('precision_score of decision tree regression: ', precision_score( y_dtr, y_test))
print('recall_score of decision tree regression: ', recall_score( y_dtr, y_test))
print('precision_recall_curve: ', precision_recall_curve(y_dtr, y_test))

# + [markdown] _cell_guid="75fb6c29-e72c-4941-a7bb-b1c3f761a8d2" _uuid="14639bdd0422eab84cade5702a37d16bc1e02ce5"
# **Decision Tree Regression**

# + _cell_guid="677d4cd3-f791-42a4-9bca-52475843c488" _uuid="88d76a71f3ad8577ec8f922469a0cf0ec1a2206b"
from sklearn.tree import DecisionTreeRegressor
regs = DecisionTreeRegressor(random_state = 0)
regs.fit(X_train, y_train)

# + _cell_guid="c5a08959-fb7e-473a-875b-38561cb869fb" _uuid="cb3079fb6def5c187df4fecc3b6e5b7baf721a2d"
y_dtr = regs.predict(X_test)

# + _cell_guid="cbe1d2eb-4819-476d-9852-5d35f8c0ea83" _uuid="d466e07bfc84fb5925f8ad9b3c491e9cd880ede8"
regs.score(X_test, y_test)
print('accuracy of training set: {:.4f}'.format(regs.score(X_train,y_train)))
print('accuaracy of test set: {:.4f}'.format(regs.score(X_test, y_test)))

# + _cell_guid="691c85c1-fd2e-4cb4-9a0f-9de4e0ba6e42" _uuid="506dbf8d00f317b986b82f183f33878633c82d89"
print('accuracy_score of decision tree regression: ', accuracy_score( y_dtr , y_test))
print('confusion_matrix of decision tree regression: ', confusion_matrix(y_dtr, y_test))
print('precision_score of decision tree regression: ', precision_score( y_dtr, y_test))
print('recall_score of decision tree regression: ', recall_score( y_dtr, y_test))
print('precision_recall_curve: ', precision_recall_curve(y_dtr, y_test))

# + [markdown] _cell_guid="4484726e-8fcc-409c-afeb-96f41437746c" _uuid="ac53c589c6293d42f2e899c88d83f32728d7601e"
# **Logistic Regression**

# + _cell_guid="b0178990-2132-47b0-892e-2953b54d642f" _uuid="a247dd9ff9c1b62a94e9ecf20bb73b19842a9945"
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression(random_state = 0)
logreg.fit(X_train, y_train)

# + _cell_guid="3f4c8cc0-3379-4e13-8636-1bf06c141a1c" _uuid="4c9e8416220d9d71389a6126b3ff4d3403823bd3"
y_lr = logreg.predict(X_test)

# + _cell_guid="9dc3440d-670f-47de-b3bb-a0f5192a9092" _uuid="5ffa546443aec12702c77f024d8fefa6c2c9c5f4"
logreg.score(X_test, y_test)
print('accuracy of training set: {:.4f}'.format(logreg.score(X_train,y_train)))
print('accuaracy of test set: {:.4f}'.format(logreg.score(X_test, y_test)))

# + _cell_guid="b448c1fe-8188-4db1-9ded-484c2f39854e" _uuid="546dd7a7ce47865b3973ec0025f2912d0a3d8709"
print('accuracy_score of logistic regression : ', accuracy_score(y_test, y_lr))
print('confusion_matrix of logistic regression: ', confusion_matrix(y_test, y_lr))
print('precision_score of logistic regression: ', precision_score(y_test, y_lr))
print('recall_score of logistic regression: ', recall_score(y_test, y_lr))
print('precision_recall_curve: ', precision_recall_curve(y_test, y_lr))

# + _cell_guid="c5b9087b-0d6a-43e7-b7b1-6957ac0066aa" _uuid="b6bcfdc6e204c98d0d445914a5322114f212b4ae"
logreg100 = LogisticRegression(random_state = 1000, C =100)
logreg100.fit(X_train, y_train)

# + _cell_guid="0328b73e-281e-49d3-a107-904c511c5d7a" _uuid="4d259ec96a8099644c72c18eb064912924504a60"
y_lr100 = logreg100.predict(X_test)

# + _cell_guid="108996a9-b6be-4a5b-9759-8735ab01db30" _uuid="c7ed06a66c79d93e0f0552fcc4a6643e98809373"
logreg100.score(X_test, y_test)
print('accuracy of training set: {:.4f}'.format(logreg100.score(X_train,y_train)))
print('accuaracy of test set: {:.4f}'.format(logreg100.score(X_test, y_test)))

# + _cell_guid="338ba66f-f58d-40ef-b9eb-f7c712c94c57" _uuid="13b12f86804f5716ad5147295c0c21a3f07e2070"
logreg01 = LogisticRegression(random_state = 0, C =0.001)
logreg01.fit(X_train, y_train)

# + _cell_guid="05ec4882-0c3a-4c85-8d86-61618d5738b9" _uuid="15570fe288712a176d7b321b8cd266bd00cc224e"
y_p01 = logreg01.predict(X_test)

# + _cell_guid="a5bd1e32-196b-49bc-85db-de19eb765a59" _uuid="1207c785b6060f60d73ed525f5dfcdd43ccc0842"
logreg01.score(X_test, y_test)
print('accuracy of training set: {:.4f}'.format(logreg01.score(X_train,y_train)))
print('accuaracy of test set: {:.4f}'.format(logreg01.score(X_test, y_test)))

# + [markdown] _cell_guid="69b84cc0-ff95-4397-a1db-910a0eede21b" _uuid="13d5c29f6e00f8eba64f523e55bb654a5bd27590"
# **Decision Tree Classification**

# + _cell_guid="085a6801-3fbd-4256-aeed-f6fffb4fae1a" _uuid="d9157b5ec40293a9b6b4385d400efdced3d2ebd5"
from sklearn.tree import DecisionTreeClassifier
classifier = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
classifier.fit(X_train, y_train)

# + _cell_guid="91665364-446a-485b-8fef-82413d3b009c" _uuid="298179a928374a1cce41f8b4d7131bf9f7b3d5b6"
y_dtc = classifier.predict(X_test)

# + _cell_guid="b5cd0832-5012-45d3-ac52-f3ef822a32e2" _uuid="2135b186e232249dadd8edab62b31b2650a36b29"
classifier.score(X_test, y_test)
print('accuracy of training set: {:.4f}'.format(classifier.score(X_train,y_train)))
print('accuaracy of test set: {:.4f}'.format(classifier.score(X_test, y_test)))

# + _cell_guid="07f38a5f-ab6d-4f98-afaa-90e6fe52c1ab" _uuid="f5f6ae69bca5559754b401099b5990d812b1d23b"
classifier = DecisionTreeClassifier(max_depth = 4, random_state = 42)
classifier.fit(X_train,y_train)
print('accuracy of training set: {:.4f}'.format(classifier.score(X_train,y_train)))
print('accuaracy of test set: {:.4f}'.format(classifier.score(X_test, y_test)))

# + _cell_guid="48256be1-c424-416e-92cb-cddf8a2b74ff" _uuid="8e23a3063c30d7ba4f04340b8a14d94a65b7297e"
print('accuracy_score of decesion tree classifier: ', accuracy_score(y_dtc, y_test))
print('confusion_matrix of decision tree classifier: ', confusion_matrix(y_dtc, y_test))
print('precision_score of decision tree classifier: ', precision_score(y_dtc, y_test))
print('recall_score of decision tree classifier: ', recall_score(y_dtc, y_test))
print('precision_recall_curve of decision tree classifier: ', precision_recall_curve(y_dtc, y_test))

# + [markdown] _cell_guid="81127b02-68ce-483a-bafd-5ee0b0a2fac2" _uuid="14ad9f2b9c2dd5626455c68481d96c045572d6ee"
# **Naive Bayes Classification**

# + _cell_guid="80346ecf-961e-4b49-843a-635868231949" _uuid="ca03290edfdfd7b448078c555743ce16fdd39435"
from sklearn.naive_bayes import GaussianNB
NBC = GaussianNB()
NBC.fit(X_train, y_train)

# + _cell_guid="fcb99663-e637-49a9-8f05-f9fb3e5879f5" _uuid="b8eb8b468a58a8d5859e21c9578fe6e5ba7dc381"
y_nb = NBC.predict(X_test)

# + _cell_guid="7845d1dd-8800-44a1-b313-a2bc9daf8895" _uuid="c7bf365c5e5e2db75d4faecee214e9bac63191c6"
NBC.score(X_test, y_test)
print('accuracy of training set: {:.4f}'.format(NBC.score(X_train,y_train)))
print('accuaracy of test set: {:.4f}'.format(NBC.score(X_test, y_test)))

# + _cell_guid="b0eb51e6-217f-4e50-98f4-7d96e9c3766f" _uuid="895dea927cae46cff52a2fdba4a8c62726e3781c"
print('accuracy_score of Naive Bayes: ', accuracy_score(y_test, y_nb))
print('confusion_matrix of Naive Bayes: ', confusion_matrix(y_test, y_nb))
print('precision_score of Naive Bayes: ', precision_score(y_test, y_nb))
print('recall_score of Naive Bayes: ', recall_score(y_test, y_nb))
print('precision_recall_curve of Naive Bayes: ', precision_recall_curve(y_test, y_nb))

StarcoderdataJupyterScriptsDedupFiltered