Class: LibSVMLoader
- Inherits:
-
Object
- Object
- LibSVMLoader
- Defined in:
- lib/libsvmloader.rb,
lib/libsvmloader/version.rb
Overview
LibSVMLoader loads (and dumps) dataset file with the libsvm file format.
Constant Summary collapse
- VERSION =
'0.1.3'.freeze
Class Method Summary collapse
-
.dump_libsvm_file(data, labels, filename, zero_based: false) ⇒ Object
Dump the dataset with the libsvm file format.
-
.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.
Class Method Details
.dump_libsvm_file(data, labels, filename, zero_based: false) ⇒ Object
Dump the dataset with the libsvm file format.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/libsvmloader.rb', line 39 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 |
# 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.read(filename).split("\n").each 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 [convert_to_nmatrix(ftvecs, n_features, value_dtype, stype), NMatrix.new([labels.size, 1], labels, dtype: label_dtype)] end |