コード例 #1
0
def compute_G(A, alpha=0.95):
    '''
        compute the pagerank transition Matrix G from addjacency matrix A, which solves both the sink node problem and the sing region problem.
        G[j][i] represents the probability of moving from node i to node j.
        If node i is a sink node, S[j][i] = 1/n.
        Input: 
                A: adjacency matrix, a (n by n) numpy matrix of binary values. If there is a link from node i to node j, A[j][i] =1. Otherwise A[j][i]=0 if there is no link.
                alpha: a float scalar value, which is the probability of choosing option 1 (randomly follow a link on the page)
        Output: 
                G: the transition matrix, a (n by n) numpy matrix of float values.  G[j][i] represents the probability of moving from node i to node j.
    The values in each column of matrix G should sum to 1.
    '''
    #########################################
    ## INSERT YOUR CODE HERE

    num_rows = np.shape(A)[0]
    num_cols = np.shape(A)[1]

    S = compute_S(A)
    S_full = np.full((num_cols, num_rows), (1. / num_cols))
    # (1/n)1 matrix

    G = alpha * S + (1 - alpha) * S_full
    # print("S: %s\nS_full: %s\nG: %s" % (S, S_full, G));

    #########################################

    return G
コード例 #2
0
ファイル: problem5.py プロジェクト: rahul-pande/ds501
def compute_G(A, alpha=0.95):
    '''
        compute the pagerank transition Matrix G from addjacency matrix A, which solves both the sink node problem and the sing region problem.
        G[j][i] represents the probability of moving from node i to node j.
        If node i is a sink node, S[j][i] = 1/n.
        Input:
                A: adjacency matrix, a (n by n) numpy matrix of binary values. If there is a link from node i to node j, A[j][i] =1. Otherwise A[j][i]=0 if there is no link.
                alpha: a float scalar value, which is the probability of choosing option 1 (randomly follow a link on the page)
        Output:
                G: the transition matrix, a (n by n) numpy matrix of float values.  G[j][i] represents the probability of moving from node i to node j.
    The values in each column of matrix G should sum to 1.
    '''
    #########################################
    ## INSERT YOUR CODE HERE

    S = compute_S(A)

    shape = set(np.shape(A))
    assert len(shape) == 1
    n = shape.pop()

    G = alpha * S + (1 - alpha) / n * np.ones((n, n))

    #########################################
    return G
コード例 #3
0
ファイル: problem5.py プロジェクト: aefernandez/DS501
def compute_G(A, alpha = 0.95):
    '''
        compute the pagerank transition Matrix G from addjacency matrix A, which solves both the sink node problem and the sing region problem.
        G[j][i] represents the probability of moving from node i to node j.
        If node i is a sink node, S[j][i] = 1/n.
        Input: 
                A: adjacency matrix, a (n by n) numpy matrix of binary values. If there is a link from node i to node j, A[j][i] =1. Otherwise A[j][i]=0 if there is no link.
                alpha: a float scalar value, which is the probability of choosing option 1 (randomly follow a link on the page)
        Output: 
                G: the transition matrix, a (n by n) numpy matrix of float values.  G[j][i] represents the probability of moving from node i to node j.
    The values in each column of matrix G should sum to 1.
    '''
    #########################################
    ## INSERT YOUR CODE HERE
    # Generate the new matrix with equal probabilities for every node
    n = A.shape[0]

    equal_probability_matrix = np.zeros(list(A.shape))
    equal_probability_matrix.fill(1/n)

    # Compute the transitional matrix with the sink node problem solved
    S = compute_S(A)

    # Combine the two matrices to form the final transitional matrix
    G = equal_probability_matrix * (1-alpha) + S * alpha

    #########################################
    return G
コード例 #4
0
def compute_G(A, alpha = 0.95):
    '''
        compute the pagerank transition Matrix G from addjacency matrix A, which solves both the sink node problem and the sink region problem.
        G[j][i] represents the probability of moving from node i to node j.
        If node i is a sink node, S[j][i] = 1/n.
        Input: 
                A: adjacency matrix, a (n by n) numpy matrix of binary values. If there is a link from node i to node j, A[j][i] =1. Otherwise A[j][i]=0 if there is no link.
                alpha: a float scalar value, which is the probability of choosing option 1 (randomly follow a link on the page)
        Output: 
                G: the transition matrix, a (n by n) numpy matrix of float values.  G[j][i] represents the probability of moving from node i to node j.
    The values in each column of matrix G should sum to 1.
    '''
    second_part = (1 - alpha) / A.shape[0]
    G = alpha * compute_S(A) + second_part

    return G
コード例 #5
0
def compute_G(A, alpha = 0.95):
    '''
        compute the pagerank transition Matrix G from addjacency matrix A, which solves both the sink node problem and the sing region problem.
        G[j][i] represents the probability of moving from node i to node j.
        If node i is a sink node, S[j][i] = 1/n.
        Input: 
                A: adjacency matrix, a (n by n) numpy matrix of binary values. If there is a link from node i to node j, A[j][i] =1. Otherwise A[j][i]=0 if there is no link.
                alpha: a float scalar value, which is the probability of choosing option 1 (randomly follow a link on the page)
        Output: 
                G: the transition matrix, a (n by n) numpy matrix of float values.  G[j][i] represents the probability of moving from node i to node j.
    The values in each column of matrix G should sum to 1.
    '''
    S = compute_S(A)
    # computing a matrix with equal probability entries for jumping from one node to another
    equaldist = np.full(A.shape,1/A.shape[1])
    G = alpha*S + (1 - alpha)*equaldist
    return G
コード例 #6
0
ファイル: problem5.py プロジェクト: vanand23/WPI_Projects
def compute_G(A, alpha=0.95):
    '''
        compute the pagerank transition Matrix G from addjacency matrix A, which solves both the sink node problem and the sing region problem.
        G[j][i] represents the probability of moving from node i to node j.
        If node i is a sink node, S[j][i] = 1/n.
        Input: 
                A: adjacency matrix, a (n by n) numpy matrix of binary values. If there is a link from node i to node j, A[j][i] =1. Otherwise A[j][i]=0 if there is no link.
                alpha: a float scalar value, which is the probability of choosing option 1 (randomly follow a link on the page)
        Output: 
                G: the transition matrix, a (n by n) numpy matrix of float values.  G[j][i] represents the probability of moving from node i to node j.
                    The values in each column of matrix G should sum to 1.
        Hint: you could solve this problem using 3 lines of code. You could re-use the functions that you have implemented in problem 3 and 4. 
    '''
    #########################################
    ## INSERT YOUR CODE HERE
    S = compute_S(A)
    n = A.shape[0]
    G = np.dot(alpha, S) + (1 - alpha) * (1 / n) * 1
    #########################################
    return G
    ''' TEST: Now you can test the correctness of your code above by typing `nosetests -v test5.py:test_compute_G' in the terminal.  '''
コード例 #7
0
ファイル: problem5.py プロジェクト: sevensail/105SD
def compute_G(A, alpha=0.95):
    '''
        compute the pagerank transition Matrix G from addjacency matrix A, which solves both the sink node problem and the sing region problem.
        G[j][i] represents the probability of moving from node i to node j.
        If node i is a sink node, S[j][i] = 1/n.
        Input:
                A: adjacency matrix, a (n by n) numpy matrix of binary values. If there is a link from node i to node j, A[j][i] =1.
                   Otherwise A[j][i]=0 if there is no link.
                alpha: a float scalar value, which is the probability of choosing option 1 (randomly follow a link on the page)
        Output:
                G: the transition matrix, a (n by n) numpy matrix of float values.
                   G[j][i] represents the probability of moving from node i to node j.
    The values in each column of matrix G should sum to 1.
    '''
    #########################################
    ## INSERT YOUR CODE HERE
    n = len(A)
    G = compute_S(A)
    for i in range(n):
        for j in range(n):
            G[j, i] = G[j, i] * (alpha) + (1 - alpha) / n
    #########################################
    return G
コード例 #8
0
ファイル: test_np.py プロジェクト: wwangrock/501
import numpy as np
from problem3 import random_walk, compute_P
from problem4 import compute_S
from problem5 import compute_G

A = np.mat([[0., 0.], [1., 0.]])

alpha = 0
S = compute_S(A)
L = np.asmatrix(np.ones((A.shape[0], A.shape[1]))) * 1 / float(A.shape[0])
G = S * alpha + L * (1 - alpha)

print(G)