如何获取pyspark数据帧的相关矩阵? [英] How to get the correlation matrix of a pyspark data frame?

查看:131
本文介绍了如何获取pyspark数据帧的相关矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的pyspark数据框.我想得到它的相关矩阵.我知道如何使用熊猫数据框来获取它.但是我的数据太大,无法转换为熊猫.所以我需要用pyspark数据框获得结果.我搜索了其他类似的问题,答案对我不起作用. 有谁能够帮我?谢谢!

I have a big pyspark data frame. I want to get its correlation matrix. I know how to get it with a pandas data frame.But my data is too big to convert to pandas. So I need to get the result with pyspark data frame.I searched other similar questions, the answers don't work for me. Can any body help me? thanks!

数据示例: 数据示例

推荐答案

欢迎使用!

我准备了一些虚拟数据以便于复制(也许下次您也可以提供一些易于复制的数据;-)):

I prepared some dummy data for easier replication (perhaps next time you may supply some easy to copy data, too ;-)):

data = pd.DataFrame(np.random.random((10, 5)), 
                   columns=["x{}".format(x) for x in range(5)])
df = spark.createDataFrame(data)

df.show()

这是数据:

+-------------------+-------------------+-------------------+-------------------+--------------------+
|                 x0|                 x1|                 x2|                 x3|                  x4|
+-------------------+-------------------+-------------------+-------------------+--------------------+
| 0.9965335347601945|0.09311299224360992| 0.9273393764180728| 0.8523333283310564|  0.5040716744686445|
| 0.2341313103221958| 0.9356109544246494| 0.6377089480113576| 0.8129047787928055| 0.22215891357547046|
| 0.6310473705907303| 0.2040705293700683|0.17329601185489396| 0.9062007987480959| 0.44105687572209895|
|0.27711903958232764| 0.9434521502343274| 0.9300724702792151| 0.9916836130997986|  0.6869145183972896|
| 0.8247010263098201| 0.6029990758603708|0.07266306799434707| 0.6808038838294564| 0.27937146479120245|
| 0.7786370627473335|0.17583334607075107| 0.8467715537463528|   0.67702427694934|  0.8976402177586831|
|0.40620117097757724| 0.5080531043890719| 0.3722402520743703|0.14555317396545808|  0.7954133091360741|
|0.20876805543974553| 0.9755867281355178| 0.7570617946515066| 0.6974893162590945|0.054708580878511825|
|0.47979629269402546| 0.1851379589735923| 0.4786682088989791| 0.6809358266732168|  0.8829180507209633|
| 0.1122983875801804|0.45310988757198734| 0.4713203140134805|0.45333792855503807|  0.9189083355172629|
+-------------------+-------------------+-------------------+-------------------+--------------------+

解决方案

ml子包 pyspark.ml.stat .但是,它要求您提供类型为Vector的列.因此,您需要先使用 VectorAssembler将列转换为向量列,然后应用相关性:

Solution

There is a correlation function in the ml subpackage pyspark.ml.stat. However, it requires you to provide a column of type Vector. So you need to convert your columns into a vector column first using the VectorAssembler and then apply the correlation:

from pyspark.ml.stat import Correlation
from pyspark.ml.feature import VectorAssembler

# convert to vector column first
vector_col = "corr_features"
assembler = VectorAssembler(inputCols=df.columns, outputCol=vector_col)
df_vector = assembler.transform(df).select(vector_col)

# get correlation matrix
matrix = Correlation.corr(df_vector, vector_col)

如果要以numpy数组的形式获取结果(在驱动程序上),则可以使用以下命令:

If you want to get the result as a numpy array (on your driver), you can use the following:

matrix.collect()[0]["pearson({})".format(vector_col)].values

array([ 1.        , -0.66882741, -0.06459055,  0.21802534,  0.00113399,
       -0.66882741,  1.        ,  0.14854203,  0.09711389, -0.5408654 ,
       -0.06459055,  0.14854203,  1.        ,  0.33513733,  0.09001684,
        0.21802534,  0.09711389,  0.33513733,  1.        , -0.37871581,
        0.00113399, -0.5408654 ,  0.09001684, -0.37871581,  1.        ])

这篇关于如何获取pyspark数据帧的相关矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆