Class: Spark::Mllib::DenseVector

Inherits:
VectorBase show all
Defined in:
lib/spark/mllib/vector.rb

Overview

A dense vector represented by a value array.

Dense vector is a vector in which most of the elements are non-zero.

Example:

DenseVector.new([1,2,3,4,5]).values
# => [1, 2, 3, 4, 5]

DenseVector.new(1..5).values
# => [1, 2, 3, 4, 5]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from VectorAdapter

#[]=, #dot, new, #squared_distance, #values

Methods inherited from Vector

elements

Constructor Details

#initialize(values) ⇒ DenseVector

Returns a new instance of DenseVector.



59
60
61
# File 'lib/spark/mllib/vector.rb', line 59

def initialize(values)
  super(:dense, values.to_a)
end

Class Method Details

.from_java(object) ⇒ Object



94
95
96
# File 'lib/spark/mllib/vector.rb', line 94

def self.from_java(object)
  DenseVector.new(object.values)
end

.parse(data) ⇒ Object

Covert string to vector

DenseVector.parse("[1.0,2.0,3.0,4.0,5.0]")


67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/spark/mllib/vector.rb', line 67

def self.parse(data)
  unless data =~ /\[[0-9., ]+\]/
    raise ArgumentError, 'Unknow format for DenseVector.'
  end

  data.sub!('[', '')
  data.sub!(']', '')

  data = data.split(',')
  data.map!(&:to_f)

  DenseVector.new(data)
end

Instance Method Details

#marshal_dumpObject



98
99
100
# File 'lib/spark/mllib/vector.rb', line 98

def marshal_dump
  values
end

#marshal_load(array) ⇒ Object



102
103
104
# File 'lib/spark/mllib/vector.rb', line 102

def marshal_load(array)
  initialize(array)
end

#to_javaObject



90
91
92
# File 'lib/spark/mllib/vector.rb', line 90

def to_java
  JDenseVector.new(values)
end

#to_sObject

Convert vector to string

DenseVector.new([1,2,3,4,5]).to_s
# => "[1.0,2.0,3.0,4.0,5.0]"


86
87
88
# File 'lib/spark/mllib/vector.rb', line 86

def to_s
  "[#{values.join(',')}]"
end