<jupyter_start><jupyter_text>cross validation like KFOLD and Hyper parameter Tuning<jupyter_code>### independent and dependent features
X = df.drop("Class",axis=1)
y = df.Class
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
from sklearn.model_selection import KFold
import numpy as np
from sklearn.model_selection import GridSearchCV
log_class = LogisticRegression()
grid={'C':10.0 **np.arange(-2,3), 'penalty':['l1','l2']}
cv = KFold(n_splits=5,random_state=None,shuffle=False)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train,y_test=train_test_split(X,y, train_size=0.7)
clf = GridSearchCV(log_class,grid,cv=cv, n_jobs=-1, scoring='f1_macro')
clf.fit(X_train,y_train)
y_pred=clf.predict(X_test)
print(confusion_matrix(y_test,y_pred))
print(accuracy_score(y_test,y_pred))
print(classification_report(y_test,y_pred))
class_weight=dict({0:1,1:100})
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(class_weight=class_weight)
classifier.fit(X_train,y_train)
y_pred=classifier.predict(X_test)
print(confusion_matrix(y_test,y_pred))
print(accuracy_score(y_test,y_pred))
print(classification_report(y_test,y_pred))<jupyter_output>[[85292     7]
 [   39   105]]
0.9994616293903538
              precision    recall  f1-score   support

           0       1.00      1.00      1.00     85299
           1       0.94      0.73      0.82       144

    accuracy                           1.00     85443
   macro avg       0.97      0.86      0.91     85443
weighted avg       1.00      1.00      1.00     85443<jupyter_text>Under Sampling<jupyter_code>from collections import Counter
from imblearn.under_sampling import NearMiss
ns=NearMiss(0.8)
X_train_ns,y_train_ns=ns.fit_sample(X_train,y_train)
print("The number of classes before fit {}".format(Counter(y_train)))
print("The number of classes after fit {}".format(Counter(y_train_ns)))
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier()
classifier.fit(X_train_ns,y_train_ns)
y_pred=classifier.predict(X_test)
print(confusion_matrix(y_test,y_pred))
print(accuracy_score(y_test,y_pred))
print(classification_report(y_test,y_pred))<jupyter_output>[[60277 25022]
 [   10   134]]
0.7070327586812261
              precision    recall  f1-score   support

           0       1.00      0.71      0.83     85299
           1       0.01      0.93      0.01       144

    accuracy                           0.71     85443
   macro avg       0.50      0.82      0.42     85443
weighted avg       1.00      0.71      0.83     85443<jupyter_text>Over Sampling<jupyter_code>from imblearn.over_sampling import RandomOverSampler
os=RandomOverSampler(0.5)
X_train_ns,y_train_ns=os.fit_sample(X_train,y_train)
print("The number of classes before fit {}".format(Counter(y_train)))
print("The number of classes after fit {}".format(Counter(y_train_ns)))
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier()
classifier.fit(X_train_ns,y_train_ns)
y_pred=classifier.predict(X_test)
print(confusion_matrix(y_test,y_pred))
print(accuracy_score(y_test,y_pred))
print(classification_report(y_test,y_pred))<jupyter_output>[[85288    11]
 [   32   112]]
0.9994967405170698
              precision    recall  f1-score   support

           0       1.00      1.00      1.00     85299
           1       0.91      0.78      0.84       144

    accuracy                           1.00     85443
   macro avg       0.96      0.89      0.92     85443
weighted avg       1.00      1.00      1.00     85443<jupyter_text>SmoteTomek<jupyter_code>from imblearn.combine import SMOTETomek
os=SMOTETomek(0.5)
X_train_ns,y_train_ns=os.fit_sample(X_train,y_train)
print("The number of classes before fit {}".format(Counter(y_train)))
print("The number of classes after fit {}".format(Counter(y_train_ns)))
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier()
classifier.fit(X_train_ns,y_train_ns)
y_pred=classifier.predict(X_test)
print(confusion_matrix(y_test,y_pred))
print(accuracy_score(y_test,y_pred))
print(classification_report(y_test,y_pred))<jupyter_output>[[85271    28]
 [   23   121]]
0.999403110845827
              precision    recall  f1-score   support

           0       1.00      1.00      1.00     85299
           1       0.81      0.84      0.83       144

    accuracy                           1.00     85443
   macro avg       0.91      0.92      0.91     85443
weighted avg       1.00      1.00      1.00     85443<jupyter_text>Ensemble Techniques<jupyter_code>from imblearn.ensemble import EasyEnsembleClassifier
easy = EasyEnsembleClassifier()
easy.fit(X_train,y_train)
y_pred=easy.predict(X_test)
print(confusion_matrix(y_test,y_pred))
print(accuracy_score(y_test,y_pred))
print(classification_report(y_test,y_pred))<jupyter_output>[[82629  2670]
 [    9   135]]
0.9686457638425617
              precision    recall  f1-score   support

           0       1.00      0.97      0.98     85299
           1       0.05      0.94      0.09       144

    accuracy                           0.97     85443
   macro avg       0.52      0.95      0.54     85443
weighted avg       1.00      0.97      0.98     85443
StarcoderdataJupyterStructuredCleanDedup