This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df_card = pd.read_csv("https://cainvas-static.s3.amazonaws.com/media/user_data/hrithikgupta/creditcard.csv") | |
df_card.head(10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print(df_card.shape) | |
print(df_card.size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df_card.info() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df_card.drop(columns = ['Time'], inplace= True) | |
# Standardizing the amount column | |
from sklearn import preprocessing | |
scaler = preprocessing.StandardScaler() | |
#standard scaling | |
df_card['Stand_Amount'] = scaler.fit_transform(df_card['Amount'].values.reshape (-1,1)) | |
#removing Amount | |
df = df_card.drop("Amount", axis=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sns.countplot(x="Class", data=df_card) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import imblearn | |
from imblearn.under_sampling import RandomUnderSampler | |
undersample = RandomUnderSampler(sampling_strategy=0.5) | |
cols = df.columns.tolist() | |
cols = [c for c in cols if c not in ["Class"]] | |
target = "Class" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define X and Y | |
X = df[cols] | |
Y = df[target] | |
# Undersampling | |
X_sample, Y_sample = undersample.fit_resample(X, Y) | |
test = pd.DataFrame(Y_sample, columns = ['Class']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#visualizing undersampling results | |
fig, axs = plt.subplots(ncols=2, figsize=(13,4.5)) | |
sns.countplot(x="Class", data=df, ax=axs[0]) | |
sns.countplot(x="Class", data=test, ax=axs[1]) | |
fig.suptitle("Class repartition before and after undersampling") | |
a1=fig.axes[0] | |
a1.set_title("Before") | |
a2=fig.axes[1] | |
a2.set_title("After") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.model_selection import train_test_split | |
X_train, X_test, y_train, y_test = train_test_split(X_sample, Y_sample, test_size=0.3, random_state=1) | |
print(len(X_train)) | |
print(len(y_train)) | |
print(len(X_test)) | |
print(len(y_test)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
from tensorflow import keras | |
from tensorflow.keras.models import Sequential | |
from tensorflow.keras.layers import Dropout | |
from tensorflow.keras.layers import Dense | |
from tensorflow.keras import layers | |
from tensorflow.keras import regularizers | |
from sklearn import metrics | |
model = Sequential() |