Class: Datasets::LIBSVM

Inherits:
Dataset show all
Defined in:
lib/datasets/libsvm.rb

Defined Under Namespace

Classes: Record

Instance Attribute Summary

Attributes inherited from Dataset

#metadata

Instance Method Summary collapse

Methods inherited from Dataset

#clear_cache!, #to_table

Constructor Details

#initialize(name, note: nil, default_feature_value: 0) ⇒ LIBSVM

Returns a new instance of LIBSVM.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/datasets/libsvm.rb', line 34

def initialize(name,
               note: nil,
               default_feature_value: 0)
  super()
  @libsvm_dataset_metadata = fetch_dataset_info(name)
  @file = choose_file(note)
  @default_feature_value = default_feature_value
  @metadata.id = "libsvm-#{normalize_name(name)}"
  @metadata.name = "LIBSVM dataset: #{name}"
  @metadata.url = "https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/"
  @metadata.licenses = ["BSD-3-Clause"]
end

Instance Method Details

#eachObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/datasets/libsvm.rb', line 47

def each
  return to_enum(__method__) unless block_given?

  open_data do |input|
    n_features = @libsvm_dataset_metadata.n_features
    csv = CSV.new(input, col_sep: " ")
    csv.each do |row|
      label = parse_label(row.shift)
      features = [@default_feature_value] * n_features
      row.each do |column|
        next if column.nil?
        index, value = column.split(":", 2)
        features[Integer(index, 10) - 1] = parse_value(value)
      end
      yield(Record.new(label, features))
    end
  end
end