我对此主题有相同的疑问:
How to get the correlation matrix of a pyspark data frame?
“我有一个大的pyspark数据框。我想获取它的相关矩阵。我知道如何使用熊猫数据框来获取它。但是我的数据太大,无法转换为熊猫。所以我需要获取结果我搜索了其他类似的问题,但答案对我不起作用。有人可以帮助我吗?谢谢!”
df4是我的数据集,他有9列,它们都是整数:
reference__YM_unix:integer
tenure_band:integer
cei_global_band:integer
x_band:integer
y_band:integer
limit_band:integer
spend_band:integer
transactions_band:integer
spend_total:integer
我首先完成了这一步:
# convert to vector column first
vector_col = "corr_features"
assembler = VectorAssembler(inputCols=df4.columns, outputCol=vector_col)
df_vector = assembler.transform(df4).select(vector_col)
# get correlation matrix
matrix = Correlation.corr(df_vector, vector_col)
并获得以下输出:
(matrix.collect()[0]["pearson({})".format(vector_col)].values)
Out[33]: array([ 1. , 0.0760092 , 0.09051543, 0.07550633, -0.08058203,
-0.24106848, 0.08229602, -0.02975856, -0.03108094, 0.0760092 ,
1. , 0.14792512, -0.10744735, 0.29481762, -0.04490072,
-0.27454922, 0.23242408, 0.32051685, 0.09051543, 0.14792512,
1. , -0.03708623, 0.13719527, -0.01135489, 0.08706559,
0.24713638, 0.37453265, 0.07550633, -0.10744735, -0.03708623,
1. , -0.49640664, 0.01885793, 0.25877516, -0.05019079,
-0.13878844, -0.08058203, 0.29481762, 0.13719527, -0.49640664,
1. , 0.01080777, -0.42319841, 0.01229877, 0.16440178,
-0.24106848, -0.04490072, -0.01135489, 0.01885793, 0.01080777,
1. , 0.00523737, 0.01244241, 0.01811365, 0.08229602,
-0.27454922, 0.08706559, 0.25877516, -0.42319841, 0.00523737,
1. , 0.32888075, 0.21416322, -0.02975856, 0.23242408,
0.24713638, -0.05019079, 0.01229877, 0.01244241, 0.32888075,
1. , 0.53310864, -0.03108094, 0.32051685, 0.37453265,
-0.13878844, 0.16440178, 0.01811365, 0.21416322, 0.53310864,
1. ])
我试图将此结果插入数组或Excel文件中,但没有成功。 我做到了:
matrix2 = (matrix.collect()[0]["pearson({})".format(vector_col)])
然后,当我尝试显示此信息时出现以下错误:
display(matrix2)
Exception: ML model display does not yet support model type <class 'pyspark.ml.linalg.DenseMatrix'>.
我原本希望将列名从df4
插入回来,但没有成功,我读到我需要使用df4.columns,但我不知道它是如何工作的。
最后,我期望打印出我从中篇文章中看到的以下图表
但它也不起作用:
from sklearn.preprocessing import StandardScaler
stdsc = StandardScaler()
X_std = stdsc.fit_transform(df4.iloc[:,range(0,7)].values)
cov_mat =np.cov(X_std.T)
plt.figure(figsize=(10,10))
sns.set(font_scale=1.5)
hm = sns.heatmap(cov_mat,
cbar=True,
annot=True,
square=True,
fmt='.2f',
annot_kws={'size': 12},
cmap='coolwarm',
yticklabels=cols,
xticklabels=cols)
plt.title('Covariance matrix showing correlation coefficients', size = 18)
plt.tight_layout()
plt.show()
AttributeError: 'DataFrame' object has no attribute 'iloc'
我尝试将df4替换为matrix2,但也无法正常工作
答案 0 :(得分:1)
您可以使用以下内容以可操纵的形式获取相关矩阵:
matrix = matrix.toArray().tolist()
您可以从那里转换为数据框pd.DataFrame(matrix)
,该数据框将允许您绘制热图,或保存到excel等。