-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwork2vec.py
140 lines (80 loc) · 2.37 KB
/
work2vec.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
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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
from tqdm import tqdm
from sklearn.model_selection import KFold
from sklearn.metrics import classification_report, f1_score
import lightgbm as lgb
from collections import Counter
import warnings
import random
# In[2]:
df=pd.read_csv("topo.txt",sep='\t',names=['head_node','tail_node'],header=None) #导入数据
df.head()
df['tail_node']=df['tail_node'].apply(lambda x: x.split(","))
df.head()
# In[3]:
data=df.iloc[:,:]
data_dict=dict()
for i in tqdm(range(0,data.shape[0])):
data_dict[str(data.iloc[i,0])]=data.iloc[i,1] #拆分成字典
# In[4]:
##随机游走
step=3
work_vec_row=[]
for i in tqdm(range(0,700000)):
str_i=str(i)
if str_i in data_dict:
work_vec_loc=[]
work_vec_loc.insert(1,str_i )
rd=random.choice(data_dict[str_i])
str_j=rd
for j in range(0,step):
if str_j in data_dict:
work_vec_loc.insert(1,str_j)
str_j=random.choice(data_dict[str_j])
else :
work_vec_loc.insert(1,str_j)
work_vec_row.insert(1,work_vec_loc)
work_vec_row
# In[5]:
##写入文件
df_result = pd.DataFrame(columns=['head', 'step_1', 'step_2', 'step_3'])
work_vec_row=np.array(work_vec_row)
df_result['head'] = work_vec_row[:,0]
df_result['step_1'] = work_vec_row[:,1]
df_result['step_2'] = work_vec_row[:,2]
df_result['step_3'] = work_vec_row[:,3]
df_result.to_csv("word2vec_input.csv", index=False)
# In[11]:
sentences=work_vec_row[:,:]
sentences=sentences.tolist()
type(sentences)
# In[19]:
##Word2vec生成词向量
from gensim.models import Word2Vec
model = Word2Vec(sentences, min_count=1,workers=12,iter=128)
print(len(model.wv.vocab))
# In[20]:
model.save('word2vec_test_1.model')
# In[21]:
print(model['458966'])
# In[8]:
#导出文件
import pandas as pd
import numpy as np
from tqdm import tqdm
from sklearn.model_selection import KFold
from sklearn.metrics import classification_report, f1_score
import lightgbm as lgb
from collections import Counter
import warnings
import random
from gensim.models import Word2Vec
model = Word2Vec()
# In[10]:
model = Word2Vec.load('word2vec_test_1.model')
# In[12]:
model.wv.save_word2vec_format('word2vec_test_1.csv',binary = False)