Module: SVMKit::Utils

Defined in:
lib/svmkit/utils.rb

Overview

Module for utility methods.

Class Method Summary collapse

Class Method Details

.dump_nmatrix(mat) ⇒ Object

Dump an NMatrix object converted to a Ruby Hash. # call-seq:

dump_nmatrix(mat) -> Hash
  • Arguments :

    • mat – An NMatrix object converted to a Ruby Hash.

  • Returns :

    • A Ruby Hash containing matrix information.



13
14
15
16
# File 'lib/svmkit/utils.rb', line 13

def dump_nmatrix(mat)
  return nil if mat.class != NMatrix
  { shape: mat.shape, array: mat.to_flat_a, dtype: mat.dtype, stype: mat.stype }
end

.restore_nmatrix(dmp = {}) ⇒ Object

Return the results of converting the dumped data into an NMatrix object.

call-seq:

restore_nmatrix(dumped_mat) -> NMatrix
  • Arguments :

    • dumpted_mat – A Ruby Hash about NMatrix object created with SVMKit::Utils.dump_nmatrix method.

  • Returns :

    • An NMatrix object restored from the given Hash.



27
28
29
30
# File 'lib/svmkit/utils.rb', line 27

def restore_nmatrix(dmp = {})
  return nil unless dmp.class == Hash && %i[shape array dtype stype].all?(&dmp.method(:has_key?))
  NMatrix.new(dmp[:shape], dmp[:array], dtype: dmp[:dtype], stype: dmp[:stype])
end