稀疏向量与密集向量 [英] Sparse Vector vs Dense Vector

查看:64
本文介绍了稀疏向量与密集向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建稀疏向量和密集向量表示

How to create SparseVector and dense Vector representations

如果 DenseVector 是:

denseV = np.array([0., 3., 0., 4.])

稀疏向量表示将是什么?

What will be the Sparse Vector representation ?

推荐答案

除非我彻底误解了你的疑问,MLlib 数据类型文档 非常清楚地说明了这一点:

Unless I have thoroughly misunderstood your doubt, the MLlib data type documentation illustrates this quite clearly:

import org.apache.spark.mllib.linalg.Vector;
import org.apache.spark.mllib.linalg.Vectors;

// Create a dense vector (1.0, 0.0, 3.0).
Vector dv = Vectors.dense(1.0, 0.0, 3.0);
// Create a sparse vector (1.0, 0.0, 3.0) by specifying its indices and values corresponding to nonzero entries.
Vector sv = Vectors.sparse(3, new int[] {0, 2}, new double[] {1.0, 3.0});

其中 Vectors.sparse 的第二个参数是索引数组,第三个参数是这些索引中的实际值数组.

Where the second argument of Vectors.sparse is an array of the indices, and the third argument is the array of the actual values in those indices.

这篇关于稀疏向量与密集向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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