pySpark Columnsimilarities 的问题
Problems with pySpark Columnsimilarities
tl;博士
如何使用 pySpark 比较行的相似度?
我有一个 numpy 数组,我想在其中比较每一行的相似性
print (pdArray)
#[[ 0. 1. 0. ..., 0. 0. 0.]
# [ 0. 0. 3. ..., 0. 0. 0.]
# [ 0. 0. 0. ..., 0. 0. 7.]
# ...,
# [ 5. 0. 0. ..., 0. 1. 0.]
# [ 0. 6. 0. ..., 0. 0. 3.]
# [ 0. 0. 0. ..., 2. 0. 0.]]
使用 scipy 我可以计算余弦相似度如下...
pyspark.__version__
# '2.2.0'
from sklearn.metrics.pairwise import cosine_similarity
similarities = cosine_similarity(pdArray)
similarities.shape
# (475, 475)
print(similarities)
array([[ 1.00000000e+00, 1.52204908e-03, 8.71545594e-02, ...,
3.97681174e-04, 7.02593036e-04, 9.90472253e-04],
[ 1.52204908e-03, 1.00000000e+00, 3.96760121e-04, ...,
4.04724413e-03, 3.65324300e-03, 5.63519735e-04],
[ 8.71545594e-02, 3.96760121e-04, 1.00000000e+00, ...,
2.62367141e-04, 1.87878869e-03, 8.63876439e-06],
...,
[ 3.97681174e-04, 4.04724413e-03, 2.62367141e-04, ...,
1.00000000e+00, 8.05217639e-01, 2.69724702e-03],
[ 7.02593036e-04, 3.65324300e-03, 1.87878869e-03, ...,
8.05217639e-01, 1.00000000e+00, 3.00229809e-03],
[ 9.90472253e-04, 5.63519735e-04, 8.63876439e-06, ...,
2.69724702e-03, 3.00229809e-03, 1.00000000e+00]])
因为我希望扩展到比我原来的(475 行)矩阵大得多的集合,所以我正在考虑通过 pySpark 使用 Spark
from pyspark.mllib.linalg.distributed import RowMatrix
#load data into spark
tempSpark = sc.parallelize(pdArray)
mat = RowMatrix(tempSpark)
# Calculate exact similarities
exact = mat.columnSimilarities()
exact.entries.first()
# MatrixEntry(128, 211, 0.004969676943490767)
# Now when I get the data out I do the following...
# Convert to a RowMatrix.
rowMat = approx.toRowMatrix()
t_3 = rowMat.rows.collect()
a_3 = np.array([(x.toArray()) for x in t_3])
a_3.shape
# (488, 749)
如您所见,数据的形状是 a) 不再是正方形(它应该是正方形 b)的尺寸与原始行数不匹配...现在它确实匹配(在 part_ the每行中的特征数 (len(pdArray[0]) = 749) 但我不知道 488 来自哪里
749 的存在让我觉得我需要先转置我的数据。对吗?
最后,如果是这种情况,为什么尺寸不是 (749, 749)?
首先,columnSimilarities
方法仅 returns 相似矩阵上三角部分的非对角线条目。由于沿对角线缺少 1,因此在生成的相似性矩阵中,整行可能都是 0。
其次,pyspark RowMatrix
没有有意义的行索引。所以基本上当从 CoordinateMatrix
转换为 RowMatrix
时,MatrixEntry
中的 i
值被映射到任何方便的值(可能是一些递增索引)。所以可能发生的情况是全 0 的行被忽略,当您将矩阵转换为 RowMatrix
时,矩阵被垂直挤压。
用columnSimilarities
方法计算后立即检查相似矩阵的维数可能是有意义的。您可以使用 numRows()
和 numCols()
方法来完成此操作。
print(exact.numRows(),exact.numCols())
除此之外,听起来您确实需要转置矩阵以获得正确的向量相似度。此外,如果出于某种原因您需要以类似 RowMatrix
的形式使用它,您可以尝试使用 IndexedRowMatrix
它确实具有有意义的行索引并且会在转换时保留原始 CoordinateMatrix 中的行索引.
tl;博士 如何使用 pySpark 比较行的相似度?
我有一个 numpy 数组,我想在其中比较每一行的相似性
print (pdArray)
#[[ 0. 1. 0. ..., 0. 0. 0.]
# [ 0. 0. 3. ..., 0. 0. 0.]
# [ 0. 0. 0. ..., 0. 0. 7.]
# ...,
# [ 5. 0. 0. ..., 0. 1. 0.]
# [ 0. 6. 0. ..., 0. 0. 3.]
# [ 0. 0. 0. ..., 2. 0. 0.]]
使用 scipy 我可以计算余弦相似度如下...
pyspark.__version__
# '2.2.0'
from sklearn.metrics.pairwise import cosine_similarity
similarities = cosine_similarity(pdArray)
similarities.shape
# (475, 475)
print(similarities)
array([[ 1.00000000e+00, 1.52204908e-03, 8.71545594e-02, ...,
3.97681174e-04, 7.02593036e-04, 9.90472253e-04],
[ 1.52204908e-03, 1.00000000e+00, 3.96760121e-04, ...,
4.04724413e-03, 3.65324300e-03, 5.63519735e-04],
[ 8.71545594e-02, 3.96760121e-04, 1.00000000e+00, ...,
2.62367141e-04, 1.87878869e-03, 8.63876439e-06],
...,
[ 3.97681174e-04, 4.04724413e-03, 2.62367141e-04, ...,
1.00000000e+00, 8.05217639e-01, 2.69724702e-03],
[ 7.02593036e-04, 3.65324300e-03, 1.87878869e-03, ...,
8.05217639e-01, 1.00000000e+00, 3.00229809e-03],
[ 9.90472253e-04, 5.63519735e-04, 8.63876439e-06, ...,
2.69724702e-03, 3.00229809e-03, 1.00000000e+00]])
因为我希望扩展到比我原来的(475 行)矩阵大得多的集合,所以我正在考虑通过 pySpark 使用 Spark
from pyspark.mllib.linalg.distributed import RowMatrix
#load data into spark
tempSpark = sc.parallelize(pdArray)
mat = RowMatrix(tempSpark)
# Calculate exact similarities
exact = mat.columnSimilarities()
exact.entries.first()
# MatrixEntry(128, 211, 0.004969676943490767)
# Now when I get the data out I do the following...
# Convert to a RowMatrix.
rowMat = approx.toRowMatrix()
t_3 = rowMat.rows.collect()
a_3 = np.array([(x.toArray()) for x in t_3])
a_3.shape
# (488, 749)
如您所见,数据的形状是 a) 不再是正方形(它应该是正方形 b)的尺寸与原始行数不匹配...现在它确实匹配(在 part_ the每行中的特征数 (len(pdArray[0]) = 749) 但我不知道 488 来自哪里
749 的存在让我觉得我需要先转置我的数据。对吗?
最后,如果是这种情况,为什么尺寸不是 (749, 749)?
首先,columnSimilarities
方法仅 returns 相似矩阵上三角部分的非对角线条目。由于沿对角线缺少 1,因此在生成的相似性矩阵中,整行可能都是 0。
其次,pyspark RowMatrix
没有有意义的行索引。所以基本上当从 CoordinateMatrix
转换为 RowMatrix
时,MatrixEntry
中的 i
值被映射到任何方便的值(可能是一些递增索引)。所以可能发生的情况是全 0 的行被忽略,当您将矩阵转换为 RowMatrix
时,矩阵被垂直挤压。
用columnSimilarities
方法计算后立即检查相似矩阵的维数可能是有意义的。您可以使用 numRows()
和 numCols()
方法来完成此操作。
print(exact.numRows(),exact.numCols())
除此之外,听起来您确实需要转置矩阵以获得正确的向量相似度。此外,如果出于某种原因您需要以类似 RowMatrix
的形式使用它,您可以尝试使用 IndexedRowMatrix
它确实具有有意义的行索引并且会在转换时保留原始 CoordinateMatrix 中的行索引.