本篇文章为大家展示了计算PageRank的示例分析,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
Page Rank就是MapReduce的来源,下文是一个简单的计算PageRank的示例。
import java.text.DecimalFormat;
/**
* Created by jinsong.sun on 2014/7/15.
*/
public class PageRankCaculator {
public static void main(String[] args) {
double[][] g = calcG(genS(), 0.85);
double[] q = genQ();
int i = 0;
while (i++ < 100000) {
q = calcQ(g, q);
printQString(q);
}
}
public static double[][] genS() {
double[] linkA = {0.00, 0.50, 0.50, 0.00, 0.50};
double[] linkB = {0.25, 0.00, 0.00, 0.00, 0.00};
double[] linkC = {0.25, 0.00, 0.00, 1.00, 0.50};
double[] linkD = {0.25, 0.50, 0.50, 0.00, 0.00};
double[] linkE = {0.25, 0.00, 0.00, 0.00, 0.00};
return new double[][]{linkA, linkB, linkC, linkD, linkE};
}
public static double[] genQ() {
return new double[] {1.00, 1.00, 1.00, 1.00, 1.00};
}
/**
* 计算G矩阵。公式:G = α*S + (1-α)*(1/n)*U
*
* @param s 原始矩阵
* @param alpha 权重
* @return G矩阵
*/
public static double[][] calcG(double[][] s, double alpha) {
int size = 5;
//all one matrix
double[][] u = {{1.00, 1.00, 1.00, 1.00, 1.00}, {1.00, 1.00, 1.00, 1.00, 1.00}
, {1.00, 1.00, 1.00, 1.00, 1.00}, {1.00, 1.00, 1.00, 1.00, 1.00}
, {1.00, 1.00, 1.00, 1.00, 1.00}};
//计算a*S
double[][] m1 = new double[size][size];
for (int i = 0; i < s.length; i++) {
for (int j = 0; j < s[i].length; j++) {
m1[i][j] = s[i][j] * alpha;
}
}
//(1-α)*(1/n)*U
double[][] m2 = new double[size][size];
for (int i = 0; i < u.length; i++) {
for (int j = 0; j < u[i].length; j++) {
DecimalFormat df = new DecimalFormat("#.0000");
m2[i][j] = Double.parseDouble(df.format((1.0 - alpha) * (1.0 / size) * u[i][j]));
}
}
//G = α*S + (1-α)*(1/n)*U
double[][] m3 = new double[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
m3[i][j] = m1[i][j] + m2[i][j];
DecimalFormat df = new DecimalFormat("#.0000");
m3[i][j] = Double.parseDouble(df.format(m3[i][j]));
}
}
return m3;
}
/**
* 计算特征向量。公式:q_next = G * q_curr
*
* @param g G矩阵
* @param q 特征向量
* @return
*/
public static double[] calcQ(double[][] g, double[] q) {
double[] qNext = new double[g.length];
for (int i = 0; i < g.length; i++) {
for (int j = 0; j < g[i].length; j++) {
qNext[i] += g[i][j] * q[j];
}
}
return qNext;
}
public static void printQString(double[] m) {
String s = "{ {:p00}, {:p10}, {:p20}, {:p30}, {:p40} }";
for (int i = 0; i < 5; i++) {
s = s.replace(":p" + i + "0", String.valueOf(m[i]));
}
System.out.println(s);
}
}
上述内容就是计算PageRank的示例分析,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注天达云行业资讯频道。