-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.py
277 lines (216 loc) · 8.59 KB
/
analysis.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
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
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 15 03:32:05 2015
@author: leo
Trying different stuff here :
One part is just loading the data.
Another part is creating a co-existance frequency table
Another part is implementing a "word2vec-like" thing to assign vectors to politicians
"""
import numpy as np
from math import sqrt
import pandas as pd
from os import listdir
from os.path import isfile, join
import itertools
import matplotlib.pyplot as plt
from scipy.spatial.distance import cosine
import pandas_pybrain
import pybrain
from pybrain.datasets import SupervisedDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.structure.modules import SigmoidLayer
from pybrain.structure.modules import TanhLayer
def create_ids_tab(path_ids_group, group_name):
'''Creates table of ids in record format for group group_name'''
print 'Creating ids_tab'
files = [ f for f in listdir(path_ids_group) if isfile(join(path_ids_group,f)) ]
ids_tab_rec = pd.DataFrame(columns = [group_name, 'id'])
all_ids = dict()
for file in files:
f = open(join(path_ids_group, file), 'r')
ids = f.read().split('\n')
f.close()
local_tab = pd.DataFrame()
local_tab['id'] = ids[:-1]
local_tab[group_name] = file.replace('_ids.txt', '')
ids_tab_rec = ids_tab_rec.append(local_tab)
all_ids[file.replace('_ids.txt', '')] = ids
ids_tab_rec['True'] = True
ids_tab = pd.pivot_table(ids_tab_rec, values = 'True', columns = group_name, index = 'id').fillna(False)
print 'Ids_tab created'
return (all_ids, ids_tab_rec, ids_tab)
def _make_train_set(row):
'''Assumes all columns in row are words in vocabulary'''
new_columns = list(row.index) + ['context_' + col for col in row.index]
return_tab = pd.DataFrame(columns = new_columns)
list_row = list(row)
len_row = len(row)
for i, val in enumerate(list_row):
if val:
new_list = [False]*i + [True] + [False]*(len_row - i - 1) + list_row[:i] + [False] + list_row[i+1:]
return_tab = return_tab.append(pd.DataFrame([new_list], columns = new_columns))
return return_tab
path = '.'
path_ids = join(path, 'data', 'ids')
path_ids_news_fr = join(path_ids, 'news_fr')
path_ids_politiques_fr = join(path_ids, 'politiques_fr')
print 'here3'
(all_ids, ids_tab_rec, ids_tab) = create_ids_tab(path_ids_politiques_fr, 'politique')
# Perform PCA
from sklearn.decomposition import PCA
import random
from mpl_toolkits.mplot3d import Axes3D
ids_tab = ids_tab[ids_tab.sum(axis = 1) >= 2]
index = random.sample(ids_tab.index, min(1000000, len(ids_tab))) # Max 1M
ids_tab = ids_tab.loc[index,:]
pca = PCA(n_components=5)
pca.fit(ids_tab)
eigenvectors = pd.DataFrame(columns = ids_tab.columns)
for i, row in enumerate(pca.components_):
eigenvectors.loc[i, :] = row
# Make plot according to top eigenvectors
a = eigenvectors.loc[0, :] #
b = eigenvectors.loc[1, :] #
c = eigenvectors.loc[2, :] #
d = eigenvectors.loc[3, :] #
x = (ids_tab*a).sum(axis = 1)
y = (ids_tab*b).sum(axis = 1)
z = (ids_tab*c).sum(axis = 1)
g = (ids_tab*d).sum(axis = 1)
#fig = plt.figure()
#ax = fig.add_subplot(111, projection='3d')
index = random.sample(ids_tab.index, 30000)
#ax.scatter(x[index], y[index], z[index], c='r', marker='x') # 3D
plt.scatter(y[index], z[index], c='r', alpha=0.3) # 2D
plt.show()
assert False
ids_tab_for_input = ids_tab[ids_tab.sum(axis = 1) >= 2]
# Make actual word2vec (take out additional value ie 'johnny has toys' --> johnny predicts has and toys)
print 'here2'
#ids_tab = ids_tab[ids_tab.sum(axis = 1) > 1]
input_table = pd.DataFrame()
count = 0
import datetime
tic = datetime.datetime.now()
for ind, row in ids_tab.iloc[:20000].iterrows():
if count%100 == 0:
print 'Did', count, 'in', datetime.datetime.now() - tic
tic = datetime.datetime.now()
count += 1
input_table = input_table.append(_make_train_set(row))
input_table.index = range(len(input_table))
input_table = input_table[input_table.sum(axis = 1) > 1]
print 'here1'
train_type = 'word2vec_ish' # 'autoencoder' or ''word2vec-ish'
pred_cols = ids_tab.columns
context_cols = ['context_' + col for col in pred_cols]
if train_type == 'autoencoder':
ds = pandas_pybrain.make_pybrain_ds(ids_tab.iloc[:2000], pred_cols, pred_cols, normalise = False)
elif train_type == 'word2vec_ish':
ds = pandas_pybrain.make_pybrain_ds(input_table, pred_cols, context_cols, normalise = False)
print 'here'
n_nodes = 10
hiddenclass = 'SigmoidLayer'
learningrate = 0.05
# Build Network
net = buildNetwork(ds.indim, n_nodes, ds.outdim, bias = True, hiddenclass = eval(hiddenclass))
trainer = BackpropTrainer(net, dataset=ds, learningrate= learningrate, lrdecay=1.0, momentum=0.0, verbose=False, batchlearning=False, weightdecay=0.0)
# Train Network
for epoch in range(10):
print epoch
trainer.train()
vec_table = pd.DataFrame(index = pred_cols, columns = range(n_nodes))
for i, col in enumerate(pred_cols):
inpt = [False]*i + [True] + [False]*(len(pred_cols) - 1 - i)
net.activate(inpt)
vec = net['hidden0'].outputbuffer[net['hidden0'].offset]
vec_table.loc[col] = list(vec)
distance_tab = pd.DataFrame(columns = pred_cols, index = pred_cols)
for col in pred_cols:
for index in pred_cols:
distance_tab.loc[index, col] = cosine(vec_table.loc[index], vec_table.loc[col])
import matplotlib.pyplot as plt
plt.pcolor(distance_tab)
plt.yticks(np.arange(0.5, len(distance_tab.index), 1), distance_tab.index)
plt.xticks(np.arange(0.5, len(distance_tab.columns), 1), distance_tab.columns)
plt.show()
assert False
num_following = ids_tab.groupby('id').size()
num_following.name = 'num_following'
weight = 1/num_following
weight.name = 'weight'
#ids_tab = ids_tab.join(num_following, on = 'id')
#assert False
#for politique in ['fhollande', 'nicolassarkozy']:
# f = open(join(path_ids_politiques_fr, politique + '_ids.txt'))
# list_ids_tab_politique = f.read().split('\n')
# ids_tab_politique = pd.DataFrame()
# ids_tab_politique['id'] = list_ids_tab_politique
# ids_tab_politique[politique] = True
# f.close()
#
# ids_tab = ids_tab.merge(ids_tab_politique, on = 'id', how = 'left')
# ids_tab[politique] = ids_tab[politique].fillna(False)
#
#assert False
#
#b = a.groupby('artist')[news].mean()
#b.sort()
#for i in range(len(b)):
# print b.index[i], ' >> ', b.iloc[i]
#
#
#d = a.groupby('id').size()
#e = pd.DataFrame()
#e['count'] = d
#a = a.merge(e, left_on = 'id', right_index = True)
affinity_mat = pd.DataFrame()
grp = ids_tab.groupby('politique')
for (key_x, key_y) in itertools.combinations(ids_tab.politique.unique(), 2):
print (key_x, key_y)
x = grp.get_group(key_x)
y = grp.get_group(key_y)
z = pd.merge(x, y, how = 'inner', on = 'id')
z = z.join(weight, on = 'id')
val = z.weight.sum()
affinity_mat.loc[key_x, key_y] = val
affinity_mat.loc[key_y, key_x] = val
#for (key_x, key_y) in itertools.combinations(ids_tab.politique.unique(), 2):
for key_x in ids_tab.politique.unique():
print key_x
x = grp.get_group(key_x)
z = x.join(weight, on = 'id')
affinity_mat.loc[key_x, key_x] = z.weight.sum()
print affinity_mat
assert False
divider_mat = pd.DataFrame(columns = affinity_mat.columns, index = affinity_mat.index)
for x in divider_mat.index:
for y in divider_mat.columns:
divider_mat.loc[x, y] = sqrt(affinity_mat.loc[y, y]**2)# + affinity_mat.loc[x, x]**2 #, affinity_mat.loc[y, y])
a = affinity_mat / divider_mat
columns = ['jlmelenchon', 'cecileduflot', u'evajoly', 'fleurpellerin', 'eelv','benoithamon', 'montebourg', 'chtaubira', 'anne_hidalgo', 'najatvb', 'harlemdesir','martineaubry', 'fhollande', 'royalsegolene', 'manuelvalls', 'partisocialiste', 'bayrou', 'modem', u'datirachida', u'alainjuppe', u'francoisfillon', u'jf_cope', u'nk_m', u'christineboutin', u'bruno_lemaire', 'vpecresse','nadine__morano',u'nicolassarkozy', 'ump', u'mlp_officiel', u'fn_officiel']
a = a.loc[columns, columns]
for i in range(len(a)):
a.iloc[i, i] = np.nan
b = (a + a.T)/2
b = (b.T + b)/2
#for x in range(len(b)):
# for y in range(len(b)):
# if x > y:
# b.iloc[x, y] = ''
#print a
#plt.pcolor(a)
#plt.xticks(range(len(a)), list(a.columns), rotation = 45)
#plt.yticks(range(len(a)), list(a.columns))
#plt.show()
#
#def sorted(s, num):
# tmp = s.order(ascending=False)[:num]
# tmp.index = range(num)
# return tmp
for y in range(len(a)):
print b.index[y], ' ',b.apply(lambda x: x.argmax()).iloc[y]
from sklearn.cluster import SpectralClustering
clustering = SpectralClustering()