polars数据写入excel为什么很慢,有没有更好的办法? polars数据
本文档详细介绍了如何使用Polars库计算DataFrame中各列之间的余弦相似度,并方便以相关矩阵的形式表示。通过join_where方法生成列组合,利用Polars表达式计算余弦相似度,最后使用pivot方法将结果为矩阵形式,进行数据分析和挖掘。前提条件
其确保你已经安装了Polars库。可以使用pip进行安装:pip install Polars登录后复制数据准备
首先,我们创建一个Polars DataFrame,其中包含字符串列col1和列表列col2。col2列包含数值列表,我们将根据这些列表计算余弦相似度。import Polars as plfrom numpy.linalg importnormdata = { quot;col1quot;: [quot;aquot;, quot;bquot;, quot;cquot;, quot;dquot;], quot;col2quot;:[[-0.06066, 0.072485, 0.548874, 0.158507], [-0.536674, 0.10478, 0.926022, -0.083722], [-0.21311, -0.030623, 0.300583, 0.261814], [-0.308025, 0.006694, 0.176335, 0.533835]],}df = pl.DataFrame(data)print(df)登录后复制
输出:shape: (4, 2)┌──────┬────────────────────────────────┐│col1 ┆ col2 ││ --- ┆ --- │ str ┆ 列表[f64] │╞══════╪═════════════ ════════════════════╡│ a ┆ [-0.06066, 0.072485, … 0.15850… │ │ b ┆ [-0.536674, 0.10478, … -0.0837… │ c ┆ [-0.21311, -0.030623, … 0.2618… │ d ┆ [-0.308025, 0.006694, … 0.5338… │└──────┴────────────────────────────────┘登录后复制生成列组合
为了计算每对列之间的余弦相似度,我们需要生成所有可能的列组合。我们可以使用 join_where 方法来实现这一点。首先,添加一个行索引,然后使用join_where 将DataFrame与自身连接,条件是左边的索引小于右边的索引,居民重复计算。
df = df.with_row_index().lazy()combinations_df = df.join_where(df, pl.col(quot;indexquot;) lt;= pl.col(quot;index_rightquot;)).collect()print(combinations_df)登录后复制
输出:shape: (10, 6)┌────────┬──────┬────────────────────────────────┬────────────────┬────────────────────────┬────────────────────────────┐│ index ┆ col1 ┆ col2 ┆ index_right ┆ col1_right ┆ col2_right ││ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ││ u32 ┆ str ┆ list[f64] ┆ u32 ┆ str ┆ list[f64] │╞═══════╪═ ... … 0.15850… ││ 0 ┆ a ┆ [-0.06066, 0.072485, … 0.15850… ┆ 1 ┆ b ┆ [-0.536674, 0.10478, … -0.0837… ││ 0 ┆ a ┆ [-0.06066, 0.072485, … 0.15850… ┆ 2 ┆ c ┆ [-0.21311, -0.030623, … 0.2618… ││ 0 ┆ a ┆ [-0.06066, 0.072485, … 0.15850… ┆ 3 ┆ d ┆ [-0.308025, 0.006694, … 0.5338… ││ 1 ┆ b ┆ [-0.536674, 0.10478, … -0.0837… ┆ 1 ┆ b ┆ [-0.536674, 0.10478, … -0.0837… ││ 1 ┆ b ┆ [-0.536674, 0.10478, … -0.0837… ┆ 2 ┆ c ┆ [-0.21311, -0.030623, … 0
.2618… ││ 1 ┆ b ┆ [-0.536674, 0.10478, … -0.0837… ┆ 3 ┆ d ┆ [-0.308025, 0.006694, … 0.5338… ││ 2 ┆ c ┆ [-0.21311, -0.030623, … 0.2618… ┆ 2 ┆ c ┆ [-0.21311, -0.030623, … 0.2618… ││ 2 ┆ c ┆ [-0.21311, -0.030623, … 0.2618… ┆ 3 ┆ d ┆ [-0.308025, 0.006694, … 0.5338… │ 3 ┆ d ┆ [-0.308025, 0.006694, … 0.5338… ┆ 3 ┆ d ┆ [-0.308025, 0.006694, … 0.5338… │└────────┴──────┴────────────────────────────────┴──────────────┴────────────┴────────────┴──────────────────────────────┘登录后复制计算余弦相似度
定义了一个函数来计算两个之间的余弦相似度。利用Polars 表达式,我们可以高效计算余弦相似度。cosine_similarity = lambda x, y: ( (x * y).list.sum() / ( (x * x).list.sum().sqrt() * (y * y).list.sum().sqrt() ))登录后复制
现在,我们可以使用这个函数来计算每对列之间的余弦相似度。
通义万相
通义万相,一个不断演化的AI艺术创作大模型 596 查看详情 out = (combinations_df .select( col = quot;col1quot;, other = quot;col1_rightquot;, cosine = cosine_similarity( x = pl.col(quot;col2quot;), y = pl.col(quot;col2_rightquot;) ) ))print(out)登录后复制
输出:shape:(10, 3)┌─────┬──────┬──────────┐│ col ┆ other ┆ cosine │ │ --- ┆ --- ┆ --- │ │ str ┆ str ┆ f64 │╞═════╪═══════╪══════════╡│ ┆ a ┆ 1.0 ││ a ┆ b ┆ 0.856754 ││ a ┆ c ┆ 0.827877 ││ a ┆ d ┆ 0.540282 ││ b ┆ b ┆ 1.0 ││ b ┆ c ┆ 0.752199 ││ b ┆ d ┆ 0.411564 ││ c ┆ c ┆ 1.0 ││ c ┆ d ┆ 0.889009 ││ d ┆ d ┆ 1.0 │└──────┴────────┴──────────┘登录后转换复制为相关矩阵
为了将结果转换为相关矩阵的形式,我们需要将上面的结果进行透视。首先,我们需要将DataFrame中列和其他列进行互换,然后与原始的out DataFrame进行垂直拼接,最后使用pivot方法进行透视。
result = pl.concat( [ out, out.filter(pl.col(quot;colquot;) != pl.col(quot;otherquot;)).select(col=quot;otherquot;, other=quot;colquot;, cosine=quot;cosinequot;) ]).collect().pivot(values=quot;cosinequot;, index=quot;colquot;, columns=quot;otherquot;)print(result)登录后复制
输出:shape: (4, 5)┌──────┬──────────┬──────────┬──────────┬──────────┐│ col ┆ a ┆ b ┆ c ┆ d ││ --- ┆ --- ┆ --- ┆ --- ┆ --- ││ str ┆ f64 ┆ f64 ┆ f64 ┆ f64 │╞═════╪══════════════╪════════════════════════╪══════════════╡│ a ┆ 1.0 ┆ 0.856754 ┆ 0.827877 ┆ 0.540282 ││ b ┆ 0.856754 ┆ 1.0 ┆ 0.752199 ┆ 0.411564 ││ c ┆ 0.827877 ┆ 0.752199 ┆ 1.0 ┆ 0.889009 ││ d ┆ 0.540282 ┆ 0.411564 ┆ 0.889009 ┆ 1.0 │└──────┴──────────┴──────────┴──────────┴────────┘登录后复制
现在,结果DataFrame就是我们想要的相关矩阵,其中每个值表示对应列之间的余弦相似度。总结
本文档介绍了如何使用Polars库计算DataFrame中各列之间的余弦相似度,将其以相关矩阵的表现形式。通过join_where方法生成列组合,利用Polars表达式计算余弦相似度,最后使用pivot方法将结果转换为矩阵形式。这种方法可以考虑各种数据分析和挖掘任务,例如推荐系统、文本相似度计算等。
注意事项:确保你的Polars版本支持列表算术攻击。如果你的Polars版本低于1.8.0,请升级到最新版本。在处理大型DataFrame时,可以使用惰性求值 来提高性能。余弦收缩度是一种常用的收缩度收缩方法,但它只考虑保护之间的角度,而不考虑保护长度。在某些情况下,可能需要使用其他收缩度收缩方法。
以上就是利用Polars计算DataFrame的相关矩阵:余弦相似度方法详细解的详细内容,更多请关注乐哥常识网其他相关文章! 相关标签:cos pip 字符串数据分析