将 pyspark 数据框中的两列相乘.其中之一包含一个向量,其中一个包含一个常数 [英] Multiplying two columns in a pyspark dataframe. One of them contains a vector and one of them contains a constant

查看:21
本文介绍了将 pyspark 数据框中的两列相乘.其中之一包含一个向量,其中一个包含一个常数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 pyspark 数据框,其中一列带有向量值,一列带有常量数值.比如说

I have a pyspark dataframe which has one Column with vector values and one column with constant numerical values. Say for example

A | B
1 | [2,4,5]
5 | [6,5,3] 

我想将向量列与常量列相乘.我试图这样做基本上是因为我在 B 列中有单词 wmbeddings,在 A 列中有一些权重.我的最终目的是获得加权嵌入.

I want to multiple the vector column with the constant column. I’m trying to do this basically cause I have word wmbeddings in the B column and some weights in the A column. And my final purpose to get weighted embeddings.

推荐答案

如果您的矢量数据存储为双精度数组,您可以这样做:

If your vector data is stored as an array of doubles, you can do this:

import breeze.linalg.{Vector => BV}

val data = spark.createDataset(Seq(
    (1, Array[Double](2, 4, 5)),
    (5, Array[Double](6, 5, 3))
  )).toDF("A", "B")

data.as[(Long, Array[Double])].map(r => {
  (BV(r._2) * r._1.toDouble).toArray
}).show()

变成了

+------------------+
|             value|
+------------------+
|   [2.0, 4.0, 5.0]|
|[30.0, 25.0, 15.0]|
+------------------+

这篇关于将 pyspark 数据框中的两列相乘.其中之一包含一个向量,其中一个包含一个常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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