-
Notifications
You must be signed in to change notification settings - Fork 0
/
HN_DataTransform_toNormalizedVec.py
72 lines (37 loc) · 1.1 KB
/
HN_DataTransform_toNormalizedVec.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
# coding: utf-8
# In[ ]:
import pandas as pd
FILE = "tokens_lower.csv"
df = pd.read_csv(FILE, header=None, dtype=str)
# In[ ]:
import numpy as np
title_id = df[0]
title_id = title_id.astype(np.int32)
# In[ ]:
title = df[np.arange(1,df.shape[1])]
# In[ ]:
import DataLoader.GloVe as gl
glove = gl.load('/Users/Shared/data/glove.6B/glove.6B.50d.txt')
# In[ ]:
from sklearn import preprocessing
import math
def toVec(word):
if not isinstance(word, str) and np.isnan(word):
return np.zeros(50)
try:
vec = glove.loc[word]
norm_vec = preprocessing.normalize(vec.values.reshape(1,-1))
return norm_vec[0]
except:
return np.zeros(50)
title_vec = title.applymap(toVec)
# In[ ]:
title_expand_vec = pd.DataFrame()
for i in range(1,title_vec.shape[1] + 1):
title_expand_vec = pd.concat([title_expand_vec, title_vec[i].apply(pd.Series)], axis=1)
# In[ ]:
result = pd.concat([title_id, title_expand_vec], axis=1)
result.columns = ['id'] + list(range(0, 1200))
# In[ ]:
result.to_csv('./hn_title_norm_vec.csv', header=True, index=False)
# In[ ]: