Class: LibSVMLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/libsvmloader.rb,
lib/libsvmloader/version.rb

Overview

module LibSVMLoader

VERSION = "0.1.0"

end

Defined Under Namespace

Modules: VERSION

Class Method Summary collapse

Class Method Details

.dump_libsvm_file(data, labels, filename, zero_based: false) ⇒ Object

Dump the dataset with the libsvm file format.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/libsvmloader.rb', line 42

def dump_libsvm_file(data, labels, filename, zero_based: false)
  n_samples = [data.rows, labels.rows].min
  label_type = detect_dtype(labels)
  value_type = detect_dtype(data)
  File.open(filename, 'w') do |file|
    n_samples.times do |n|
      file.puts(dump_libsvm_line(labels[n], data.row(n),
                                 label_type, value_type, zero_based))
    end
  end
end

.load_libsvm_file(filename, zero_based: false, stype: :yale, label_dtype: :int32, value_dtype: :float64) ⇒ Array<NMatrix>

Load a dataset with the libsvm file format into NMatrix.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/libsvmloader.rb', line 19

def load_libsvm_file(filename, zero_based: false, stype: :yale,
                     label_dtype: :int32, value_dtype: :float64)
  ftvecs = []
  labels = []
  n_features = 0
  File.open(filename, 'r') do |file|
    file.each_line do |line|
      label, ftvec, max_idx = parse_libsvm_line(line, zero_based)
      labels.push(label)
      ftvecs.push(ftvec)
      n_features = [n_features, max_idx].max
    end
  end
  [convert_to_nmatrix(ftvecs, n_features, value_dtype, stype),
   NMatrix.new([labels.size, 1], labels, dtype: label_dtype)]
end