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

查看:104
本文介绍了在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天全站免登陆