第一周:PageRank學習心得--JAVA編程實現__IOS

來源:互聯網
上載者:User

最近複習Hadoop  發現之前很多筆記,貼出來很大家共用下。


Google 矩陣和Page Rank的簡單介紹  

                                                                                                                                                                       

  Page Rank是Google排名演算法法則的一部分,是Google用於標識網頁的等級/重要性的一種方法,是Google用來衡量一個網站好壞的標準。在揉合了諸如Title標識和Keywords標識等所有其它因素之後,Google通過PageRank來調整結果,使那些更具“等級/重要性”的網頁在搜尋結果中的排名獲得提升,從而提高搜尋結果的相關性和品質。其層級從0到10級,10級為滿分。PR值越高說明該網頁越重要。
    Google的PageRank根據網站的外部連結和內部連結的數量和品質來衡量網站的價值。
    [以上引自:百度百科,詳細見http://baike.baidu.com/view/1518.htm,就不多作介紹了]


矩陣概念相關

     相信很多人對於大學的“線性代數”忘記的差不多了;在Google矩陣的求解過程中我們要使用到的幾個基本的概念:特徵向量,矩陣的加法,矩陣的乘法。

    一個矩陣說穿了就是一個二維數組。一個n行m列的矩陣可以乘以一個m行p列的矩陣,得到的結果是一個n行p列的矩陣,其中的第i行第j列位置上的數等於前一個矩陣第i行上的m個數與後一個矩陣第j列上的m個數對應相乘後所有m個乘積的和。比如,下面的算式表示一個2行2列的矩陣乘以2行3列的矩陣,其結果是一個2行3列的矩陣

    如果你忘得差不多了(樓主也是),建議花兩三個小時複習下。推薦:http://wenku.baidu.com/view/3d8e80373968011ca30091c0  從第14頁(行列式)開始回憶。

    (題外話: 大家都在罵大學應試教育的不合理性,但是在這裡我們能發現:錯誤的是制度本身而不是知識。 對於知識的獲得:我學過忘了和我沒學過是有質的區別的---歡迎拍磚)

    假如你還是沒弄明白特徵向量的話,其實可以先掠過的,當作是一個N行1列的矩陣(哈哈本來就是),我們要通過這個矩陣來不停的進行q(n)=G*q(n-1)(G是pangRank矩陣,q是特徵向量) 的運算,直到q(n)=q(n-1),q(n)就是PR的值。

    參考的資料:http://www.cnblogs.com/itTeacher/archive/2013/06/08/3126914.html


用JAVA編程實現PageRank

package com.hadoop;public class GetPageRank {        // a是阻尼係數,Google取a等於0.85        private static float alpha = 0.85f;        public static void main(String[] args) {                try {                        // 轉移矩陣                        float[][] S = { { 0, 0, 0, 0 }, { 0.3333333f, 0, 0, 1 },                                        { 0.3333333f, 0.5f, 0, 0 }, { 0.3333333f, 0.5f, 1, 0 } };                        // 初始特徵向量                        float[][] U = { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 },                                        { 1, 1, 1, 1 } };                        float[][] f1 = multiGeneMatrix(alpha, S);// as                        float[][] f2 = multiGeneMatrix((1 - alpha) / S[1].length, U);// (1-a)/n*U                        // 擷取pageRank                        float[][] G = addMatrix(f1, f2);// aS+(1-a)/n*U                        // 列印矩陣內容                        printContentOfMatrix(G);                        // 特徵向量                        float[] pr_cur = { 1f, 1f, 1f, 1f };// result:0.15000004 1.492991                        // 0.82702124 1.5299894                                                int i = 0;                        // 求pageRank值  q(n)=G*q(n-1),直到q(n)=q(n-1),q(n)就是PR的值。                        while (true) {                                float[] pr_next = multiMatrixVector(G, pr_cur);                                if (compareMatrix(pr_cur, pr_next)) {                                        System.out.println("總共計算了:" + i + "次");                                        System.out.println(pr_next[0] + "--" + pr_next[1] + "--"                                                        + pr_next[2] + "--" + pr_next[3]);                                        break;                                } else {                                        i++;                                        pr_cur = pr_next;                                }                        }                } catch (Exception e) {                        System.out.println(e.getMessage());                }        }        // 矩陣與向量相乘        public static float[] multiMatrixVector(float[][] m, float[] v) {                float[] rv = null;                try {                        rv = new float[v.length];                        for (int vl = 0; vl < v.length; vl++) {                                for (int row = 0; row < m.length; row++) {                                        float one = 0;                                        for (int col = 0; col < m[1].length; col++) {                                                one += m[row][col] * v[col];                                        }                                        rv[row] = one;                                }                        }                } catch (Exception e) {                        System.out.println(e.getMessage());                }                return rv;        }        // 兩矩陣相加        public static float[][] addMatrix(float[][] f1, float[][] f2) {                float[][] result = null;                try {                        result = new float[f1.length][f1[1].length];                        for (int row = 0; row < f1.length; row++) {                                for (int col = 0; col < f1[1].length; col++) {                                        result[row][col] = f1[row][col] + f2[row][col];                                }                        }                } catch (Exception e) {                }                return result;        }        // 矩陣乘因子        public static float[][] multiGeneMatrix(float f, float[][] fm) {                float[][] result = null;                try {                        result = new float[fm.length][fm[1].length];                        for (int row = 0; row < fm.length; row++) {                                for (int col = 0; col < fm[1].length; col++) {                                        result[row][col] = fm[row][col] * f;                                }                        }                } catch (Exception e) {                }                return result;        }        // 列印矩陣內容        public static void printContentOfMatrix(float[][] f) {                try {                        System.out.println("--------------得到的Google矩陣如下---------------");                        for (int row = 0; row < f.length; row++) {                                for (int col = 0; col < f[1].length; col++) {                                        System.out.print(f[row][col] + " ");                                }                                System.out.println();                        }                        System.out                                        .println("----------------------------------------------");                } catch (Exception e) {                        System.out.println(e.getMessage());                }        }        // 比較兩個特徵向量        public static boolean compareMatrix(float[] now, float[] next) {                try {                        for (int i = 0; i < next.length; i++) {                                if (next - now > 0.0000001) {                                        return false;                                }                        }                } catch (Exception e) {                        System.out.println(e.getMessage());                }                return true;        }}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.