<jupyter_start><jupyter_text>Split the data into train and test sets<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(data['text_parsed'], 
                                                    data['money_laundering'], 
                                                    test_size=0.25, 
                                                    random_state=42)
ngram_range = (1,2)
min_df = 0
max_df = 1.
max_features = 300<jupyter_output><empty_output><jupyter_text>Text must be vectorized<jupyter_code>tfidf = TfidfVectorizer(encoding='utf-8',
                        ngram_range=ngram_range,
                        stop_words=None,
                        lowercase=False,
                        max_df=max_df,
                        min_df=min_df,
                        max_features=max_features,
                        norm='l2',
                        sublinear_tf=True)
                        
features_train = tfidf.fit_transform(X_train).toarray()
labels_train = y_train
print(features_train)

features_test = tfidf.transform(X_test).toarray()
labels_test = y_test
print(features_test.shape)<jupyter_output>[[0.         0.09434589 0.         ... 0.06146513 0.06787635 0.        ]
 [0.08409251 0.         0.         ... 0.11849634 0.06235373 0.        ]
 [0.         0.         0.         ... 0.         0.         0.        ]
 ...
 [0.         0.         0.31070768 ... 0.17533924 0.         0.        ]
 [0.         0.         0.         ... 0.         0.         0.        ]
 [0.         0.         0.         ... 0.         0.         0.        ]]
(50, 300)<jupyter_text>Building Models  Random Forest<jupyter_code>from sklearn.ensemble import RandomForestClassifier
model_RF  = RandomForestClassifier(random_state=42)
model_RF.fit(features_train, labels_train)
RF_predictions = model_RF.predict(features_test)
print('Accuracy: ', accuracy_score(labels_test, RF_predictions))
print(classification_report(labels_test, RF_predictions))
warnings.filterwarnings("ignore", category=DeprecationWarning)
model_RF.get_params()
from sklearn.model_selection import GridSearchCV
n_estimators = [100, 300, 500]
max_depth = [5, 8, 15, 20]
min_samples_split = [2, 5, 10, 15, 50]
min_samples_leaf = [1, 2, 5, 10] 

RF_params = dict(n_estimators = n_estimators, max_depth = max_depth,  
              min_samples_split = min_samples_split, 
             min_samples_leaf = min_samples_leaf)

grid_RF = GridSearchCV(model_RF, RF_params, cv = 5, verbose = 1, 
                      n_jobs = -1)
best_RF = grid_RF.fit(features_train, labels_train)
warnings.filterwarnings("ignore", category=DeprecationWarning)
best_RF.best_params_<jupyter_output><empty_output><jupyter_text>Final Random Forest Model<jupyter_code>from sklearn.ensemble import RandomForestClassifier
warnings.filterwarnings("ignore", category=DeprecationWarning)
final_RF  = RandomForestClassifier(random_state=42,
                                 max_depth= 5,
                                 min_samples_leaf= 2,
                                 min_samples_split= 50,
                                 n_estimators= 300)

final_RF.fit(features_train, labels_train)
final_RF_predictions = final_RF.predict(features_test)
print('Accuracy: ', accuracy_score(labels_test, final_RF_predictions))
print(classification_report(labels_test, final_RF_predictions))<jupyter_output>Accuracy:  0.88
              precision    recall  f1-score   support

           0       0.93      0.93      0.93        40
           1       0.70      0.70      0.70        10

   micro avg       0.88      0.88      0.88        50
   macro avg       0.81      0.81      0.81        50
weighted avg       0.88      0.88      0.88        50<jupyter_text>Logistic Regression Model<jupyter_code>model_LR = LogisticRegression(solver='liblinear')
model_LR.fit(features_train, labels_train)
LR_predictions = model_LR.predict(features_test)
print('Accuracy: ', accuracy_score(labels_test, LR_predictions))
print(classification_report(labels_test, LR_predictions))
model_LR.get_params()<jupyter_output>Accuracy:  0.84
              precision    recall  f1-score   support

           0       0.88      0.93      0.90        40
           1       0.62      0.50      0.56        10

   micro avg       0.84      0.84      0.84        50
   macro avg       0.75      0.71      0.73        50
weighted avg       0.83      0.84      0.83        50<jupyter_text>Logistic Regression Tuning<jupyter_code>LR_params = {'C': [0.1,0.001,1],
              'penalty' : ['l1', 'l2'],
              'solver': ['liblinear']}
grid_LR = GridSearchCV(model_LR, LR_params, cv = 5, verbose = 1)

best_LR = grid_LR.fit(features_train, labels_train)
best_LR.best_params_<jupyter_output>Fitting 5 folds for each of 6 candidates, totalling 30 fits<jupyter_text>Final Logistic Regression Model<jupyter_code>final_LR = LogisticRegression(C=1, penalty='l1', solver='liblinear')
final_LR.fit(features_train, labels_train)
final_LR_predictions = final_LR.predict(features_test)
print('Accuracy: ', accuracy_score(labels_test, final_LR_predictions))
print(classification_report(labels_test, final_LR_predictions))<jupyter_output>Accuracy:  0.84
              precision    recall  f1-score   support

           0       0.88      0.93      0.90        40
           1       0.62      0.50      0.56        10

   micro avg       0.84      0.84      0.84        50
   macro avg       0.75      0.71      0.73        50
weighted avg       0.83      0.84      0.83        50<jupyter_text>KNeighborsClassifier Model<jupyter_code>model_KNN = KNeighborsClassifier()
model_KNN.fit(features_train, labels_train)
KNN_predictions = model_KNN.predict(features_test)
print('Accuracy: ', accuracy_score(labels_test, KNN_predictions))
print(classification_report(labels_test, KNN_predictions))
model_KNN.get_params()<jupyter_output><empty_output><jupyter_text>KNeighborsClassifier Tuning<jupyter_code>from sklearn.model_selection import GridSearchCV
params_KNN = {'n_neighbors': [1, 2, 3, 4, 5, 6, 7], 
              'p': [1, 2, 5]}


grid_KNN = GridSearchCV(model_KNN, params_KNN, cv = 5, verbose = 1, 
                      n_jobs = -1)
best_KNN = grid_KNN.fit(features_train, labels_train)
best_KNN.best_params_<jupyter_output><empty_output><jupyter_text>Final KNeighbors Model<jupyter_code>final_model_KNN = KNeighborsClassifier(n_neighbors=3,p=1)
final_model_KNN.fit(features_train, labels_train)
KNN_predictions = final_model_KNN.predict(features_test)
print('Accuracy: ', accuracy_score(labels_test, KNN_predictions))
print(classification_report(labels_test, KNN_predictions))<jupyter_output>Accuracy:  0.86
              precision    recall  f1-score   support

           0       0.90      0.93      0.91        40
           1       0.67      0.60      0.63        10

   micro avg       0.86      0.86      0.86        50
   macro avg       0.78      0.76      0.77        50
weighted avg       0.86      0.86      0.86        50<jupyter_text>Decision Tree<jupyter_code>model_DT = DecisionTreeClassifier(random_state=42)
model_DT.fit(features_train, labels_train)
DT_predictions = model_DT.predict(features_test)
print('Accuracy: ', accuracy_score(labels_test, DT_predictions))
print(classification_report(labels_test, DT_predictions))
model_DT.get_params()<jupyter_output>Accuracy:  0.84
              precision    recall  f1-score   support

           0       0.88      0.93      0.90        40
           1       0.62      0.50      0.56        10

   micro avg       0.84      0.84      0.84        50
   macro avg       0.75      0.71      0.73        50
weighted avg       0.83      0.84      0.83        50<jupyter_text>Decision Tree Tuning<jupyter_code>from sklearn.model_selection import GridSearchCV,RandomizedSearchCV

min_samples_leaf = [1, 3, 8]
criterion = ['gini', 'entropy']
max_depth = [3,None]



DT_params = dict(max_depth=max_depth,
                      criterion=criterion,
                      min_samples_leaf=min_samples_leaf)

model_DT  = DecisionTreeClassifier(random_state=42)
grid_DT = RandomizedSearchCV(model_DT, DT_params, cv = 5, verbose = 1)
best_DT = grid_DT.fit(features_train, labels_train)
best_DT.best_params_<jupyter_output><empty_output><jupyter_text>Final Desicion Tree Model<jupyter_code>final_model_DT = DecisionTreeClassifier(random_state=42,
                                        criterion= 'gini', 
                                        max_depth= 3,
                                        min_samples_leaf=1)
final_model_DT.fit(features_train, labels_train)
final_DT_predictions = final_model_DT.predict(features_test)
print('Accuracy: ', accuracy_score(labels_test, final_DT_predictions))
print(classification_report(labels_test, final_DT_predictions))<jupyter_output>Accuracy:  0.84
              precision    recall  f1-score   support

           0       0.88      0.93      0.90        40
           1       0.62      0.50      0.56        10

   micro avg       0.84      0.84      0.84        50
   macro avg       0.75      0.71      0.73        50
weighted avg       0.83      0.84      0.83        50<jupyter_text>GaussianNB<jupyter_code>model_GNB = GaussianNB()
model_GNB.fit(features_train, labels_train)
model_GNB_predictions = model_GNB.predict(features_test)
print('Accuracy: ', accuracy_score(labels_test, model_GNB_predictions))
print(classification_report(labels_test, model_GNB_predictions))<jupyter_output>Accuracy:  0.84
              precision    recall  f1-score   support

           0       0.92      0.88      0.90        40
           1       0.58      0.70      0.64        10

   micro avg       0.84      0.84      0.84        50
   macro avg       0.75      0.79      0.77        50
weighted avg       0.85      0.84      0.85        50<jupyter_text>Tuning GaussianNB model<jupyter_code>from sklearn.model_selection import GridSearchCV
params_NB = {'var_smoothing': np.logspace(0,-9, num=100)}

grid_NB = GridSearchCV(model_GNB, params_NB, cv = 5, verbose = 1, 
                      n_jobs = -1)

bestNB = grid_NB.fit(features_train, labels_train)
bestNB.best_params_<jupyter_output><empty_output><jupyter_text>Final GNB model<jupyter_code>final_model_GNB = GaussianNB(var_smoothing=0.01)
final_model_GNB.fit(features_train, labels_train)
final_GNB_predictions = final_model_GNB.predict(features_test)
print('Accuracy: ', accuracy_score(labels_test, final_GNB_predictions))
print(classification_report(labels_test, final_GNB_predictions))<jupyter_output>Accuracy:  0.88
              precision    recall  f1-score   support

           0       0.95      0.90      0.92        40
           1       0.67      0.80      0.73        10

   micro avg       0.88      0.88      0.88        50
   macro avg       0.81      0.85      0.83        50
weighted avg       0.89      0.88      0.88        50
StarcoderdataJupyterStructuredCleanDedup