<jupyter_start><jupyter_text>Since the classes are highly imbalanced we have to perform some sampling methods to balance the data so that we could get the best result from our model.<jupyter_code>leg_df = df[df.Class == 0]
fraud_df = df[df.Class == 1]

no_of_samples = round(leg_df.shape[0] * 0.05)
no_of_samples

from imblearn.over_sampling import RandomOverSampler
from sklearn.utils import resample

leg_df_2 = resample(leg_df, n_samples=no_of_samples, random_state=15)
# leg_df_2.describe()
df_sampled = pd.concat([leg_df_2,fraud_df],axis=0)

x_sampled = df_sampled.drop('Class', axis=1)
y_sampled = df_sampled.Class

ros = RandomOverSampler(random_state=42)

x,y = ros.fit_resample(x_sampled,y_sampled)
y.value_counts()
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,shuffle=True,test_size=0.2)
from sklearn.linear_model import LogisticRegression
lr=LogisticRegression(max_iter=100,random_state=10)
lr.fit(x_train,y_train)
y_pred=lr.predict(x_test)
lr.score(x_test,y_test)
from sklearn.metrics import confusion_matrix,f1_score,recall_score,precision_score
sns.heatmap(confusion_matrix(y_test,y_pred),annot=True)

from sklearn.metrics import f1_score,recall_score,precision_score
def print_scores(y_test,y_pred):
  print(f'The precision score is {precision_score(y_test,y_pred)}')
  print(f'The recall score is {recall_score(y_test,y_pred)}')
  print(f'The f1 score is {f1_score(y_test,y_pred)}')
print_scores(y_test,y_pred)
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from xgboost import XGBClassifier
from sklearn.model_selection import GridSearchCV

xgb=XGBClassifier()
params={'eta':[0.1,0.01],
        'max_depth':[1,3,4],
        'max_leaf_nodes':[10,20,30],
        'objective':['binary:logistic']}

clf=GridSearchCV(xgb,params)
clf.fit(x_train,y_train)

clf.best_params_
clf.best_score_
clf1=XGBClassifier(4,0.1,max_leaf_nodes=10,objective='binary:logistic')
clf1.fit(x_train,y_train)

y_pred1=clf1.predict(x_test)
print(f'F1 score {f1_score(y_test,y_pred1)}')
print(f'Precision {precision_score(y_test,y_pred1)}')
print(f'Recall {recall_score(y_test,y_pred1)}')

sns.heatmap(confusion_matrix(y_test,y_pred1),annot=True)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup