import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
df = pd.read_csv('../input/creditcardfraud/creditcard.csv')
df.head()
df.info()
df.isnull().sum()
df['Class'].value_counts()
# Hasilnya unbalance
# 0 -> Normal Transaksi
# 1 -> Transaksi Tidak Normal
"""
## Separate Data
"""
legit = df[df['Class']==0]
fraud = df[df['Class']==1]
legit['Amount'].describe()
fraud['Amount'].describe()
df.groupby('Class').mean()
"""
## Deal With Unbalance Data
Menggunakan Metode Under-Sampling
Yaitu data yang lebih banyak, datanya akan disamakan dengan data yang lebih dikit
"""
legit_sample = legit.sample(492)
legit_sample.shape
new_df = pd.concat([legit_sample,fraud],axis = 0)
new_df.tail()
new_df['Class'].value_counts()
new_df.groupby('Class').mean()
"""
## Split Data Feature & Target
"""
X = new_df.drop(columns='Class')
Y = new_df['Class']
X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size=0.2, stratify = Y, random_state=48)
"""
## Train Model
"""
lr = LogisticRegression()
lr.fit(X_train,Y_train)
y_pred1 = lr.predict(X_train)
accuracy_score(y_pred1,Y_train)
y_pred2 = lr.predict(X_test)
accuracy_score(y_pred2,Y_test)