-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx_file.py
More file actions
317 lines (241 loc) · 11.1 KB
/
tx_file.py
File metadata and controls
317 lines (241 loc) · 11.1 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
# -*- coding: utf-8 -*-
"""TX_file.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1wGenDcQLV9YMAJZYb7G6AQCqYyAM5ng7
"""
import pandas as pd
data_xlsx = pd.read_csv('/content/drive/MyDrive/Step_5/combined_file_latest.csv')
# Display the first few rows of the dataset
data_xlsx.head()
data_xlsx.shape
from gensim.models import KeyedVectors
embeddings = KeyedVectors.load_word2vec_format('/content/drive/MyDrive/Step_5/custom_word2vec_format.bin', binary=True)
import numpy as np
def review_to_embedding(review):
# Tokenize the review using spaces (since we already preprocessed the reviews)
tokens = review.split()
# Get the embeddings for each token
token_embeddings = [embeddings[token] for token in tokens if token in embeddings]
# Aggregate the embeddings (using mean here)
if token_embeddings:
review_embedding = np.mean(token_embeddings, axis=0)
else:
review_embedding = np.zeros(embeddings.vector_size) # If no word has an embedding, return a zero vector
return review_embedding
# Convert each review in top_1999_reviews to its embedding
embeddings_list = [review_to_embedding(review) for review in data_xlsx["Documents"]]
embeddings_array = np.array(embeddings_list)
embeddings_array.shape
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
# Features (embeddings)
X = embeddings_array
# Labels encoding
label_encoder = LabelEncoder()
y = label_encoder.fit_transform(data_xlsx['Labels'])
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.05, random_state=42)
X_train.shape, X_test.shape, y_train.shape, y_test.shape
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import accuracy_score, classification_report
# Split the data with the new specified split
X_train_new, X_test_new, y_train_new, y_test_new = train_test_split(X, y, test_size=49, train_size=2950, random_state=42)
# Train the GBM model with the new split
gbm_clf_new = GradientBoostingClassifier(random_state=42)
gbm_clf_new.fit(X_train_new, y_train_new)
# Predict on the new test set
y_pred_gbm_new = gbm_clf_new.predict(X_test_new)
from sklearn.metrics import f1_score
f1 = f1_score(y_test_new, y_pred_gbm_new, average='weighted')
print(f"F1 Score: {f1:.4f}")
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import uniform
# Define the hyperparameters and their distributions
param_dist = {
'learning_rate': uniform(0.01, 0.1),
'n_estimators': [50, 100, 150],
'max_depth': [3, 4, 5],
'min_samples_split': [2, 3],
'min_samples_leaf': [1, 2]
}
# Initialize a RandomizedSearchCV object to sample hyperparameters
random_search = RandomizedSearchCV(GradientBoostingClassifier(random_state=42),
param_distributions=param_dist,
n_iter=10, # Number of parameter settings to sample
cv=3,
scoring='accuracy',
n_jobs=-1,
verbose=1,
random_state=42)
# Fit the model to find the best hyperparameters
random_search.fit(X_train_new, y_train_new)
# Retrieve the best hyperparameters and the best model
best_params_random = random_search.best_params_
best_gbm_model_random = random_search.best_estimator_
# Evaluate the best model from random search on the test set
y_pred_best_gbm_random = best_gbm_model_random.predict(X_test_new)
accuracy_best_gbm_random = accuracy_score(y_test_new, y_pred_best_gbm_random)
class_report_best_gbm_random = classification_report(y_test_new, y_pred_best_gbm_random)
best_params_random, accuracy_best_gbm_random, class_report_best_gbm_random
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import accuracy_score, classification_report, f1_score
from sklearn.model_selection import train_test_split
# Split the data with the new specified split
X_train_new, X_test_new, y_train_new, y_test_new = train_test_split(X, y, test_size=49, train_size=2950, random_state=42)
# Train the GBM model with the new split
gbm_clf_new = GradientBoostingClassifier(
random_state=42,
learning_rate=0.062475643163223786,
max_depth=3,
min_samples_leaf=1,
min_samples_split=2,
n_estimators=150
)
gbm_clf_new.fit(X_train_new, y_train_new)
# Predict on the new test set
y_pred_gbm_new = gbm_clf_new.predict(X_test_new)
# Compute the F1 score
f1 = f1_score(y_test_new, y_pred_gbm_new, average='weighted')
f1
#Testing with Val set
import pandas as pd
val_set = pd.read_csv("/content/drive/MyDrive/Step_5/dev_set.csv")
embeddings_list_dev = [review_to_embedding(review) for review in val_set["Document"]]
embeddings_array_dev = np.array(embeddings_list_dev)
y_pred_gbm_new = gbm_clf_new.predict(embeddings_array_dev)
y_true = np.array(pd.read_csv("/content/drive/MyDrive/Step_5/Movies_Val.csv", header= None).iloc[:,0])
f1 = f1_score(y_true, y_pred_gbm_new, average='weighted')
print(f"F1 Score: {f1:.4f}")
#Testing with Test set
import pandas as pd
test_set = pd.read_csv("/content/drive/MyDrive/Step_5/test_set.csv")
embeddings_list_test = [review_to_embedding(review) for review in test_set["Document"]]
embeddings_array_test = np.array(embeddings_list_test)
y_pred_gbm_new = gbm_clf_new.predict(embeddings_array_test)
with open("/content/drive/MyDrive/Step_5/output.txt", "w") as file:
for number in y_pred_gbm_new:
file.write(str(number) + '\n')
y_pred_gbm_new.shape
import pandas as pd
# Define a function to get the last n lines from a file
def get_last_n_lines(file_path, n):
with open(file_path, 'r') as f:
lines = f.readlines()[:n]
return [line.strip() for line in lines]
# Use the function to get the last 7600 lines
lines = get_last_n_lines("/content/drive/MyDrive/Step5_News/text.txt", 100)
# Convert the list of lines to a DataFrame
df = pd.DataFrame(lines, columns=["Document"])
# Save the DataFrame to a CSV file
df.to_csv("/content/drive/MyDrive/Step5_News/dev_set.csv", index=False)
#News
import pandas as pd
data_xlsx = pd.read_csv('/content/drive/MyDrive/Step5_News/new_set.csv')
# Display the first few rows of the dataset
data_xlsx.head()
from gensim.models import KeyedVectors
embeddings = KeyedVectors.load_word2vec_format('/content/drive/MyDrive/Step5_News/custom_word2vec_format (3).bin', binary=True)
import numpy as np
def review_to_embedding(review):
# Tokenize the review using spaces (since we already preprocessed the reviews)
tokens = review.split()
# Get the embeddings for each token
token_embeddings = [embeddings[token] for token in tokens if token in embeddings]
# Aggregate the embeddings (using mean here)
if token_embeddings:
review_embedding = np.mean(token_embeddings, axis=0)
else:
review_embedding = np.zeros(embeddings.vector_size) # If no word has an embedding, return a zero vector
return review_embedding
embeddings_list = [review_to_embedding(review) for review in data_xlsx["Documents"]]
embeddings_array = np.array(embeddings_list)
embeddings_array.shape
from sklearn.metrics import f1_score
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingClassifier
# Custom mapping
custom_mapping = {
'politics': 0,
'sports': 1,
'business': 2,
'technology': 3
}
# Apply the custom mapping to encode the labels
encoded_labels = data_xlsx['Labels'].map(custom_mapping)
# Split the data with the new specified split
X_train_new, X_test_new, y_train_new, y_test_new = train_test_split(embeddings_array, encoded_labels, test_size=100, train_size=5900, random_state=42)
# Train the GBM model with the new split
gbm_clf_new = GradientBoostingClassifier(random_state=42)
gbm_clf_new.fit(X_train_new, y_train_new)
# Predict on the new test set
y_pred_gbm_new = gbm_clf_new.predict(X_test_new)
# Calculate the F1 score
f1 = f1_score(y_test_new, y_pred_gbm_new, average='weighted')
print(f"F1 Score: {f1:.4f}")
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import uniform
# Define the hyperparameters and their distributions
param_dist = {
'learning_rate': uniform(0.01, 0.1),
'n_estimators': [50, 100, 150],
'max_depth': [3, 4, 5],
'min_samples_split': [2, 3],
'min_samples_leaf': [1, 2]
}
# Initialize a RandomizedSearchCV object to sample hyperparameters
random_search = RandomizedSearchCV(GradientBoostingClassifier(random_state=42),
param_distributions=param_dist,
n_iter=10, # Number of parameter settings to sample
cv=3,
scoring='accuracy',
n_jobs=-1,
verbose=1,
random_state=42)
# Fit the model to find the best hyperparameters
random_search.fit(X_train_new, y_train_new)
# Retrieve the best hyperparameters and the best model
best_params_random = random_search.best_params_
best_gbm_model_random = random_search.best_estimator_
# Evaluate the best model from random search on the test set
y_pred_best_gbm_random = best_gbm_model_random.predict(X_test_new)
f1_score_best_gbm_random = f1_score(y_test_new, y_pred_best_gbm_random,average='weighted')
best_params_random, f1_score_best_gbm_random
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import accuracy_score, classification_report, f1_score
from sklearn.model_selection import train_test_split
# Split the data with the new specified split
X_train_new, X_test_new, y_train_new, y_test_new = train_test_split(embeddings_array, encoded_labels, test_size=100, train_size=5900, random_state=42)
# Train the GBM model with the new split
gbm_clf_new = GradientBoostingClassifier(
random_state=42,
learning_rate=0.062475643163223786,
max_depth=3,
min_samples_leaf=1,
min_samples_split=2,
n_estimators=150
)
gbm_clf_new.fit(X_train_new, y_train_new)
# Predict on the new test set
y_pred_gbm_new = gbm_clf_new.predict(X_test_new)
# Compute the F1 score
f1 = f1_score(y_test_new, y_pred_gbm_new, average='weighted')
f1
#Testing with Val set
import pandas as pd
val_set = pd.read_csv("/content/drive/MyDrive/Step5_News/dev_set.csv")
embeddings_list_dev = [review_to_embedding(review) for review in val_set["Document"]]
embeddings_array_dev = np.array(embeddings_list_dev)
y_pred_gbm_new = gbm_clf_new.predict(embeddings_array_dev)
y_true = np.array(pd.read_csv("/content/drive/MyDrive/Step5_News/dev_labels.csv").iloc[:,0])
f1 = f1_score(y_true, y_pred_gbm_new, average='weighted')
print(f"F1 Score: {f1:.4f}")
#Testing with Test set
import pandas as pd
test_set = pd.read_csv("/content/drive/MyDrive/Step5_News/test_set.csv")
embeddings_list_test = [review_to_embedding(review) for review in test_set["Document"]]
embeddings_array_test = np.array(embeddings_list_test)
y_pred_gbm_new = gbm_clf_new.predict(embeddings_array_test)
with open("/content/drive/MyDrive/Step5_News/output.txt", "w") as file:
for number in y_pred_gbm_new:
file.write(str(number) + '\n')