Matrix 如何获得pyspark数据帧的相关矩阵?新2020
我在这个话题上有同样的问题: “我有一个大的pyspark数据框。我想得到它的相关矩阵。我知道如何用pandas数据框得到它。但是我的数据太大,无法转换成pandas。所以我需要用pyspark数据框得到结果。我搜索了其他类似的问题,答案对我不适用。有人能帮我吗?谢谢!” df4是我的数据集,他有9列,都是整数:Matrix 如何获得pyspark数据帧的相关矩阵?新2020,matrix,pyspark,correlation,pearson-correlation,Matrix,Pyspark,Correlation,Pearson Correlation,我在这个话题上有同样的问题: “我有一个大的pyspark数据框。我想得到它的相关矩阵。我知道如何用pandas数据框得到它。但是我的数据太大,无法转换成pandas。所以我需要用pyspark数据框得到结果。我搜索了其他类似的问题,答案对我不适用。有人能帮我吗?谢谢!” df4是我的数据集,他有9列,都是整数: reference__YM_unix:integer tenure_band:integer cei_global_band:integer x_band:integer y_ban
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文件中插入此结果,但没有成功。
我做到了:
当我试图显示此信息时,出现以下错误:
display(matrix2)
Exception: ML model display does not yet support model type <class 'pyspark.ml.linalg.DenseMatrix'>.
我尝试将df4替换为matrix2,但效果不太好您可以使用以下方法以可操作的形式获取相关矩阵:
matrix = matrix.toArray().tolist()
从那里,您可以转换为数据框pd.dataframe(matrix)
,这将允许您绘制热图,或保存到excel等
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'
matrix = matrix.toArray().tolist()