Module: SVMKit::Utils

Defined in:
lib/svmkit/utils.rb

Overview

Module for utility methods.

Class Method Summary collapse

Class Method Details

.dump_nmatrix(mat) ⇒ Hash

Dump an NMatrix object converted to a Ruby Hash.

Parameters:

  • mat (NMatrix)

    An NMatrix object converted to a Ruby Hash.

Returns:

  • (Hash)

    A Ruby Hash containing matrix information.



9
10
11
12
# File 'lib/svmkit/utils.rb', line 9

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 = {}) ⇒ NMatrix

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

Parameters:

  • dmp (Hash) (defaults to: {})

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

Returns:

  • (NMatrix)

    An NMatrix object restored from the given Hash.



18
19
20
21
# File 'lib/svmkit/utils.rb', line 18

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