In [42]:
import sys
sys.path.append('../src/mane/prototype/')
import numpy as np
import graph as g
import pickle as p
from sklearn.preprocessing import normalize, scale
from sklearn.metrics import f1_score
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import LinearSVC
from sklearn.linear_model import LogisticRegression
In [2]:
# Load weight of experiment B3006 - Check embeddings folder for detail
with open('../src/mane/prototype/embeddings/BC3006.weights', 'rb') as f:
w = p.load(f)
# Get embedding weight and normalize
e_norm = normalize(w[0])
# Get average embedding and nce embedding
e_nce_norm = normalize((w[0] + w[1]) / 2)
In [3]:
# Load graph and get predictor
bc = g.graph_from_pickle('../src/mane/data/blogcatalog3.graph',
'../src/mane/data/blogcatalog3.community')
xids, y_train = bc.gen_community(0.5)
X_emb = [e_norm[i] for i in xids]
X_nce = [e_nce_norm[i] for i in xids]
predictor = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_emb, y_train)
predictor_nce = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_nce, y_train)
In [7]:
# Prediction using trained linear one vs rest model
y_true = [bc._communities[i] for i in bc.nodes()]
y_pred = [predictor.predict(e_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
y_pred_nce = [predictor_nce.predict(e_nce_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
In [8]:
f1_score(y_true, y_pred, average='macro')
Out[8]:
In [9]:
f1_score(y_true, y_pred_nce, average='macro')
Out[9]:
In [14]:
# Load weight of experiment B3007 - Check embeddings folder for detail
with open('../src/mane/prototype/embeddings/BC3007.weights', 'rb') as f:
w = p.load(f)
# Get embedding weight and normalize
e_norm = normalize(w[0])
# Get average embedding and nce embedding
e_nce_norm = normalize((w[0] + w[1]) / 2)
# Load graph and get predictor
bc = g.graph_from_pickle('../src/mane/data/blogcatalog3.graph',
'../src/mane/data/blogcatalog3.community')
xids, y_train = bc.gen_community(0.5)
X_emb = [e_norm[i] for i in xids]
X_nce = [e_nce_norm[i] for i in xids]
predictor = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_emb, y_train)
predictor_nce = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_nce, y_train)
# Prediction using trained linear one vs rest model
y_true = [bc._communities[i] for i in bc.nodes()]
y_pred = [predictor.predict(e_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
y_pred_nce = [predictor_nce.predict(e_nce_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
In [15]:
f1_score(y_true, y_pred, average='macro')
Out[15]:
In [16]:
f1_score(y_true, y_pred_nce, average='macro')
Out[16]:
In [17]:
# Load weight of experiment B3008 - Check embeddings folder for detail
with open('../src/mane/prototype/embeddings/BC3008.weights', 'rb') as f:
w = p.load(f)
# Get embedding weight and normalize
e_norm = normalize(w[0])
# Get average embedding and nce embedding
e_nce_norm = normalize((w[0] + w[1]) / 2)
# Load graph and get predictor
bc = g.graph_from_pickle('../src/mane/data/blogcatalog3.graph',
'../src/mane/data/blogcatalog3.community')
xids, y_train = bc.gen_community(0.5)
X_emb = [e_norm[i] for i in xids]
X_nce = [e_nce_norm[i] for i in xids]
predictor = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_emb, y_train)
predictor_nce = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_nce, y_train)
# Prediction using trained linear one vs rest model
y_true = [bc._communities[i] for i in bc.nodes()]
y_pred = [predictor.predict(e_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
y_pred_nce = [predictor_nce.predict(e_nce_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
In [18]:
print('f1_macro (emb): ', f1_score(y_true, y_pred, average='macro'))
print('f1_macro (nce): ', f1_score(y_true, y_pred_nce, average='macro'))
In [19]:
# Load weight of experiment B3009 - Check embeddings folder for detail
with open('../src/mane/prototype/embeddings/BC3009.weights', 'rb') as f:
w = p.load(f)
# Get embedding weight and normalize
e_norm = normalize(w[0])
# Get average embedding and nce embedding
e_nce_norm = normalize((w[0] + w[1]) / 2)
# Load graph and get predictor
bc = g.graph_from_pickle('../src/mane/data/blogcatalog3.graph',
'../src/mane/data/blogcatalog3.community')
xids, y_train = bc.gen_community(0.5)
X_emb = [e_norm[i] for i in xids]
X_nce = [e_nce_norm[i] for i in xids]
predictor = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_emb, y_train)
predictor_nce = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_nce, y_train)
# Prediction using trained linear one vs rest model
y_true = [bc._communities[i] for i in bc.nodes()]
y_pred = [predictor.predict(e_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
y_pred_nce = [predictor_nce.predict(e_nce_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
print('f1_macro (emb): ', f1_score(y_true, y_pred, average='macro'))
print('f1_macro (nce): ', f1_score(y_true, y_pred_nce, average='macro'))
In [21]:
# Load weight of experiment B3010 - Check embeddings folder for detail
with open('../src/mane/prototype/embeddings/BC3010.weights', 'rb') as f:
w = p.load(f)
# Get embedding weight and normalize
e_norm = normalize(w[0])
# Get average embedding and nce embedding
e_nce_norm = normalize((w[0] + w[1]) / 2)
# Load graph and get predictor
bc = g.graph_from_pickle('../src/mane/data/blogcatalog3.graph',
'../src/mane/data/blogcatalog3.community')
xids, y_train = bc.gen_community(0.5)
X_emb = [e_norm[i] for i in xids]
X_nce = [e_nce_norm[i] for i in xids]
predictor = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_emb, y_train)
predictor_nce = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_nce, y_train)
# Prediction using trained linear one vs rest model
y_true = [bc._communities[i] for i in bc.nodes()]
y_pred = [predictor.predict(e_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
y_pred_nce = [predictor_nce.predict(e_nce_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
print('f1_macro (emb): ', f1_score(y_true, y_pred, average='macro'))
print('f1_macro (nce): ', f1_score(y_true, y_pred_nce, average='macro'))
In [90]:
# Load weight of experiment B3003 - Check embeddings folder for detail
with open('../src/mane/prototype/embeddings/BC3003_b.weights', 'rb') as f:
w = p.load(f)
# Get embedding weight and normalize
e_norm = normalize(w[0])
# Get average embedding and nce embedding
e_nce_norm = normalize((w[0] + w[1]) / 2)
# Load graph and get predictor
bc = g.graph_from_pickle('../src/mane/data/blogcatalog3.graph',
'../src/mane/data/blogcatalog3.community')
xids, y_train = bc.gen_community(0.5)
X_emb = [e_norm[i] for i in xids]
X_nce = [e_nce_norm[i] for i in xids]
predictor = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_emb, y_train)
predictor_nce = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_nce, y_train)
# Prediction using trained linear one vs rest model
y_true = [bc._communities[i] for i in bc.nodes()]
y_pred = [predictor.predict(e_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
y_pred_nce = [predictor_nce.predict(e_nce_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
print('f1_macro (emb): ', f1_score(y_true, y_pred, average='macro'))
print('f1_macro (nce): ', f1_score(y_true, y_pred_nce, average='macro'))
In [33]:
# Load weight of experiment B3011 - Check embeddings folder for detail
with open('../src/mane/prototype/embeddings/BC3011.weights', 'rb') as f:
w = p.load(f)
# Get embedding weight and normalize
e_norm = normalize(w[0])
# Get average embedding and nce embedding
e_nce_norm = normalize((w[0] + w[1]) / 2)
# Load graph and get predictor
bc = g.graph_from_pickle('../src/mane/data/blogcatalog3.graph',
'../src/mane/data/blogcatalog3.community')
xids, y_train = bc.gen_community(0.9)
X_emb = [e_norm[i] for i in xids]
X_nce = [e_nce_norm[i] for i in xids]
predictor = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_emb, y_train)
predictor_nce = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_nce, y_train)
# Prediction using trained linear one vs rest model
y_true = [bc._communities[i] for i in bc.nodes()]
y_pred = [predictor.predict(e_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
y_pred_nce = [predictor_nce.predict(e_nce_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
print('f1_macro (emb): ', f1_score(y_true, y_pred, average='macro'))
print('f1_macro (nce): ', f1_score(y_true, y_pred_nce, average='macro'))
In [30]:
# Load weight of experiment B3012 - Check embeddings folder for detail
with open('../src/mane/prototype/embeddings/BC3012.weights', 'rb') as f:
w = p.load(f)
# Get embedding weight and normalize
e_norm = normalize(w[0])
# Get average embedding and nce embedding
e_nce_norm = normalize((w[0] + w[1]) / 2)
# Load graph and get predictor
bc = g.graph_from_pickle('../src/mane/data/blogcatalog3.graph',
'../src/mane/data/blogcatalog3.community')
xids, y_train = bc.gen_community(0.5)
X_emb = [e_norm[i] for i in xids]
X_nce = [e_nce_norm[i] for i in xids]
predictor = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_emb, y_train)
predictor_nce = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_nce, y_train)
# Prediction using trained linear one vs rest model
y_true = [bc._communities[i] for i in bc.nodes()]
y_pred = [predictor.predict(e_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
y_pred_nce = [predictor_nce.predict(e_nce_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
print('f1_macro (emb): ', f1_score(y_true, y_pred, average='macro'))
print('f1_macro (nce): ', f1_score(y_true, y_pred_nce, average='macro'))
In [32]:
# Load weight of experiment B3014 - Check embeddings folder for detail
with open('../src/mane/prototype/embeddings/BC3014.weights', 'rb') as f:
w = p.load(f)
# Get embedding weight and normalize
e_norm = normalize(w[0])
# Get average embedding and nce embedding
e_nce_norm = normalize((w[0] + w[1]) / 2)
# Load graph and get predictor
bc = g.graph_from_pickle('../src/mane/data/blogcatalog3.graph',
'../src/mane/data/blogcatalog3.community')
xids, y_train = bc.gen_community(0.5)
X_emb = [e_norm[i] for i in xids]
X_nce = [e_nce_norm[i] for i in xids]
predictor = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_emb, y_train)
predictor_nce = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_nce, y_train)
# Prediction using trained linear one vs rest model
y_true = [bc._communities[i] for i in bc.nodes()]
y_pred = [predictor.predict(e_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
y_pred_nce = [predictor_nce.predict(e_nce_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
print('f1_macro (emb): ', f1_score(y_true, y_pred, average='macro'))
print('f1_macro (nce): ', f1_score(y_true, y_pred_nce, average='macro'))
In [96]:
# Load weight of experiment B3015 - Check embeddings folder for detail
with open('../src/mane/prototype/embeddings/BC3015.weights', 'rb') as f:
w = p.load(f)
# Get embedding weight and normalize
e_norm = normalize(w[0])
# Get average embedding and nce embedding
e_nce_norm = normalize((w[0] + w[1]) / 2)
# Append Z
e_nce_norm_z = np.append(e_nce_norm, w[2], axis=1)
# Load graph and get predictor
bc = g.graph_from_pickle('../src/mane/data/blogcatalog3.graph',
'../src/mane/data/blogcatalog3.community')
xids, y_train = bc.gen_community(0.5)
X_emb = [e_norm[i] for i in xids]
X_nce = [e_nce_norm[i] for i in xids]
X_z = [e_nce_norm_z[i] for i in xids]
predictor = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_emb, y_train)
predictor_nce = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_nce, y_train)
predictor_z_svc = OneVsRestClassifier(LinearSVC(random_state=0)).fit(X_z, y_train)
# Prediction using trained linear one vs rest model
y_true = [bc._communities[i] for i in bc.nodes()]
y_pred = [predictor.predict(e_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
y_pred_nce = [predictor_nce.predict(e_nce_norm[i].reshape(1,-1))[0] for i in bc.nodes()]
y_pred_z = [predictor_z_svc.predict(e_nce_norm_z[i].reshape(1,-1))[0] for i in bc.nodes()]
print('f1_macro (emb): ', f1_score(y_true, y_pred, average='macro'))
print('f1_macro (nce): ', f1_score(y_true, y_pred_nce, average='macro'))
print('f1_micro (emb): ', f1_score(y_true, y_pred, average='micro'))
print('f1_micro (nce): ', f1_score(y_true, y_pred_nce, average='micro'))
print('f1_micro (emb_z): ', f1_score(y_true, y_pred_z, average='macro'))
print('f1_micro (nce_z): ', f1_score(y_true, y_pred_z, average='micro'))
In [98]:
predictor_lg_z = LogisticRegression().fit(X_z, y_train)
y_pred = [predictor_lg_z.predict(e_nce_norm_z[i].reshape(1,-1))[0] for i in bc.nodes()]
print('f1_macro (emb): ', f1_score(y_true, y_pred, average='macro'))
In [ ]: