Grouped linear regression in Spark

I work at PySpark and I would like to find a way to perform linear regressions on data groups. In particular, this data block

import pandas as pd
pdf = pd.DataFrame({'group_id':[1,1,1,2,2,2,3,3,3,3],
                    'x':[0,1,2,0,1,5,2,3,4,5],
                    'y':[2,1,0,0,0.5,2.5,3,4,5,6]})
df = sqlContext.createDataFrame(pdf)

df.show()
# +--------+-+---+
# |group_id|x|  y|
# +--------+-+---+
# |       1|0|2.0|
# |       1|1|1.0|
# |       1|2|0.0|
# |       2|0|0.0|
# |       2|1|0.5|
# |       2|5|2.5|
# |       3|2|3.0|
# |       3|3|4.0|
# |       3|4|5.0|
# |       3|5|6.0|
# +--------+-+---+

Now I would like to be able to customize a separate model y ~ ax + bfor each group_id, and output a new framework with columns aand band a row for each group.

For example, for a group, 1I could do:

from sklearn import linear_model
# Regression on group_id = 1
data = df.where(df.group_id == 1).toPandas()
regr = linear_model.LinearRegression()
regr.fit(data.x.values.reshape(len(data),1), data.y.reshape(len(data),1))
a = regr.coef_[0][0]
b = regr.intercept_[0]
print('For group 1, y = {0}*x + {1}'.format(a, b))
# Repeat for group_id=2, group_id=3

But for this, for each group, it is necessary to return the data back to the driver, one of which will not use any Spark parallelism.

+6
source share
1 answer

. , , :

from pyspark.mllib.regression import LabeledPoint, SparseVector

# Label points for regression
def groupid_to_feature(group_id, x, num_groups):
    intercept_id = num_groups + group_id-1
    # Need a vector containing x and a '1' for the intercept term
    return SparseVector(num_groups*2, {group_id-1: x, intercept_id: 1.0})

labelled = df.map(lambda line:LabeledPoint(line[2],
                groupid_to_feature(line[0], line[1], 3)))

labelled.take(5)
# [LabeledPoint(2.0, (6,[0,3],[0.0,1.0])),
#  LabeledPoint(1.0, (6,[0,3],[1.0,1.0])),
#  LabeledPoint(0.0, (6,[0,3],[2.0,1.0])),
#  LabeledPoint(0.0, (6,[1,4],[0.0,1.0])),
#  LabeledPoint(0.5, (6,[1,4],[1.0,1.0]))]

Spark LinearRegressionWithSGD :

from pyspark.mllib.regression import LinearRegressionModel, LinearRegressionWithSGD
lrm = LinearRegressionWithSGD.train(labelled, iterations=5000, intercept=False)

group_id, ..

lrm.weights
# DenseVector([-1.0, 0.5, 1.0014, 2.0, 0.0, 0.9946])

DataFrame, a b :

pd.DataFrame(lrm.weights.reshape(2,3).transpose(), columns=['a','b'], index=[1,2,3])    
#           a              b
# 1 -0.999990   1.999986e+00
# 2  0.500000   5.270592e-11
# 3  1.001398   9.946426e-01
+7

Source: https://habr.com/ru/post/1617983/


All Articles