-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbaseline_extension_3.py
More file actions
154 lines (100 loc) · 3.46 KB
/
baseline_extension_3.py
File metadata and controls
154 lines (100 loc) · 3.46 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
from sklearn.preprocessing import MinMaxScaler
import pandas as pd
from sklearn import svm
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LogisticRegression, LinearRegression
from sklearn.ensemble import AdaBoostRegressor, GradientBoostingRegressor, AdaBoostClassifier, GradientBoostingClassifier
import datetime
start = datetime.datetime.now()
X_train_df = pd.read_csv('Training_Data_Extension_3.csv')
X_test_full_df = pd.read_csv('Test_Data_Extension_3.csv')
y_train_df = pd.DataFrame(X_train_df['Label'],columns=["Label"])
X_train_df = X_train_df.drop(columns=['Label'])
X_test_ids = X_test_full_df['article-sentence'].tolist()
X_test_articles = [x.split("-")[0] for x in X_test_ids]
y_test_df = pd.DataFrame(X_test_full_df['Label'],columns=["Label"])
X_test_df = X_test_full_df.drop(columns=['Label','article-sentence'])
# print(X_train_df.head(10))
# print(y_train_df)
y_train_np = np.array(y_train_df).squeeze()
X_train_np = np.array(X_train_df)
y_test_np = np.array(y_test_df).squeeze()
X_test_np = np.array(X_test_df)
#clf = LogisticRegression()
#CANDIDATE WINNER
#clf = RandomForestRegressor(n_estimators=60, max_depth=10, random_state=0)
#clf = LinearRegression()
#CANDIDATE WINNER
#clf = AdaBoostClassifier(n_estimators=100)
clf = GradientBoostingClassifier()
#clf = AdaBoostRegressor()
#clf = GradientBoostingRegressor(n_estimators=100, max_depth=10, max_features = 3)
#clf = svm.SVR(kernel='linear', C=1e3, gamma=0.1)
scaler = MinMaxScaler()
X_train_np = scaler.fit_transform(X_train_np)
clf.fit(X_train_np, y_train_np)
X_test_np = scaler.transform(X_test_np)
value = clf.predict(X_test_np)
#value = clf.predict_proba(X_test_np)
#value = value[:,0]
#print('output')
value = value.tolist()
final_df = pd.DataFrame(
{'article': X_test_articles,
'index': X_test_ids,
'score': value
}).set_index('index', drop=True)
# print('final_df')
# print(final_df)
# output_df = final_df.sort_values(['article','sentence','score'], ascending=[1,0]).groupby('article').head(3)
output_df = final_df.sort_values(['article','score'], ascending=[1,0]).groupby('article').head(3)
# t = df.groupby(['borough', 'title']).sum()
# t.sort('total_loans', ascending=True)
# t = t.groupby(level=[0,1]).head(3).reset_index()
# t.sort(['borough', 'title'], ascending=(True, False))
# print(value)
# print('set',y_test_set)
# print('output_df')
# print(output_df)
selected_sentences = output_df.index.values
selected_sentences = selected_sentences.tolist()
# print('selected_sentences')
# print(selected_sentences)
y_pred = [1 if x in selected_sentences else 0 for x in X_test_ids]
y_pred_df = pd.DataFrame(
{'y_pred': y_pred,
})
# print('y_pred_df')
# print(y_pred_df.head(20))
f = open("entity_scores_test.txt","r")
sentences = []
article_ids = []
sentence_ids = []
article_id = 0
sentence_id = 0
summary = ""
summaries = []
for line in f:
article_num = line.split("@@@")[0]
text = line.split("@@@")[2].strip()
if article_num != article_id:
sentence_id = 0
summaries.append(summary)
summary = ""
article_id = article_num
else:
sentence_id += 1
new_id = str(article_num.strip())+"-"+str(sentence_id)
if new_id in selected_sentences:
summary += text
summaries = summaries[1:]
#print(summaries)
with open("y_pred_extension_3.csv","w") as f:
for line in summaries:
f.write(line)
f.write("\n")
end = datetime.datetime.now()
time = end - start
print(time)