Skip to content

Instantly share code, notes, and snippets.

!ls "/content/drive/My Drive/Credit_Fraud"
data = pd.read_csv("/content/drive/My Drive/Credit_Fraud/creditcard.csv")
print(data.head(),"\n"*3)
data.shape
sns.countplot(x=data['Class'])
data["Class"].value_counts()
# Adjusting figuresize, and fontsize
plt.rcParams["figure.figsize"] = "8,6"
font = {'size': 12}
plt.rc('font', **font)
# Adding titles to the plots and axes
plt.title("Distribution of Amount over Both Classes")
plt.xlabel("Amount")
plt.ylabel("Class")
# Adjusting figuresize, and fontsize
plt.rcParams["figure.figsize"] = "8,6"
font = {'size': 12}
plt.rc('font', **font)
# Adding titles to the plots and axes
plt.title("Distribution of Time over Both Classes")
plt.xlabel("Time")
plt.ylabel("Class")
correlation = data.corr()
fig = plt.subplots(figsize=(15,15))
sns.heatmap(correlation, vmax= 1 )
X = data.drop(["Class","Time"],axis=1).values
y = data["Class"].values
X_train_Before, X_test, y_train_Before, y_test = train_test_split(
X, y, test_size = 0.3,
random_state = 42)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
for i in X_train_Before:
scaler = StandardScaler()
X_train_Before[i] = scaler.fit_transform(X_train_Before[i].values.reshape(-1,1))
X_test[i] = scaler.transform(X_test[i].values.reshape(-1,1))
borderlineSMOTE = BorderlineSMOTE(k_neighbors = 10, random_state = 42)
X_train, y_train = borderlineSMOTE.fit_resample(X_train_Before,y_train_Before)
#Random Forest Classifier
rf = RandomForestClassifier(n_estimators = 15,random_state=42)
rf.fit(X_train, y_train)
y_pred = rf.predict(X_test)
precision = precision_score(y_test, y_pred)