-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLightGBM.py
71 lines (57 loc) · 2.85 KB
/
LightGBM.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
import pandas as pd
import numpy as np
from tqdm.auto import tqdm
import sys
from xgb_dataset_generation import *
import os
import lightgbm as lgb
import time
###### Change this parameter ###########
validation_path = "dataset/validation_2"
train = base_expanded_df(isValidation=True, save=True, path=validation_path)
test = base_expanded_df(isValidation=False, save=True)
#train = pd.read_csv("dataset/expanded/base_expanded_train.csv")
#test = pd.read_csv("dataset/expanded/base_expanded_test.csv")
train = adding_features(train, isValidation=True, path=validation_path)
test = adding_features(test, isValidation=False)
save_path = os.path.join(validation_path, "train_complete.csv")
train.to_csv(save_path , index=False)
test.to_csv('test_complete.csv', index=False)
#train = pd.read_csv("train_complete.csv")
#test = pd.read_csv("test_complete.csv")
group = train.groupby('queried_record_id').size().values
ranker = lgb.LGBMRanker(device='gpu')
print('Start LGBM...')
t1 = time.time()
ranker.fit(train.drop(['queried_record_id', 'target', 'predicted_record_id','predicted_record_id_record', 'linked_id_idx'], axis=1), train['target'], group=group)
t2 = time.time()
print(f'Learning completed in {int(t2-t1)} seconds.')
predictions = ranker.predict(test.drop(['queried_record_id', 'linked_id_idx', 'predicted_record_id','predicted_record_id_record'], axis=1))
test['predictions'] = predictions
df_predictions = test[['queried_record_id', 'predicted_record_id', 'predicted_record_id_record', 'predictions']]
rec_pred = []
for (l,p,record_id) in zip(df_predictions.predicted_record_id, df_predictions.predictions, df_predictions.predicted_record_id_record):
rec_pred.append((l, p, record_id))
df_predictions['rec_pred'] = rec_pred
group_queried = df_predictions[['queried_record_id', 'rec_pred']].groupby('queried_record_id').apply(lambda x: list(x['rec_pred']))
df_predictions = pd.DataFrame(group_queried).reset_index().rename(columns={0 : 'rec_pred'})
def reorder_preds(preds):
ordered_lin = []
ordered_score = []
ordered_record = []
for i in range(len(preds)):
l = sorted(preds[i], key=lambda t: t[1], reverse=True)
lin = [x[0] for x in l]
s = [x[1] for x in l]
r = [x[2] for x in l]
ordered_lin.append(lin)
ordered_score.append(s)
ordered_record.append(r)
return ordered_lin, ordered_score, ordered_record
df_predictions['ordered_linked'], df_predictions['ordered_scores'], df_predictions['ordered_record'] = reorder_preds(df_predictions.rec_pred.values)
#df_predictions = df_predictions[['queried_record_id', 'ordered_preds']].rename(columns={'ordered_preds': 'predicted_record_id'})
#new_col = []
#for t in tqdm(df_predictions.predicted_record_id):
# new_col.append(' '.join([str(x) for x in t]))
#df_predictions.predicted_record_id = new_col
df_predictions.to_csv('lgb_predictions_new.csv', index=False)