稀疏的矢量与密集的矢量

时间:2015-07-20 17:37:31

标签: apache-spark apache-spark-mllib

如何创建SparseVector和密集的矢量表示

如果DenseVector是:

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

稀疏矢量表示是什么?

2 个答案:

答案 0 :(得分:20)

除非我彻底误解了你的疑问,否则MLlib data type documentation非常清楚地说明了这一点:

<?php
exit; //I've only got this in here to test whether it's even reaching the Observer.php file. So far no luck.                                                                                                                                                                             
class Feature_Tooltip_Model_Observer 
{
   /**
    * Hook that allows us to edit the form that is used to create and/or edit attributes.
    * @param Varien_Event_Observer $observer
    */                         
   public function addFieldToAttributeEditForm($observer)
   {
       // Add an extra field to the base fieldset:
       $fieldset = $observer->getForm()->getElement('base_fieldset');
       $fieldset->addField('tooltip', 'text', array(
           'name' => 'tooltip',
           'label' => Mage::helper('core')->__('Tooltip'),
           'title' => Mage::helper('core')->__('Tooltip')
       ));
   }
}

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}); 的第二个参数是索引数组,第三个参数是这些索引中实际值的数组。

答案 1 :(得分:14)

稀疏向量是指向量中有很多值为零。虽然密集向量是指向量中的大多数值都不为零。

如果必须从指定的密集向量创建稀疏向量,请使用以下语法:

Vector sparseVector = Vectors.sparse(4, new int[] {1, 3}, new double[] {3.0, 4.0});