数据挖掘第三次实验PageRank

问题:

整理出来的数据如下:

1 2
1 3
1 4
2 3
2 4
2 6
3 4
4 3
4 6
5 6
6 2
6 4

代码如下:

import numpy as np

if __name__ == '__main__':

    # 读入有向图,存储边
    f = open('input_1.txt', 'r')
    edges = [line.strip('\n').split(' ') for line in f]
    print(edges)

    # 根据边获取节点的集合
    nodes = []
    for edge in edges:
        if edge[0] not in nodes:
            nodes.append(edge[0])
        if edge[1] not in nodes:
            nodes.append(edge[1])
    print(nodes)

    N = len(nodes)

    # 将节点符号(字母),映射成阿拉伯数字,便于后面生成A矩阵/H矩阵
    i = 0
    node_to_num = {}
    for node in nodes:
        node_to_num[node] = i
        i += 1
    for edge in edges:
        edge[0] = node_to_num[edge[0]]
        edge[1] = node_to_num[edge[1]]
    print(edges)

    # 生成初步的S矩阵
    H = np.zeros([N, N])
    for edge in edges:
        H[edge[1], edge[0]] = 1
    print(H)

    # 计算比例:即一个网页对其他网页的PageRank值的贡献,即进行列的归一化处理
    for j in range(N):
        sum_of_col = sum(H[:, j])
        for i in range(N):
            H[i, j] /= sum_of_col
    print(H)

    # 计算矩阵A
    alpha = 0.85
    A = alpha * H + (1 - alpha) / N * np.ones([N, N])
    print(A)

    # 生成初始的PageRank值,记录在P_n中,P_n和P_n1均用于迭代
    P_n = np.ones(N) / N
    P_n1 = np.zeros(N)

    e = 100000  # 误差初始化
    k = 0  # 记录迭代次数
    print('loop...')

    while e > 0.00000001:  # 开始迭代
        P_n1 = np.dot(A, P_n)  # 迭代公式
        e = P_n1 - P_n
        e = max(map(abs, e))  # 计算误差
        P_n = P_n1
        k += 1
        print('iteration %s:' % str(k), P_n1)

    print('final result:', P_n)

运行结果如下:

E:\Python\A.venv\Scripts\python.exe E:\Python\viapython\data_mining\pagerank.py 
[['1', '2'], ['1', '3'], ['1', '4'], ['2', '3'], ['2', '4'], ['2', '6'], ['3', '4'], ['4', '3'], ['4', '6'], ['5', '6'], ['6', '2'], ['6', '4']]
['1', '2', '3', '4', '6', '5']
[[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [1, 4], [2, 3], [3, 2], [3, 4], [5, 4], [4, 1], [4, 3]]
[[0. 0. 0. 0. 0. 0.]
 [1. 0. 0. 0. 1. 0.]
 [1. 1. 0. 1. 0. 0.]
 [1. 1. 1. 0. 1. 0.]
 [0. 1. 0. 1. 0. 1.]
 [0. 0. 0. 0. 0. 0.]]
[[0.         0.         0.         0.         0.         0.        ]
 [0.33333333 0.         0.         0.         0.5        0.        ]
 [0.33333333 0.33333333 0.         0.5        0.         0.        ]
 [0.33333333 0.33333333 1.         0.         0.5        0.        ]
 [0.         0.33333333 0.         0.5        0.         1.        ]
 [0.         0.         0.         0.         0.         0.        ]]
[[0.025      0.025      0.025      0.025      0.025      0.025     ]
 [0.30833333 0.025      0.025      0.025      0.45       0.025     ]
 [0.30833333 0.30833333 0.025      0.45       0.025      0.025     ]
 [0.30833333 0.30833333 0.875      0.025      0.45       0.025     ]
 [0.025      0.30833333 0.025      0.45       0.025      0.875     ]
 [0.025      0.025      0.025      0.025      0.025      0.025     ]]
loop...
iteration 1: [0.025      0.14305556 0.19027778 0.33194444 0.28472222 0.025     ]
iteration 2: [0.025      0.15309028 0.21369213 0.3553588  0.2278588  0.025     ]
iteration 3: [0.025      0.12892332 0.2264864  0.35393721 0.24065307 0.025     ]
iteration 4: [0.025      0.13436089 0.21903492 0.3634026  0.23320159 0.025     ]
iteration 5: [0.025      0.13119401 0.22459836 0.35544261 0.23876502 0.025     ]
iteration 6: [0.025      0.13355847 0.22031808 0.36163871 0.23448475 0.025     ]
iteration 7: [0.025      0.13173935 0.22362135 0.35685128 0.23778802 0.025     ]
iteration 8: [0.025      0.13314324 0.22107128 0.36054754 0.23523794 0.025     ]
iteration 9: [0.025      0.13205946 0.22303995 0.35769396 0.23720662 0.025     ]
iteration 10: [0.025      0.13289615 0.22152011 0.35989696 0.23568678 0.025     ]
iteration 11: [0.025      0.13225022 0.22269345 0.35819622 0.23686011 0.025     ]
iteration 12: [0.025      0.13274888 0.22178762 0.35950921 0.23595429 0.025     ]
iteration 13: [0.025      0.13236391 0.22248693 0.35849557 0.2366536  0.025     ]
iteration 14: [0.025      0.13266111 0.22194706 0.35927811 0.23611372 0.025     ]
iteration 15: [0.025      0.13243167 0.22236384 0.35867398 0.23653051 0.025     ]
iteration 16: [0.025      0.1326088  0.22204208 0.35914037 0.23620875 0.025     ]
iteration 17: [0.025      0.13247205 0.22229049 0.35878031 0.23645715 0.025     ]
iteration 18: [0.025      0.13257762 0.22209871 0.35905828 0.23626538 0.025     ]
iteration 19: [0.025      0.13249612 0.22224676 0.35884369 0.23641343 0.025     ]
iteration 20: [0.025      0.13255904 0.22213247 0.35900936 0.23629913 0.025     ]
iteration 21: [0.025      0.13251047 0.22222071 0.35888146 0.23638737 0.025     ]
iteration 22: [0.025      0.13254797 0.22215258 0.3589802  0.23631925 0.025     ]
iteration 23: [0.025      0.13251902 0.22220517 0.35890397 0.23637184 0.025     ]
iteration 24: [0.025      0.13254137 0.22216457 0.35896282 0.23633124 0.025     ]
iteration 25: [0.025      0.13252411 0.22219592 0.35891739 0.23636258 0.025     ]
iteration 26: [0.025      0.13253743 0.22217172 0.35895246 0.23633839 0.025     ]
iteration 27: [0.025      0.13252715 0.2221904  0.35892538 0.23635707 0.025     ]
iteration 28: [0.025      0.13253509 0.22217598 0.35894629 0.23634265 0.025     ]
iteration 29: [0.025      0.13252896 0.22218711 0.35893015 0.23635378 0.025     ]
iteration 30: [0.025      0.13253369 0.22217852 0.35894261 0.23634518 0.025     ]
iteration 31: [0.025      0.13253004 0.22218515 0.35893299 0.23635182 0.025     ]
iteration 32: [0.025      0.13253286 0.22218003 0.35894041 0.2363467  0.025     ]
iteration 33: [0.025      0.13253068 0.22218399 0.35893468 0.23635065 0.025     ]
iteration 34: [0.025      0.13253236 0.22218093 0.35893911 0.2363476  0.025     ]
iteration 35: [0.025      0.13253106 0.22218329 0.35893569 0.23634996 0.025     ]
iteration 36: [0.025      0.13253206 0.22218147 0.35893833 0.23634814 0.025     ]
iteration 37: [0.025      0.13253129 0.22218287 0.35893629 0.23634954 0.025     ]
iteration 38: [0.025      0.13253189 0.22218179 0.35893786 0.23634846 0.025     ]
iteration 39: [0.025      0.13253143 0.22218263 0.35893665 0.23634929 0.025     ]
iteration 40: [0.025      0.13253178 0.22218198 0.35893759 0.23634865 0.025     ]
iteration 41: [0.025      0.13253151 0.22218248 0.35893686 0.23634915 0.025     ]
iteration 42: [0.025      0.13253172 0.22218209 0.35893742 0.23634876 0.025     ]
iteration 43: [0.025      0.13253156 0.22218239 0.35893699 0.23634906 0.025     ]
iteration 44: [0.025      0.13253168 0.22218216 0.35893732 0.23634883 0.025     ]
iteration 45: [0.025      0.13253159 0.22218234 0.35893707 0.23634901 0.025     ]
iteration 46: [0.025      0.13253166 0.2221822  0.35893727 0.23634887 0.025     ]
iteration 47: [0.025      0.1325316  0.22218231 0.35893711 0.23634898 0.025     ]
iteration 48: [0.025      0.13253165 0.22218223 0.35893723 0.23634889 0.025     ]
iteration 49: [0.025      0.13253161 0.22218229 0.35893714 0.23634896 0.025     ]
iteration 50: [0.025      0.13253164 0.22218224 0.35893721 0.23634891 0.025     ]
iteration 51: [0.025      0.13253162 0.22218228 0.35893716 0.23634895 0.025     ]
iteration 52: [0.025      0.13253164 0.22218225 0.3589372  0.23634892 0.025     ]
iteration 53: [0.025      0.13253162 0.22218227 0.35893717 0.23634894 0.025     ]
iteration 54: [0.025      0.13253163 0.22218226 0.35893719 0.23634892 0.025     ]
iteration 55: [0.025      0.13253163 0.22218227 0.35893717 0.23634894 0.025     ]
iteration 56: [0.025      0.13253163 0.22218226 0.35893719 0.23634892 0.025     ]
iteration 57: [0.025      0.13253163 0.22218227 0.35893717 0.23634893 0.025     ]
iteration 58: [0.025      0.13253163 0.22218226 0.35893718 0.23634893 0.025     ]
final result: [0.025      0.13253163 0.22218226 0.35893718 0.23634893 0.025     ]

进程已结束,退出代码0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值