您好,登錄后才能下訂單哦!
本篇文章為大家展示了計算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的示例分析,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。