在Map Reduce中计算数据集的线性回归

3

假设我有一个以下的输入:

60,3.1

61,3.6 

62,3.8 

63,4 

65,4.1

期望的输出如下:

期望输出:y = -8.098 + 0.19x。

我知道如何在Java中实现,但不知道如何在MapReduce模型中实现。有没有人能够提供一些思路或这个问题的示例MapReduce代码?我会非常感激。

这是一个简单的数学示例:

Regression Formula:
Regression Equation(y) = a + bx 
Slope(b) = (NΣXY - (ΣX)(ΣY)) / (NΣX2 - (ΣX)2)
Intercept(a) = (ΣY - b(ΣX)) / N

where 
              x and y are the variables.
              b = The slope of the regression line 
              a = The intercept point of the regression line and the y axis. 
              N = Number of values or elements 
              X = First Score
              Y = Second Score
              ΣXY = Sum of the product of first and Second Scores
              ΣX = Sum of First Scores
              ΣY = Sum of Second Scores
              ΣX2 = Sum of square First Scores

e.g.

X Values   Y Values 
  60          3.1 
  61          3.6 
  62          3.8 
  63            4 
  65          4.1 

为了找到回归方程,我们首先需要找到斜率和截距,并使用它们来形成回归方程。
Step 1: Count the number of values.
            N = 5

  Step 2: Find XY, X2
            See the below table

X Value   Y Value          X*Y             X*X 
  60        3.1     60 * 3.1 = 186     60 * 60 = 3600 
  61        3.6     61 * 3.6 = 219.6   61 * 61 = 3721 
  62        3.8     62 * 3.8 = 235.6   62 * 62 = 3844 
  63          4     63 * 4 = 252       63 * 63 = 3969 
  65        4.1     65 * 4.1 = 266.5   65 * 65 = 4225 



  Step 3: Find ΣX, ΣY, ΣXY, ΣX2.
            ΣX = 311 
            ΣY = 18.6 
            ΣXY = 1159.7 
            ΣX2 = 19359 

  Step 4: Substitute in the above slope formula given.
            Slope(b) = (NΣXY - (ΣX)(ΣY)) / (NΣX2 - (ΣX)2)
            = ((5)*(1159.7)-(311)*(18.6))/((5)*(19359)-(311)2)
            = (5798.5 - 5784.6)/(96795 - 96721)
            = 13.9/74
            = 0.19 

  Step 5: Now, again substitute in the above intercept formula given.
            Intercept(a) = (ΣY - b(ΣX)) / N 
            = (18.6 - 0.19(311))/5
            = (18.6 - 59.09)/5
            = -40.49/5
            = -8.098

  Step 6: Then substitute these values in regression equation formula
            Regression Equation(y) = a + bx 
            = -8.098 + 0.19x.

假设我们想知道变量x = 64时的近似y值。那么我们可以将该值代入上述方程式中。

    Regression Equation(y) = a + bx 
    = -8.098 + 0.19(64).
    = -8.098 + 12.16
    = 4.06

1
我通过谷歌搜索找到了这个。由于线性回归是Hadoop中常见的任务,因此有很多相关资源。 - Artem Tsikiridis
你能推荐一些有帮助的资源吗? - Vijay_Shinde
http://nerdslearning.wordpress.com/2012/12/04/building-linear-regression-with-mapreduce-on-hadoop/ - Artem Tsikiridis
我已经看过这个实现了。这是不同类型的实现。还有其他类似于我的问题的吗? - Vijay_Shinde
1
LinkedIn的DataFu库有一个示例:http://engineering.linkedin.com/datafu/datafu-10 - dranxo
1个回答

1

我在这里使用mrjob编写了一个线性回归的Map-Reduce实现:

https://github.com/AmazaspShumik/MapReduce-Machine-Learning/blob/master/Linear%20Regression%20MapReduce/LinearRegressionTS.py

获取线性回归参数的两个最重要的组成部分是X.t * X(其中X是输入矩阵,每行为观察值)和X.t * Y。每个Mapper计算它处理的数据部分i的X_i.t * X_i和X_i.t * Y_i,然后所有Mappers将数据输出到单个Reducer。Reducer汇总所有Mappers的结果: X.t * X = Σi (X_i.t * X_i) X.t * Y = Σi (X_i.t * Y_i)
然后参数向量b =(X.t * X)^-1 * X.t * Y。 然而,更好的参数识别解决方案将使用Cholesky分解,就像代码中所做的那样。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接