Exemplo n.º 1
0
def calculate_auc_Node2Vec(train_set, poss_set, neg_set, d=10, directed=False):
    fout = open('node2vec.in', 'w')

    for edge in train_set:
        if edge not in poss_set:
            u, w = map(int, edge.split())
            fout.write(str(u) + ' ' + str(w) + '\n')
    fout.close()

    if not directed:
        print os.system(
            'python ../node2vec/src/main.py --input node2vec.in --output node2vec.out '
            + '--dimensions %d --walk-length 80 --p 1 --iter 1' % d)
    else:
        print os.system(
            'python ../node2vec/src/main.py --input node2vec.in --output node2vec.out '
            + '--dimensions %d --walk-length 80 --p 1 --iter 1 --directed' % d)

    w2v = {}
    with open('node2vec.out') as fin:
        print(fin.readline())
        for line in fin:
            line = list(map(float, line.strip().split()))
            w2v[int(line[0])] = np.array(line[1:])

    features = tools.Node2VecFeatures(w2v)
    X, Y = tools.make_dataset(poss_set, neg_set, [features.score])
    return roc_auc_score(Y, X)
Exemplo n.º 2
0
def calculate_auc(train_set, nodes, poss_set, neg_set, auc, gap, verbose,
                  directed, bipartite, node2vec, max_iter, p):
    g = Graph(directed=False)

    g.add_vertex(max(nodes) + 1)
    for edge in train_set:
        if edge not in poss_set:
            u, w = map(int, edge.split())
            g.add_edge(g.vertex(u), g.vertex(w))

    add_auc(auc, "sfdp-default",
            calculate_auc_default(g, max_iter, poss_set, neg_set, p, directed))

    if not directed:
        add_auc(auc, "PA", calculate_auc_PA(g, poss_set, neg_set))
        add_auc(auc, "CN", calculate_auc_CN(g, poss_set, neg_set))
        add_auc(auc, "Adamic-Adar",
                calculate_auc_Adamic_Adar(g, poss_set, neg_set))

    add_auc(
        auc, "NMF-100",
        calculate_auc_NMF(100, train_set, nodes, poss_set, neg_set, directed))
    add_auc(
        auc, "svds-100",
        calculate_auc_SVDS(100, train_set, nodes, poss_set, neg_set, directed))

    if bipartite:
        is_bi, part = graph_tool.topology.is_bipartite(g, partition=True)
        if is_bi:
            groups = g.new_vertex_property("int")
            for u in g.vertices():
                groups[u] = int(part[u])
            left = "repulse-aliens"
            right = "repulse-aliens"
            pos_bip = sfdp_layout(g,
                                  groups=groups,
                                  verbose=verbose,
                                  bipartite=True,
                                  bipartite_method=[left, right],
                                  gap=gap)
            features = tools.TopologicalFeatures(g, pos_bip, gap=gap)
            X, Y = tools.make_dataset(poss_set, neg_set, [features.dist])
            add_auc(auc, "sfdp-bipartite", roc_auc_score(Y, X))

    if directed:
        g_di = Graph(directed=False)
        g_di.add_vertex(2 * max(nodes) + 2)
        for edge in train_set:
            if edge not in poss_set:
                u, w = map(int, edge.split())
                g_di.add_edge(g_di.vertex(2 * u + 1), g_di.vertex(2 * w))
        add_auc(auc, "sfdp-directed",
                calculate_auc_directed(g_di, verbose, gap, poss_set, neg_set))

    if node2vec:
        add_auc(
            auc, 'Node2Vec-100-undir',
            calculate_auc_Node2Vec(train_set, poss_set, neg_set, 100, False))
Exemplo n.º 3
0
def calculate_auc_NMF(n_components, train_set, nodes, poss_set, neg_set,
                      directed):
    from sklearn.decomposition import NMF
    model = NMF(n_components=n_components, init='random', random_state=0)
    matrix = tools.make_sparse_matrix(train_set,
                                      nodes,
                                      poss_set,
                                      directed=directed)
    features = tools.MFFeatures(model, matrix)
    X, Y = tools.make_dataset(poss_set, neg_set, [features.score])
    return roc_auc_score(Y, X)
Exemplo n.º 4
0
def calculate_auc_PA(g, poss_set, neg_set):
    features = tools.TopologicalFeatures(g)
    edges = [e for e in g.edges()]
    poss_set = set(poss_set)
    neg_set = set(neg_set)
    for e in edges:
        e1 = str(e.source()) + ' ' + str(e.target())
        e2 = str(e.target()) + ' ' + str(e.source())

    X, Y = tools.make_dataset(poss_set, neg_set,
                              [features.preferential_attachment])
    return roc_auc_score(Y, X)
Exemplo n.º 5
0
def calculate_auc_directed(g, verbose, gap, poss_set, neg_set):
    groups = g.new_vertex_property("int")
    for u in g.vertices():
        groups[u] = int(u) % 2

    pos_directed = sfdp_layout(g,
                               groups=groups,
                               verbose=verbose,
                               bipartite=True,
                               gap=gap)

    features = tools.TopologicalFeatures(g,
                                         pos_directed,
                                         directed=True,
                                         gap=gap)
    X, Y = tools.make_dataset(poss_set, neg_set, [features.dist])
    return roc_auc_score(Y, X)
Exemplo n.º 6
0
def calculate_auc_SVDS(n_components, train_set, nodes, poss_set, neg_set,
                       directed):
    from scipy.sparse import linalg
    import numpy

    matrix = tools.make_sparse_matrix(train_set,
                                      nodes,
                                      poss_set,
                                      directed=directed)
    U, s, Vh = linalg.svds(matrix.asfptype(), k=n_components)
    U = U * s

    def score(u, w):
        return numpy.dot(U[u], Vh.T[w])

    X, Y = tools.make_dataset(poss_set, neg_set, [score])
    return roc_auc_score(Y, X)
Exemplo n.º 7
0
def calculate_auc_Adamic_Adar(g, poss_set, neg_set):
    features = tools.TopologicalFeatures(g)
    X, Y = tools.make_dataset(poss_set, neg_set,
                              [features.Adamic_Adar_coefficient])
    return roc_auc_score(Y, X)
Exemplo n.º 8
0
def calculate_auc_CN(g, poss_set, neg_set):
    features = tools.TopologicalFeatures(g)
    X, Y = tools.make_dataset(poss_set, neg_set, [features.common_neighbors])
    return roc_auc_score(Y, X)
Exemplo n.º 9
0
def calculate_auc_default(g, max_iter, poss_set, neg_set, p, directed):
    pos_default = sfdp_layout(g, max_iter=max_iter, p=p)

    features = tools.TopologicalFeatures(g, pos_default, directed=directed)
    X, Y = tools.make_dataset(poss_set, neg_set, [features.dist])
    return roc_auc_score(Y, X)