Mastouri commited on
Commit
f424ca3
·
1 Parent(s): 5cfe83b

Updated XGBoost model with TF-IDF vectorizer

Browse files
Files changed (1) hide show
  1. logistic_reg.py +69 -0
logistic_reg.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datasets import load_dataset
2
+ import pandas as pd
3
+ import numpy as np
4
+ from sklearn.feature_extraction.text import TfidfVectorizer
5
+ from sklearn.preprocessing import MultiLabelBinarizer
6
+ from sklearn.metrics import hamming_loss, f1_score, classification_report
7
+ import xgboost as xgb
8
+ from joblib import dump, load
9
+
10
+ # Step 1: Load the Dataset Repository
11
+ dataset = load_dataset("meriemm6/commit-classification-dataset", data_files={"train": "training.csv", "validation": "validation.csv"})
12
+
13
+ # Convert the training and validation splits to pandas DataFrames
14
+ train_data = dataset["train"].to_pandas()
15
+ validation_data = dataset["validation"].to_pandas()
16
+
17
+ # Step 2: Clean and Process the Data
18
+ # Fill missing values in the 'Message' column with "unknown"
19
+ train_data['Message'] = train_data['Message'].fillna("unknown")
20
+ validation_data['Message'] = validation_data['Message'].fillna("unknown")
21
+
22
+ # Fill missing values in the 'Ground truth' column with "maintenance/other"
23
+ train_data['Ground truth'] = train_data['Ground truth'].fillna("maintenance/other")
24
+ validation_data['Ground truth'] = validation_data['Ground truth'].fillna("maintenance/other")
25
+
26
+ # Split the 'Ground truth' column into lists of labels
27
+ train_data['Ground truth'] = train_data['Ground truth'].apply(lambda x: x.split(', '))
28
+ validation_data['Ground truth'] = validation_data['Ground truth'].apply(lambda x: x.split(', '))
29
+
30
+ # Encode the labels
31
+ mlb = MultiLabelBinarizer()
32
+ y_train_encoded = mlb.fit_transform(train_data['Ground truth'])
33
+ y_val_encoded = mlb.transform(validation_data['Ground truth'])
34
+
35
+ # Step 3: TF-IDF Vectorization (Increased Features)
36
+ tfidf_vectorizer = TfidfVectorizer(max_features=10000, stop_words="english")
37
+ X_train_tfidf = tfidf_vectorizer.fit_transform(train_data['Message'])
38
+ X_val_tfidf = tfidf_vectorizer.transform(validation_data['Message'])
39
+
40
+
41
+
42
+ # Save the TF-IDF vectorizer
43
+ dump(tfidf_vectorizer, "tfidf_vectorizer_xgboost.joblib")
44
+
45
+ # Step 4: Add Class Weighting
46
+ label_counts = y_train_encoded.sum(axis=0)
47
+ scale_pos_weight = (len(y_train_encoded) - label_counts) / label_counts
48
+
49
+ # Step 5: Train XGBoost Models with Class Weighting and Dynamic Parameters
50
+ models = []
51
+ for i in range(y_train_encoded.shape[1]):
52
+ model = xgb.XGBClassifier(
53
+ objective="binary:logistic",
54
+ use_label_encoder=False,
55
+ eval_metric="logloss",
56
+ scale_pos_weight=scale_pos_weight[i], # Class weights
57
+ max_depth=6, # Reduced to prevent overfitting
58
+ learning_rate=0.03, # Lower learning rate for better generalization
59
+ n_estimators=300, # Increased estimators for better performance
60
+ subsample=0.8,
61
+ colsample_bytree=0.8,
62
+ min_child_weight=1 # Prevents overfitting on small datasets
63
+ )
64
+ model.fit(X_train_tfidf, y_train_encoded[:, i])
65
+ models.append(model)
66
+
67
+ # Save the models
68
+ for idx, model in enumerate(models):
69
+ dump(model, f"xgboost_model_label_{idx}.joblib")