-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model.py
More file actions
1155 lines (944 loc) · 38.4 KB
/
Copy pathtrain_model.py
File metadata and controls
1155 lines (944 loc) · 38.4 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import LogisticRegression, SGDClassifier
from sklearn.ensemble import (
RandomForestClassifier,
GradientBoostingClassifier,
AdaBoostClassifier,
VotingClassifier,
)
from sklearn.svm import SVC, LinearSVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from sklearn.preprocessing import LabelEncoder
from sklearn.cluster import KMeans
from collections import Counter
import seaborn as sns
import matplotlib.pyplot as plt
import joblib
import warnings
import os
# ================================================================================
# This file implements a complete pipeline for training and evaluating
# Vietnamese news classification models using multiple algorithms and ensemble methods.
# Output: Trained model artifacts and evaluation reports in 'train_model_assets' folder.
# ================================================================================
warnings.filterwarnings("ignore")
# Support Vietnamese characters in matplotlib
plt.rcParams["font.family"] = "DejaVu Sans"
# ==================== ASSET FOLDER SETUP ====================
ASSET_FOLDER = "train_model_assets"
os.makedirs(ASSET_FOLDER, exist_ok=True)
print(f"Asset folder created: {ASSET_FOLDER}")
# ==================== DATA LOADING ====================
def load_cleaned_data(file_path):
"""
Load preprocessed data from CSV file.
Args:
file_path: Path to cleaned data CSV file
Returns:
DataFrame containing preprocessed data
"""
print(f"Loading data from {file_path}...")
df = pd.read_csv(file_path, encoding="utf-8")
print(f"Total articles: {len(df)}")
print(f"Number of categories: {df['category'].nunique()}")
print(f"\nDistribution by category:")
print(df["category"].value_counts())
# Display source statistics if available
if "source" in df.columns:
print(f"\nDistribution by source:")
print(df["source"].value_counts())
print(f"\nCross-tabulation (category x source):")
print(pd.crosstab(df["category"], df["source"]))
# Analyze source imbalance
print(f"\nAverage articles per source:")
source_stats = df.groupby("source")["category"].count()
print(source_stats)
print(
f"\nImbalance: Min={source_stats.min()}, Max={source_stats.max()}, Gap={source_stats.max() - source_stats.min()}"
)
return df
# ==================== DATA SPLITTING ====================
def split_data(df, test_size=0.2, val_size=0.1, random_state=42):
"""
Split data into train, validation, and test sets with stratification.
Args:
df: Input DataFrame
test_size: Proportion of test set
val_size: Proportion of validation set
random_state: Random seed for reproducibility
Returns:
Tuple of (X_train, X_val, X_test, y_train, y_val, y_test, label_encoder)
"""
print(f"\n{'='*70}")
print("DATA SPLITTING (TRAIN/VALIDATION/TEST)")
print(f"{'='*70}")
X = df["content"]
y = df["category"]
# Encode labels for XGBoost compatibility
label_encoder = LabelEncoder()
y_encoded = label_encoder.fit_transform(y)
print(f"\nLabel encoding mapping:")
for i, label in enumerate(label_encoder.classes_):
print(f" {i} -> {label}")
# Split train and temp (test + validation)
X_train, X_temp, y_train, y_temp = train_test_split(
X,
y_encoded,
test_size=(test_size + val_size),
random_state=random_state,
stratify=y_encoded,
)
# Split temp into test and validation
test_ratio = test_size / (test_size + val_size)
X_val, X_test, y_val, y_test = train_test_split(
X_temp, y_temp, test_size=test_ratio, random_state=random_state, stratify=y_temp
)
print(f"\nTrain set: {len(X_train)} articles ({len(X_train)/len(df)*100:.1f}%)")
print(f"Validation set: {len(X_val)} articles ({len(X_val)/len(df)*100:.1f}%)")
print(f"Test set: {len(X_test)} articles ({len(X_test)/len(df)*100:.1f}%)")
return X_train, X_val, X_test, y_train, y_val, y_test, label_encoder
# ==================== TEXT VECTORIZATION ====================
def vectorize_text(X_train, X_val, X_test, max_features=5000):
"""
Convert text to TF-IDF feature vectors.
Args:
X_train: Training text data
X_val: Validation text data
X_test: Test text data
max_features: Maximum number of features
Returns:
Tuple of (X_train_tfidf, X_val_tfidf, X_test_tfidf, vectorizer)
"""
print(f"\n{'='*70}")
print("TEXT VECTORIZATION (TF-IDF)")
print(f"{'='*70}")
vectorizer = TfidfVectorizer(
max_features=max_features,
ngram_range=(1, 2),
min_df=2,
max_df=0.8,
sublinear_tf=True,
)
X_train_tfidf = vectorizer.fit_transform(X_train)
X_val_tfidf = vectorizer.transform(X_val)
X_test_tfidf = vectorizer.transform(X_test)
print(f"Number of features: {X_train_tfidf.shape[1]}")
print(f"Train shape: {X_train_tfidf.shape}")
print(f"Validation shape: {X_val_tfidf.shape}")
print(f"Test shape: {X_test_tfidf.shape}")
return X_train_tfidf, X_val_tfidf, X_test_tfidf, vectorizer
# ==================== MODEL TRAINING ====================
def train_models(X_train, y_train):
"""
Train multiple classification models.
Args:
X_train: Training features
y_train: Training labels
Returns:
Dictionary of trained models
"""
print(f"\n{'='*70}")
print("MODEL TRAINING (10+ ALGORITHMS)")
print(f"{'='*70}")
models = {
"Naive Bayes": MultinomialNB(alpha=0.1),
"Logistic Regression": LogisticRegression(
max_iter=1000, C=1.0, random_state=42, solver="saga", n_jobs=-1
),
"SGD Classifier": SGDClassifier(
loss="hinge",
penalty="l2",
alpha=0.0001,
random_state=42,
max_iter=1000,
n_jobs=-1,
),
"Linear SVM": LinearSVC(C=1.0, max_iter=1000, random_state=42),
"Decision Tree": DecisionTreeClassifier(
max_depth=50, min_samples_split=10, random_state=42
),
"Random Forest": RandomForestClassifier(
n_estimators=200,
max_depth=50,
min_samples_split=5,
random_state=42,
n_jobs=-1,
),
"Gradient Boosting": GradientBoostingClassifier(
n_estimators=100, learning_rate=0.1, max_depth=5, random_state=42
),
"XGBoost": XGBClassifier(
n_estimators=100,
learning_rate=0.1,
max_depth=5,
random_state=42,
n_jobs=-1,
eval_metric="mlogloss",
),
"AdaBoost": AdaBoostClassifier(
n_estimators=100, learning_rate=1.0, random_state=42, algorithm="SAMME"
),
"SVM (RBF Kernel)": SVC(kernel="rbf", C=1.0, gamma="scale", random_state=42),
"K-Nearest Neighbors": KNeighborsClassifier(
n_neighbors=5, weights="distance", n_jobs=-1
),
}
trained_models = {}
for name, model in models.items():
print(f"\n[{name}] Training model...")
try:
model.fit(X_train, y_train)
trained_models[name] = model
print(f"[{name}] ✓ Training completed")
except Exception as e:
print(f"[{name}] ✗ Error: {e}")
return trained_models
# ==================== ENSEMBLE MODEL ====================
def train_ensemble_model(trained_models, X_train, y_train):
"""
Train ensemble model using voting classifier.
Args:
trained_models: Dictionary of trained base models
X_train: Training features
y_train: Training labels
Returns:
Trained ensemble model or None
"""
print(f"\n{'='*70}")
print("ENSEMBLE MODEL TRAINING (VOTING CLASSIFIER)")
print(f"{'='*70}")
estimators = [
("lr", trained_models.get("Logistic Regression")),
("rf", trained_models.get("Random Forest")),
("xgb", trained_models.get("XGBoost")),
]
estimators = [(name, model) for name, model in estimators if model is not None]
if len(estimators) >= 2:
ensemble = VotingClassifier(
estimators=estimators,
voting="soft",
n_jobs=-1,
)
print(f"Training ensemble model with {len(estimators)} base models...")
ensemble.fit(X_train, y_train)
print(f"✓ Ensemble model training completed")
return ensemble
else:
print("Warning: Not enough models to create ensemble")
return None
# ==================== VALIDATION EVALUATION ====================
def evaluate_on_validation(models, X_val, y_val):
"""
Evaluate models on validation set.
Args:
models: Dictionary of trained models
X_val: Validation features
y_val: Validation labels
Returns:
Dictionary of validation accuracies
"""
print(f"\n{'='*70}")
print("VALIDATION SET EVALUATION")
print(f"{'='*70}")
val_results = {}
for name, model in models.items():
try:
y_pred = model.predict(X_val)
accuracy = accuracy_score(y_val, y_pred)
val_results[name] = accuracy
print(f"[{name}] Validation Accuracy: {accuracy:.4f} ({accuracy*100:.2f}%)")
except Exception as e:
print(f"[{name}] Error during evaluation: {e}")
val_results[name] = 0.0
return val_results
# ==================== TEST SET EVALUATION ====================
def evaluate_on_test(models, X_test, y_test, df_test=None, label_encoder=None):
"""
Evaluate models on test set with detailed metrics.
Args:
models: Dictionary of trained models
X_test: Test features
y_test: Test labels
df_test: Test DataFrame (optional, for source analysis)
label_encoder: Label encoder for decoding predictions
Returns:
Dictionary of test accuracies
"""
print(f"\n{'='*70}")
print("TEST SET EVALUATION (FINAL)")
print(f"{'='*70}")
test_results = {}
for name, model in models.items():
try:
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
test_results[name] = accuracy
print(f"\n{'='*70}")
print(f"Model: {name}")
print(f"{'='*70}")
print(f"Test Accuracy: {accuracy:.4f} ({accuracy*100:.2f}%)")
print(f"\nClassification Report:")
if label_encoder:
target_names = label_encoder.classes_
print(
classification_report(
y_test, y_pred, target_names=target_names, zero_division=0
)
)
else:
print(classification_report(y_test, y_pred, zero_division=0))
# Source-specific accuracy if available
if df_test is not None and "source" in df_test.columns:
print(f"\nAccuracy by source:")
for source in df_test["source"].unique():
source_mask = (df_test["source"] == source).values
y_test_source = y_test[source_mask]
y_pred_source = y_pred[source_mask]
if len(y_test_source) > 0:
source_acc = accuracy_score(y_test_source, y_pred_source)
print(
f" {source:15s}: {source_acc:.4f} ({source_acc*100:.2f}%) - {len(y_test_source)} samples"
)
except Exception as e:
print(f"[{name}] Error during evaluation: {e}")
test_results[name] = 0.0
return test_results
# ==================== CROSS-VALIDATION ====================
def perform_cross_validation(models, X_train, y_train, cv=5):
"""
Perform k-fold cross-validation on models.
Args:
models: Dictionary of models to validate
X_train: Training features
y_train: Training labels
cv: Number of folds
Returns:
Dictionary of cross-validation scores
"""
print(f"\n{'='*70}")
print(f"CROSS-VALIDATION ({cv}-FOLD)")
print(f"{'='*70}")
cv_results = {}
for name, model in models.items():
try:
print(f"\n[{name}] Running cross-validation...")
cv_scores = cross_val_score(
model, X_train, y_train, cv=cv, scoring="accuracy", n_jobs=-1
)
cv_results[name] = cv_scores
print(f"[{name}] CV Scores: {[f'{score:.4f}' for score in cv_scores]}")
print(
f"[{name}] Mean: {cv_scores.mean():.4f} (+/- {cv_scores.std() * 2:.4f})"
)
except Exception as e:
print(f"[{name}] Error during CV: {e}")
cv_results[name] = np.array([0.0] * cv)
return cv_results
# ==================== RESULTS COMPARISON ====================
def compare_results(val_results, test_results, cv_results):
"""
Compare and rank all models based on multiple metrics.
Args:
val_results: Validation accuracies
test_results: Test accuracies
cv_results: Cross-validation scores
Returns:
Tuple of (best_model_name, comparison_dataframe)
"""
print(f"\n{'='*70}")
print("MODEL COMPARISON AND RANKING")
print(f"{'='*70}")
comparison_df = pd.DataFrame(
{
"Validation Acc": val_results,
"Test Acc": test_results,
"CV Mean": {name: scores.mean() for name, scores in cv_results.items()},
"CV Std": {name: scores.std() for name, scores in cv_results.items()},
}
)
comparison_df["Val-Test Gap"] = abs(
comparison_df["Validation Acc"] - comparison_df["Test Acc"]
)
comparison_df["Stability Score"] = 1 - comparison_df["CV Std"]
# Calculate weighted final score
comparison_df["Final Score"] = (
comparison_df["Test Acc"] * 0.5
+ comparison_df["Validation Acc"] * 0.2
+ comparison_df["CV Mean"] * 0.2
+ comparison_df["Stability Score"] * 0.1
)
comparison_df = comparison_df.sort_values("Final Score", ascending=False)
print("\nModel Ranking (Top 10):")
print(comparison_df.head(10).to_string())
best_model_name = comparison_df["Final Score"].idxmax()
best_row = comparison_df.loc[best_model_name]
print(f"\n{'='*70}")
print(f"BEST MODEL: {best_model_name}")
print(f"{'='*70}")
print(
f"Test Accuracy: {best_row['Test Acc']:.4f} ({best_row['Test Acc']*100:.2f}%)"
)
print(f"Validation Accuracy: {best_row['Validation Acc']:.4f}")
print(f"CV Mean ± Std: {best_row['CV Mean']:.4f} ± {best_row['CV Std']:.4f}")
print(f"Final Score: {best_row['Final Score']:.4f}")
print(f"{'='*70}")
gap = best_row["Val-Test Gap"]
print(f"\nOverfitting Analysis:")
print(f"Validation-Test Gap: {gap:.4f}")
if gap < 0.02:
print(f"✓ Model is stable (gap < 2%)")
elif gap < 0.05:
print(f"⚠ Model shows slight overfitting (gap < 5%)")
print(f"\nAlternative stable models (gap < 2%):")
stable_models = comparison_df[comparison_df["Val-Test Gap"] < 0.02].head(3)
if len(stable_models) > 0:
print(
stable_models[["Test Acc", "Val-Test Gap", "Final Score"]].to_string()
)
else:
print(f"✗ Model shows significant overfitting (gap >= 5%)")
print(f"\nSearching for alternative models (gap < 5%)...")
stable_models = comparison_df[comparison_df["Val-Test Gap"] < 0.05]
if len(stable_models) > 0:
alternative_best = stable_models["Final Score"].idxmax()
print(f"✓ Best alternative model: {alternative_best}")
print(stable_models.loc[alternative_best].to_string())
best_model_name = alternative_best
print(f"\nSwitched to model: {best_model_name}")
return best_model_name, comparison_df
# ==================== CONFUSION MATRIX VISUALIZATION ====================
def plot_confusion_matrix(model, X_test, y_test, model_name, label_encoder=None):
"""
Generate and save confusion matrix heatmap.
Args:
model: Trained model
X_test: Test features
y_test: Test labels
model_name: Name of the model
label_encoder: Label encoder for class names
"""
y_pred = model.predict(X_test)
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(14, 10))
if label_encoder:
labels = label_encoder.classes_
else:
labels = sorted(np.unique(y_test))
sns.heatmap(
cm,
annot=True,
fmt="d",
cmap="Blues",
xticklabels=labels,
yticklabels=labels,
cbar_kws={"label": "Count"},
)
plt.title(f"Confusion Matrix - {model_name}", fontsize=16, fontweight="bold")
plt.ylabel("True Label", fontsize=12)
plt.xlabel("Predicted Label", fontsize=12)
plt.xticks(rotation=45, ha="right")
plt.yticks(rotation=0)
plt.tight_layout()
# Use consistent filename - overwrites previous file
filename = os.path.join(ASSET_FOLDER, "confusion_matrix.png")
plt.savefig(filename, dpi=300, bbox_inches="tight")
print(f"\n✓ Confusion matrix saved: {filename}")
plt.close()
# ==================== COMPARISON CHART ====================
def plot_comparison(comparison_df):
"""
Generate and save model comparison bar chart.
Args:
comparison_df: DataFrame containing comparison metrics
"""
top_df = comparison_df.head(10)
fig, ax = plt.subplots(figsize=(14, 8))
x = np.arange(len(top_df))
width = 0.25
ax.bar(
x - width, top_df["Validation Acc"], width, label="Validation", color="skyblue"
)
ax.bar(x, top_df["Test Acc"], width, label="Test", color="orange")
ax.bar(x + width, top_df["CV Mean"], width, label="CV Mean", color="green")
ax.set_xlabel("Models", fontsize=12)
ax.set_ylabel("Accuracy", fontsize=12)
ax.set_title("Top 10 Model Performance Comparison", fontsize=14, fontweight="bold")
ax.set_xticks(x)
ax.set_xticklabels(top_df.index, rotation=45, ha="right")
ax.legend()
ax.grid(axis="y", alpha=0.3)
plt.tight_layout()
# Use consistent filename - overwrites previous file
filename = os.path.join(ASSET_FOLDER, "model_comparison.png")
plt.savefig(filename, dpi=300, bbox_inches="tight")
print(f"✓ Comparison chart saved: {filename}")
plt.close()
# ==================== MODEL PERSISTENCE ====================
def save_model(model, vectorizer, model_name, label_encoder):
"""
Save trained model, vectorizer, and label encoder to disk.
Args:
model: Trained model
vectorizer: TF-IDF vectorizer
model_name: Name of the model
label_encoder: Label encoder
"""
# Use consistent filenames - overwrites previous files
model_file = os.path.join(ASSET_FOLDER, "best_model.pkl")
vectorizer_file = os.path.join(ASSET_FOLDER, "tfidf_vectorizer.pkl")
label_encoder_file = os.path.join(ASSET_FOLDER, "label_encoder.pkl")
model_info_file = os.path.join(ASSET_FOLDER, "model_info.txt")
joblib.dump(model, model_file)
joblib.dump(vectorizer, vectorizer_file)
joblib.dump(label_encoder, label_encoder_file)
# Save model information to text file
with open(model_info_file, "w", encoding="utf-8") as f:
f.write(f"Best Model Information\n")
f.write(f"=" * 70 + "\n\n")
f.write(f"Model Type: {model_name}\n")
f.write(f"Model Class: {type(model).__name__}\n")
f.write(f"Saved on: {pd.Timestamp.now()}\n")
print(f"\nModel artifacts saved:")
print(f"✓ Model: {model_file} ({model_name})")
print(f"✓ Vectorizer: {vectorizer_file}")
print(f"✓ Label Encoder: {label_encoder_file}")
print(f"✓ Model Info: {model_info_file}")
# ==================== PREDICTION TEST ====================
def test_prediction(model, vectorizer, sample_texts, label_encoder):
"""
Test model predictions on sample texts.
Args:
model: Trained model
vectorizer: TF-IDF vectorizer
sample_texts: List of sample texts to predict
label_encoder: Label encoder for decoding predictions
"""
print(f"\n{'='*70}")
print("SAMPLE PREDICTION TEST")
print(f"{'='*70}")
for i, text in enumerate(sample_texts, 1):
text_tfidf = vectorizer.transform([text])
prediction_encoded = model.predict(text_tfidf)[0]
prediction = label_encoder.inverse_transform([prediction_encoded])[0]
print(f"\nSample {i}:")
print(f"Text: {text[:100]}...")
print(f"Predicted Category: {prediction}")
# ==================== SOURCE PERFORMANCE ANALYSIS ====================
def analyze_source_performance(
model, X_test, y_test, df_test, model_name, label_encoder
):
"""
Detailed performance analysis by news source.
Args:
model: Trained model
X_test: Test features
y_test: Test labels
df_test: Test DataFrame with source information
model_name: Name of the model
label_encoder: Label encoder
"""
print(f"\n{'='*70}")
print(f"DETAILED SOURCE ANALYSIS ({model_name})")
print(f"{'='*70}\n")
y_pred = model.predict(X_test)
y_test_decoded = label_encoder.inverse_transform(y_test)
y_pred_decoded = label_encoder.inverse_transform(y_pred)
results_df = pd.DataFrame(
{
"true_label": y_test_decoded,
"predicted_label": y_pred_decoded,
"source": df_test["source"].values,
}
)
for source in df_test["source"].unique():
source_data = results_df[results_df["source"] == source]
accuracy = accuracy_score(
source_data["true_label"], source_data["predicted_label"]
)
print(f"Source: {source.upper()}")
print(f" Sample count: {len(source_data)} articles")
print(f" Accuracy: {accuracy:.4f} ({accuracy*100:.2f}%)")
print(f" Classification Report:")
print(
classification_report(
source_data["true_label"],
source_data["predicted_label"],
zero_division=0,
)
)
print()
# Calculate source accuracies
source_accuracies = {}
for source in df_test["source"].unique():
source_data = results_df[results_df["source"] == source]
acc = accuracy_score(source_data["true_label"], source_data["predicted_label"])
source_accuracies[source] = acc
print(f"Accuracy comparison across sources:")
for source, acc in sorted(
source_accuracies.items(), key=lambda x: x[1], reverse=True
):
print(f" {source:15s}: {acc:.4f} ({acc*100:.2f}%)")
fig, ax = plt.subplots(figsize=(12, 7))
sources = list(source_accuracies.keys())
accuracies = list(source_accuracies.values())
colors = plt.cm.Set3(range(len(sources)))
bars = ax.bar(sources, accuracies, color=colors, edgecolor="black", linewidth=1.2)
ax.set_xlabel("News Source", fontsize=13, fontweight="bold")
ax.set_ylabel("Accuracy", fontsize=13, fontweight="bold")
ax.set_title(
f"Accuracy by News Source - {model_name}",
fontsize=15,
fontweight="bold",
pad=20,
)
ax.set_ylim([0, 1.08])
ax.yaxis.set_major_formatter(plt.FuncFormatter(lambda y, _: f"{y:.0%}"))
for i, (bar, v) in enumerate(zip(bars, accuracies)):
if v >= 0.98:
label_y = v - 0.04
va = "top"
color = "white"
weight = "bold"
else:
label_y = v + 0.02
va = "bottom"
color = "black"
weight = "bold"
ax.text(
i,
label_y,
f"{v:.2%}",
ha="center",
va=va,
fontsize=11,
fontweight=weight,
color=color,
)
ax.grid(axis="y", alpha=0.3, linestyle="--", linewidth=0.7)
ax.set_axisbelow(True)
if len(sources) > 6:
plt.xticks(rotation=45, ha="right")
plt.tight_layout()
filename = os.path.join(ASSET_FOLDER, "accuracy_by_source.png")
plt.savefig(filename, dpi=300, bbox_inches="tight")
print(f"\n✓ Source analysis chart saved: {filename}")
plt.close()
# ==================== K-MEANS CLUSTERING ANALYSIS ====================
def perform_kmeans_clustering(
X_tfidf, vectorizer, n_clusters, label_encoder=None, y_true=None
):
"""
Perform K-Means clustering to discover natural data structure.
This unsupervised analysis helps understand if topics naturally separate
without using category labels.
Args:
X_tfidf: TF-IDF feature matrix
vectorizer: TF-IDF vectorizer (for feature names)
n_clusters: Number of clusters (typically equal to number of categories)
label_encoder: Label encoder (optional, for cluster-category mapping)
y_true: True labels (optional, for purity analysis)
Returns:
Tuple of (kmeans_model, cluster_labels, cluster_keywords)
"""
print(f"\n{'='*70}")
print(f"K-MEANS CLUSTERING ANALYSIS (k={n_clusters})")
print(f"{'='*70}")
print("Discovering natural topic structure in the data...")
# Train K-Means model
print(f"\nTraining K-Means with {n_clusters} clusters...")
kmeans = KMeans(
n_clusters=n_clusters, random_state=42, max_iter=300, n_init=10, verbose=0
)
cluster_labels = kmeans.fit_predict(X_tfidf)
print(f"✓ K-Means training completed")
# Get cluster statistics
print(f"\nCluster distribution:")
cluster_counts = Counter(cluster_labels)
for cluster_id in sorted(cluster_counts.keys()):
count = cluster_counts[cluster_id]
percentage = (count / len(cluster_labels)) * 100
print(f" Cluster {cluster_id}: {count} samples ({percentage:.2f}%)")
# Extract top keywords for each cluster
print(f"\n{'='*70}")
print("TOP KEYWORDS FOR EACH CLUSTER")
print(f"{'='*70}")
feature_names = vectorizer.get_feature_names_out()
cluster_centers = kmeans.cluster_centers_
cluster_keywords = {}
for cluster_id in range(n_clusters):
# Get indices of top features for this cluster
center = cluster_centers[cluster_id]
top_indices = center.argsort()[-15:][::-1] # Top 15 keywords
top_keywords = [feature_names[i] for i in top_indices]
cluster_keywords[cluster_id] = top_keywords
print(f"\nCluster {cluster_id} (n={cluster_counts[cluster_id]}):")
print(f" Keywords: {', '.join(top_keywords)}")
# Analyze cluster purity if true labels are provided
if y_true is not None and label_encoder is not None:
print(f"\n{'='*70}")
print("CLUSTER PURITY ANALYSIS")
print(f"{'='*70}")
print("Analyzing how well clusters correspond to actual categories...\n")
category_names = label_encoder.classes_
for cluster_id in range(n_clusters):
# Get samples in this cluster
cluster_mask = cluster_labels == cluster_id
cluster_true_labels = y_true[cluster_mask]
# Count category distribution in this cluster
label_counts = Counter(cluster_true_labels)
total_in_cluster = len(cluster_true_labels)
print(f"Cluster {cluster_id}:")
print(f" Total samples: {total_in_cluster}")
print(f" Category distribution:")
# Sort by count (descending)
sorted_labels = sorted(
label_counts.items(), key=lambda x: x[1], reverse=True
)
for label_encoded, count in sorted_labels[:5]: # Top 5 categories
label_name = category_names[label_encoded]
percentage = (count / total_in_cluster) * 100
bar_length = int(percentage / 2) # Scale to 50 chars max
bar = "█" * bar_length
print(f" {label_name:15s}: {count:4d} ({percentage:5.1f}%) {bar}")
# Calculate purity (percentage of most common category)
if sorted_labels:
most_common_label, most_common_count = sorted_labels[0]
purity = (most_common_count / total_in_cluster) * 100
dominant_category = category_names[most_common_label]
print(f" Purity: {purity:.2f}% (dominant: {dominant_category})")
print()
# Calculate overall clustering quality
print(f"{'='*70}")
print("CLUSTERING QUALITY METRICS")
print(f"{'='*70}")
# Calculate average purity
total_purity = 0
for cluster_id in range(n_clusters):
cluster_mask = cluster_labels == cluster_id
cluster_true_labels = y_true[cluster_mask]
if len(cluster_true_labels) > 0:
label_counts = Counter(cluster_true_labels)
most_common_count = max(label_counts.values())
purity = most_common_count / len(cluster_true_labels)
total_purity += purity * len(cluster_true_labels)
avg_purity = total_purity / len(y_true)
print(f"Average cluster purity: {avg_purity:.4f} ({avg_purity*100:.2f}%)")
# Calculate inertia (within-cluster sum of squares)
inertia = kmeans.inertia_
print(f"Inertia (within-cluster sum of squares): {inertia:.2f}")
# Interpretation
print(f"\nInterpretation:")
if avg_purity >= 0.7:
print(
f"✓ High purity ({avg_purity*100:.1f}%) - Clusters strongly correspond to categories"
)
print(f" Data has naturally separable topic structure")
elif avg_purity >= 0.5:
print(
f"⚠ Moderate purity ({avg_purity*100:.1f}%) - Clusters partially correspond to categories"
)
print(f" Some topic overlap exists in the data")
else:
print(
f"✗ Low purity ({avg_purity*100:.1f}%) - Clusters don't align well with categories"
)
print(f" Topics may be mixed or need better feature engineering")
return kmeans, cluster_labels, cluster_keywords
def visualize_clustering_results(X_tfidf, cluster_labels, y_true, label_encoder):
"""
Visualize clustering results using dimensionality reduction.
Args:
X_tfidf: TF-IDF feature matrix
cluster_labels: Cluster assignments from K-Means
y_true: True category labels
label_encoder: Label encoder for category names
"""
print(f"\n{'='*70}")
print("CLUSTERING VISUALIZATION")
print(f"{'='*70}")
try:
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
print("Reducing dimensions for visualization (this may take a moment)...")
# Use PCA first to reduce to 50 dimensions (faster for t-SNE)
if X_tfidf.shape[1] > 50:
pca = PCA(n_components=50, random_state=42)
X_reduced = pca.fit_transform(X_tfidf.toarray())
print(f"✓ PCA: {X_tfidf.shape[1]} → 50 dimensions")
else:
X_reduced = X_tfidf.toarray()
# Apply t-SNE for 2D visualization
tsne = TSNE(n_components=2, random_state=42, perplexity=30, n_iter=1000)
X_2d = tsne.fit_transform(X_reduced)
print(f"✓ t-SNE: {X_reduced.shape[1]} → 2 dimensions")
# Create visualization
fig, axes = plt.subplots(1, 2, figsize=(18, 7))
# Plot 1: Colored by K-Means clusters
scatter1 = axes[0].scatter(
X_2d[:, 0], X_2d[:, 1], c=cluster_labels, cmap="tab10", alpha=0.6, s=30
)
axes[0].set_title(
"K-Means Clusters (Unsupervised)", fontsize=14, fontweight="bold"
)
axes[0].set_xlabel("t-SNE Component 1", fontsize=12)
axes[0].set_ylabel("t-SNE Component 2", fontsize=12)
plt.colorbar(scatter1, ax=axes[0], label="Cluster ID")
# Plot 2: Colored by true categories
scatter2 = axes[1].scatter(
X_2d[:, 0], X_2d[:, 1], c=y_true, cmap="tab10", alpha=0.6, s=30
)
axes[1].set_title(
"True Categories (Supervised)", fontsize=14, fontweight="bold"
)
axes[1].set_xlabel("t-SNE Component 1", fontsize=12)
axes[1].set_ylabel("t-SNE Component 2", fontsize=12)
# Create legend for true categories
category_names = label_encoder.classes_
handles = [
plt.Line2D(
[0],
[0],
marker="o",
color="w",
markerfacecolor=plt.cm.tab10(i / len(category_names)),
markersize=8,
label=cat,
)
for i, cat in enumerate(category_names)
]
axes[1].legend(handles=handles, loc="center left", bbox_to_anchor=(1, 0.5))
plt.tight_layout()
# Use consistent filename - overwrites previous file
filename = os.path.join(ASSET_FOLDER, "kmeans_clustering_visualization.png")