-
Notifications
You must be signed in to change notification settings - Fork 0
/
Xgboost.py
189 lines (159 loc) · 7.41 KB
/
Xgboost.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.utils.class_weight import compute_class_weight
from sklearn.metrics import classification_report, f1_score
import xgboost as xgb
import time
from datetime import datetime
class XGBoostCybercrimeClassifier:
def __init__(self):
print("\n" + "=" * 50)
print("Initializing XGBoost Cybercrime Classifier")
print("=" * 50)
self.label_encoders = {
'category': LabelEncoder(),
'sub_category': LabelEncoder()
}
self.tfidf = TfidfVectorizer(
max_features=5000,
ngram_range=(1, 2),
strip_accents='unicode',
decode_error='replace'
)
# XGBoost parameters
self.params = {
'objective': 'multi:softprob',
'eval_metric': ['mlogloss', 'merror'],
'tree_method': 'hist',
'learning_rate': 0.05,
'max_depth': 5,
'min_child_weight': 2,
'gamma': 0.1,
'subsample': 0.8,
'colsample_bytree': 0.8,
'reg_alpha': 0.1,
'reg_lambda': 1.0,
'random_state': 42,
'n_estimators': 200
}
print("\nModel parameters:")
for param, value in self.params.items():
print(f" {param}: {value}")
self.trained_models = {}
self.class_weights = {}
def compute_class_weights(self, y, label_type):
print(f"\nComputing class weights for {label_type}...")
unique_classes = np.unique(y)
weights = compute_class_weight(
class_weight='balanced',
classes=unique_classes,
y=y
)
self.class_weights[label_type] = dict(zip(unique_classes, weights))
print(f"Found {len(unique_classes)} unique classes")
print("Class distribution:")
for cls, weight in zip(unique_classes, weights):
count = np.sum(y == cls)
print(f" Class {cls}: {count} samples (weight: {weight:.2f})")
return np.array([self.class_weights[label_type][label] for label in y])
def fit(self, X, y_category, y_subcategory):
print("\n" + "=" * 50)
print(f"Starting model training at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("=" * 50)
start_time = time.time()
print("\nGenerating TF-IDF features...")
X_tfidf = self.tfidf.fit_transform(X)
print(f"TF-IDF matrix shape: {X_tfidf.shape}")
print(f"Vocabulary size: {len(self.tfidf.vocabulary_)}")
# Train category model
print("\n" + "-" * 50)
print("Training category model...")
print("-" * 50)
y_category_encoded = self.label_encoders['category'].fit_transform(y_category)
category_weights = self.compute_class_weights(y_category_encoded, 'category')
self.trained_models['category'] = xgb.XGBClassifier(
**self.params,
num_class=len(np.unique(y_category_encoded))
)
category_start = time.time()
self.trained_models['category'].fit(
X_tfidf,
y_category_encoded,
sample_weight=category_weights,
verbose=True,
eval_set=[(X_tfidf, y_category_encoded)],
)
print(f"\nCategory model training completed in {(time.time() - category_start)/60:.1f} minutes")
# Train subcategory model
print("\n" + "-" * 50)
print("Training subcategory model...")
print("-" * 50)
y_subcategory_encoded = self.label_encoders['sub_category'].fit_transform(y_subcategory)
subcategory_weights = self.compute_class_weights(y_subcategory_encoded, 'sub_category')
self.trained_models['sub_category'] = xgb.XGBClassifier(
**self.params,
num_class=len(np.unique(y_subcategory_encoded))
)
subcategory_start = time.time()
self.trained_models['sub_category'].fit(
X_tfidf,
y_subcategory_encoded,
sample_weight=subcategory_weights,
verbose=True,
eval_set=[(X_tfidf, y_subcategory_encoded)],
)
print(f"\nSubcategory model training completed in {(time.time() - subcategory_start)/60:.1f} minutes")
total_time = time.time() - start_time
print("\n" + "=" * 50)
print(f"Total training completed at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print(f"Total training time: {total_time/60:.1f} minutes")
print("=" * 50)
return self
def predict(self, X):
print("\n" + "=" * 50)
print(f"Starting predictions at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("=" * 50)
start_time = time.time()
print("\nTransforming texts to TF-IDF features...")
X_tfidf = self.tfidf.transform(X)
print(f"Feature matrix shape: {X_tfidf.shape}")
print("\nMaking category predictions...")
category_predictions = self.trained_models['category'].predict(X_tfidf)
print("\nMaking subcategory predictions...")
subcategory_predictions = self.trained_models['sub_category'].predict(X_tfidf)
print(f"\nPredictions completed in {(time.time() - start_time)/60:.1f} minutes")
return (
self.label_encoders['category'].inverse_transform(category_predictions),
self.label_encoders['sub_category'].inverse_transform(subcategory_predictions)
)
def evaluate(self, X, y_category, y_subcategory):
print("\n" + "=" * 50)
print("Evaluating model performance")
print("=" * 50)
# Predict
pred_category, pred_subcategory = self.predict(X)
# Category metrics
print("\nCategory Classification Report:")
y_category_encoded = self.label_encoders['category'].transform(y_category)
category_f1 = f1_score(y_category_encoded, self.label_encoders['category'].transform(pred_category), average='weighted')
print(classification_report(y_category_encoded, self.label_encoders['category'].transform(pred_category)))
print(f"Category F1 Score (Weighted): {category_f1:.4f}")
# Subcategory metrics
print("\nSubcategory Classification Report:")
y_subcategory_encoded = self.label_encoders['sub_category'].transform(y_subcategory)
subcategory_f1 = f1_score(y_subcategory_encoded, self.label_encoders['sub_category'].transform(pred_subcategory), average='weighted')
print(classification_report(y_subcategory_encoded, self.label_encoders['sub_category'].transform(pred_subcategory)))
print(f"Subcategory F1 Score (Weighted): {subcategory_f1:.4f}")
return category_f1, subcategory_f1
# Example usage
if __name__ == "__main__":
print("\nLoading data...")
train_df = pd.read_csv('Train_cleaned.csv')
test_df = pd.read_csv('Test_cleaned.csv')
print(f"Training data shape: {train_df.shape}")
print(f"Test data shape: {test_df.shape}")
classifier = XGBoostCybercrimeClassifier()
classifier.fit(train_df['crimeaditionalinfo'], train_df['category'], train_df['sub_category'])
classifier.evaluate(test_df['crimeaditionalinfo'], test_df['category'], test_df['sub_category'])