File size: 11,147 Bytes
867eb64 | 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 | # import pandas as pd
# from sentence_transformers import SentenceTransformer
# from sklearn.model_selection import train_test_split
# from sklearn.ensemble import RandomForestClassifier
# from sklearn.metrics import classification_report, confusion_matrix
# from sklearn.preprocessing import LabelEncoder
# from imblearn.over_sampling import SMOTE
# import joblib
#
# # Load dataset
# df = pd.read_csv(r"D:\Python_files\fully_merged.csv")
# df = df.dropna(subset=['article', 'label'])
# df = df[df['label'].isin(['positive', 'neutral', 'negative'])]
#
# # SBERT Embedding
# sbert_model = SentenceTransformer('all-MiniLM-L6-v2')
# embeddings = sbert_model.encode(df['article'].tolist(), show_progress_bar=True)
#
# # Encode labels
# label_encoder = LabelEncoder()
# y = label_encoder.fit_transform(df['label'])
#
# # Balance the dataset
# sm = SMOTE(random_state=42)
# X_resampled, y_resampled = sm.fit_resample(embeddings, y)
#
# # Train-test split
# X_train, X_test, y_train, y_test = train_test_split(
# X_resampled, y_resampled, test_size=0.2, stratify=y_resampled, random_state=42
# )
#
# # Train classifier
# clf = RandomForestClassifier(n_estimators=100, random_state=42)
# clf.fit(X_train, y_train)
# y_pred = clf.predict(X_test)
#
# # Results
# print("\nβ
SBERT + RandomForest Results")
# print(classification_report(y_test, y_pred, zero_division=0))
# print("\nπ Confusion Matrix:")
# print(confusion_matrix(y_test, y_pred))
#
# # Define SBERT wrapper for inference compatibility
# class SBERTTransformer:
# def __init__(self, model_name='all-MiniLM-L6-v2'):
# self.model = SentenceTransformer(model_name)
#
# def transform(self, sentences):
# return self.model.encode(sentences)
#
# def fit(self, X, y=None):
# return self
#
# # Save components
# vectorizer = SBERTTransformer() # Wraps SBERT model
# pipeline = {
# "vectorizer": vectorizer,
# "model": clf,
# "label_encoder": label_encoder
# }
#
# joblib.dump(pipeline, "D:/Python_files/models/sentiment_pipeline.joblib")
# print("β
Model saved successfully to sentiment_pipeline.joblib")
#
# import pandas as pd
# from sentence_transformers import SentenceTransformer
# from sklearn.model_selection import StratifiedKFold
# from sklearn.ensemble import RandomForestClassifier
# from sklearn.metrics import classification_report, confusion_matrix
# from sklearn.preprocessing import LabelEncoder
# from imblearn.over_sampling import SMOTE
# import joblib
# import numpy as np
#
# # Load dataset
# df = pd.read_csv(r"D:\Python_files\fully_merged.csv")
# df = df.dropna(subset=['article', 'label'])
# df = df[df['label'].isin(['positive', 'neutral', 'negative'])]
#
# # SBERT Embedding
# sbert_model = SentenceTransformer('all-MiniLM-L6-v2')
# embeddings = sbert_model.encode(df['article'].tolist(), show_progress_bar=True)
#
# # Encode labels
# label_encoder = LabelEncoder()
# y = label_encoder.fit_transform(df['label'])
#
# # Balance the dataset
# sm = SMOTE(random_state=42)
# X_resampled, y_resampled = sm.fit_resample(embeddings, y)
#
# # Stratified K-Fold Cross Validation
# kf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
# all_reports = []
# fold = 1
#
# for train_index, test_index in kf.split(X_resampled, y_resampled):
# print(f"\nπ Fold {fold}")
# X_train, X_test = X_resampled[train_index], X_resampled[test_index]
# y_train, y_test = y_resampled[train_index], y_resampled[test_index]
#
# clf = RandomForestClassifier(n_estimators=100, random_state=42)
# clf.fit(X_train, y_train)
# y_pred = clf.predict(X_test)
#
# report = classification_report(y_test, y_pred, target_names=label_encoder.classes_, zero_division=0, output_dict=True)
# all_reports.append(report)
#
# print(classification_report(y_test, y_pred, target_names=label_encoder.classes_, zero_division=0))
# print("Confusion Matrix:")
# print(confusion_matrix(y_test, y_pred))
# fold += 1
#
# # Average report (macro avg)
# avg_report = {}
# for label in label_encoder.classes_:
# avg_report[label] = {
# metric: np.mean([rep[label][metric] for rep in all_reports])
# for metric in ['precision', 'recall', 'f1-score']
# }
#
# print("\nπ Average Classification Report across folds:")
# for label, metrics in avg_report.items():
# print(f"\nLabel: {label}")
# for metric, value in metrics.items():
# print(f"{metric}: {value:.4f}")
#
# # Save final model from last fold (or retrain on full data if preferred)
# final_clf = RandomForestClassifier(n_estimators=100, random_state=42)
# final_clf.fit(X_resampled, y_resampled)
#
# # Define SBERT wrapper
# class SBERTTransformer:
# def __init__(self, model_name='all-MiniLM-L6-v2'):
# self.model = SentenceTransformer(model_name)
#
# def transform(self, sentences):
# return self.model.encode(sentences)
#
# def fit(self, X, y=None):
# return self
#
# # Save final pipeline
# vectorizer = SBERTTransformer()
# pipeline = {
# "vectorizer": vectorizer,
# "model": final_clf,
# "label_encoder": label_encoder
# }
#
# joblib.dump(pipeline, "D:/Python_files/models/sentiment_pipeline.joblib")
# print("\nβ
Final model saved successfully to sentiment_pipeline.joblib")
import pandas as pd
from sentence_transformers import SentenceTransformer
from sklearn.model_selection import StratifiedKFold
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.preprocessing import LabelEncoder
from imblearn.over_sampling import SMOTE
import joblib
import numpy as np
from tqdm import tqdm
# --- 1. Data Loading and Preparation ---
print("π Loading and preparing data...")
df = pd.read_csv(r"D:\Python_files\fully_merged.csv")
df = df.dropna(subset=['article', 'label'])
df = df[df['label'].isin(['positive', 'neutral', 'negative'])]
print("β
Data loaded successfully.")
# --- 2. SBERT Embedding with Chunking and Averaging ---
print("π§ Initializing SBERT model...")
sbert_model = SentenceTransformer('all-MiniLM-L6-v2')
# Define chunking parameters
# We use a chunk size smaller than the model's max sequence length (256)
CHUNK_SIZE = 200
OVERLAP = 50
all_article_embeddings = []
print(f"π Generating embeddings with chunking (Chunk size: {CHUNK_SIZE}, Overlap: {OVERLAP})...")
# Use tqdm for a progress bar as this process is slower
for article in tqdm(df['article'].tolist(), desc="Embedding Articles"):
# Split article into words
words = article.split()
# If the article is short, no chunking is needed
if len(words) <= CHUNK_SIZE:
article_embedding = sbert_model.encode([article])
else:
# Create overlapping chunks
chunks = []
for i in range(0, len(words), CHUNK_SIZE - OVERLAP):
chunk = " ".join(words[i:i + CHUNK_SIZE])
chunks.append(chunk)
# Encode each chunk and store their embeddings
chunk_embeddings = sbert_model.encode(chunks)
# Average the embeddings of all chunks to get a single vector
article_embedding = np.mean(chunk_embeddings, axis=0, keepdims=True)
all_article_embeddings.append(article_embedding[0])
# Convert the list of embeddings to a NumPy array
embeddings = np.array(all_article_embeddings)
print("β
Embeddings generated successfully.")
# --- 3. Encode Labels ---
print("π·οΈ Encoding labels...")
label_encoder = LabelEncoder()
y = label_encoder.fit_transform(df['label'])
# --- 4. Balance the Dataset ---
print("βοΈ Balancing the dataset with SMOTE...")
sm = SMOTE(random_state=42)
X_resampled, y_resampled = sm.fit_resample(embeddings, y)
print(f"Dataset balanced. Original samples: {len(y)}, Resampled samples: {len(y_resampled)}")
# --- 5. Stratified K-Fold Cross Validation ---
print("π Starting 5-Fold Cross-Validation...")
kf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
all_reports = []
fold = 1
for train_index, test_index in kf.split(X_resampled, y_resampled):
print(f"\n--- Fold {fold} ---")
X_train, X_test = X_resampled[train_index], X_resampled[test_index]
y_train, y_test = y_resampled[train_index], y_resampled[test_index]
clf = RandomForestClassifier(n_estimators=100, random_state=42, n_jobs=-1)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
report = classification_report(y_test, y_pred, target_names=label_encoder.classes_, zero_division=0,
output_dict=True)
all_reports.append(report)
print(classification_report(y_test, y_pred, target_names=label_encoder.classes_, zero_division=0))
print("Confusion Matrix:")
print(confusion_matrix(y_test, y_pred))
fold += 1
# --- 6. Average Report Calculation ---
avg_report = {}
for label in label_encoder.classes_:
avg_report[label] = {
metric: np.mean([rep[label][metric] for rep in all_reports])
for metric in ['precision', 'recall', 'f1-score']
}
print("\nπ Average Classification Report Across All Folds:")
for label, metrics in avg_report.items():
print(f"\nLabel: {label}")
for metric, value in metrics.items():
print(f"{metric}: {value:.4f}")
# --- 7. Final Model Training ---
print("\nπͺ Training final model on the full, balanced dataset...")
final_clf = RandomForestClassifier(n_estimators=100, random_state=42, n_jobs=-1)
final_clf.fit(X_resampled, y_resampled)
print("β
Final model trained.")
# --- 8. Define SBERT Wrapper with Chunking Logic ---
# This class is CRITICAL for the saved pipeline to work correctly on new, long text.
class SBERTTransformer:
def __init__(self, model_name='all-MiniLM-L6-v2'):
self.model = SentenceTransformer(model_name)
self.chunk_size = 200
self.overlap = 50
def transform(self, sentences):
"""
Transforms a list of sentences (articles) into embeddings using chunking.
"""
all_embeddings = []
for sentence in tqdm(sentences, desc="Vectorizing new data"):
words = sentence.split()
if len(words) <= self.chunk_size:
embedding = self.model.encode([sentence])
else:
chunks = []
for i in range(0, len(words), self.chunk_size - self.overlap):
chunk = " ".join(words[i:i + self.chunk_size])
chunks.append(chunk)
chunk_embeddings = self.model.encode(chunks)
embedding = np.mean(chunk_embeddings, axis=0, keepdims=True)
all_embeddings.append(embedding[0])
return np.array(all_embeddings)
def fit(self, X, y=None):
# This model is already pre-trained, so fit does nothing.
return self
# --- 9. Save Final Pipeline ---
print("πΎ Saving the final pipeline to disk...")
vectorizer = SBERTTransformer()
pipeline = {
"vectorizer": vectorizer,
"model": final_clf,
"label_encoder": label_encoder
}
joblib.dump(pipeline, "D:/Python_files/models/sentiment_pipeline_chunking.joblib")
print("\nβ
Final model saved successfully to sentiment_pipeline_chunking.joblib")
|