-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathML_models.py
More file actions
136 lines (115 loc) · 4.36 KB
/
ML_models.py
File metadata and controls
136 lines (115 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import argparse
import nltk
from sklearnex import patch_sklearn
patch_sklearn()
from nltk.corpus import stopwords
import os
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.metrics import precision_recall_fscore_support, roc_auc_score
from xgboost import XGBClassifier
from sklearn.svm import SVC
from sklearn.svm import LinearSVC
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier
from tqdm import tqdm
from dotenv import load_dotenv
load_dotenv()
# Remember to do this the first time you run the code
# >>> import nltk
# >>> nltk.download('stopwords')
sw = stopwords.words("english")
DATASET_PATH = os.getenv("DATASET_PATH")
#represent different columns of text each reprresenting different error rate, each is used for training a classifier
fields = [
'col1', 'col2'
]
df = pd.read_csv(
f"{DATASET_PATH}/train_split.csv.gz"
)
df = df.dropna()
df_test = pd.read_csv(
f"{DATASET_PATH}/test_split.csv.gz")
df_test = df_test.dropna()
y = df["Class"]
#ML models
classifiers = [
MLPClassifier(hidden_layer_sizes=(100,), max_iter=300, early_stopping=True),
MLPClassifier(hidden_layer_sizes=(100, 50, 25), max_iter=300, early_stopping=True),
SVC(kernel="linear", C=1.0, probability=True, cache_size=5000, class_weight="balanced"),
SVC(kernel="linear", C=0.5, probability=True, cache_size=5000, class_weight="balanced"),
SVC(kernel="rbf", C=1.0, probability=True, cache_size=5000, class_weight="balanced"),
SVC(kernel="rbf", C=0.5, probability=True, cache_size=5000, class_weight="balanced"),
LogisticRegression(C=0.5, class_weight="balanced"),
RandomForestClassifier(n_estimators=100),
RandomForestClassifier(n_estimators=300),
RandomForestClassifier(n_estimators=500),
RandomForestClassifier(max_depth=3, n_estimators=100),
RandomForestClassifier(max_depth=3, n_estimators=300),
RandomForestClassifier(max_depth=3, n_estimators=500),
XGBClassifier(scale_pos_weight=(sum(y == 0) / sum(y == 1)), n_jobs=-1),
XGBClassifier(
n_estimators=100, scale_pos_weight=(sum(y == 0) / sum(y == 1)), n_jobs=-1
),
XGBClassifier(
n_estimators=300, scale_pos_weight=(sum(y == 0) / sum(y == 1)), n_jobs=-1
),
XGBClassifier(
n_estimators=500, scale_pos_weight=(sum(y == 0) / sum(y == 1)), n_jobs=-1
),
XGBClassifier(max_depth=3, scale_pos_weight=(sum(y == 0) / sum(y == 1)), n_jobs=-1),
XGBClassifier(
max_depth=3,
n_estimators=100,
scale_pos_weight=(sum(y == 0) / sum(y == 1)),
n_jobs=-1,
),
XGBClassifier(
max_depth=3,
n_estimators=300,
scale_pos_weight=(sum(y == 0) / sum(y == 1)),
n_jobs=-1,
),
XGBClassifier(
max_depth=3,
n_estimators=500,
scale_pos_weight=(sum(y == 0) / sum(y == 1)),
n_jobs=-1,
),
]
for field in tqdm(fields):
print("Processing field:", field)
# uni_vectorizer = CountVectorizer(ngram_range=(1,1), \
uni_vectorizer = TfidfVectorizer(
ngram_range=(1, 1),
stop_words=sw,
token_pattern=r"(?u)\b[a-zA-Z]+\b",
lowercase=True, # max_features=5000, \
min_df=5,
)
# bi_vectorizer = CountVectorizer(ngram_range=(1,2), \
bi_vectorizer = TfidfVectorizer(
ngram_range=(1, 2),
stop_words=sw,
token_pattern=r"(?u)\b[a-zA-Z]+\b",
lowercase=True, # max_features=5000, \
min_df=5,
)
vectorizers = [("uni", uni_vectorizer), ("bi", bi_vectorizer)]
for c in tqdm(classifiers):
print("Classifier: ", c, flush=True)
for vector_name, vectorizer in vectorizers:
X = vectorizer.fit_transform(df[field])
c.fit(X, y)
X_test = vectorizer.transform(df_test[field])
y_test = df_test["Class"]
y_pred = c.predict(X_test)
y_pred_proba = c.predict_proba(X_test)
# print("File name:", args.input_file_name)
print("Classifier: ", c)
print("Field name: ", field)
print("Vector name: ", vector_name)
print("Vocabulary size: ", len(vectorizer.vocabulary_))
print("Result: ", precision_recall_fscore_support(y_test, y_pred))
print("AUC ROC:", roc_auc_score(y_test, y_pred_proba[:, 1]))