Class: Svm::Scaler
- Inherits:
-
Object
- Object
- Svm::Scaler
- Defined in:
- lib/svm/scaler.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#num_features ⇒ Object
readonly
Returns the value of attribute num_features.
-
#num_samples ⇒ Object
readonly
Returns the value of attribute num_samples.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(data) ⇒ Scaler
constructor
A new instance of Scaler.
- #labels ⇒ Object
-
#release_data! ⇒ Object
Release references to data so it can be garbage collected.
- #save(path) ⇒ Object
- #scale(sample, with_labels = false) ⇒ Object
- #scale! ⇒ Object
- #unit ⇒ Object
- #values_for_feature(i) ⇒ Object
- #zero ⇒ Object
Constructor Details
#initialize(data) ⇒ Scaler
Returns a new instance of Scaler.
16 17 18 19 20 21 |
# File 'lib/svm/scaler.rb', line 16 def initialize(data) @data = data @num_samples = data.size @num_features = data.first.size - 1 end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/svm/scaler.rb', line 5 def data @data end |
#num_features ⇒ Object (readonly)
Returns the value of attribute num_features.
7 8 9 |
# File 'lib/svm/scaler.rb', line 7 def num_features @num_features end |
#num_samples ⇒ Object (readonly)
Returns the value of attribute num_samples.
6 7 8 |
# File 'lib/svm/scaler.rb', line 6 def num_samples @num_samples end |
Class Method Details
.load(path) ⇒ Object
78 79 80 |
# File 'lib/svm/scaler.rb', line 78 def self.load(path) YAML.load(File.read(path)) end |
.scale(data) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/svm/scaler.rb', line 9 def self.scale(data) instance = self.new(data) instance.scale! instance end |
Instance Method Details
#labels ⇒ Object
55 56 57 |
# File 'lib/svm/scaler.rb', line 55 def labels values_for_feature 0 end |
#release_data! ⇒ Object
Release references to data so it can be garbage collected
68 69 70 |
# File 'lib/svm/scaler.rb', line 68 def release_data! @data = nil end |
#save(path) ⇒ Object
72 73 74 75 76 |
# File 'lib/svm/scaler.rb', line 72 def save(path) File.open(path, 'w+') do |f| f.write(YAML.dump(self)) end end |
#scale(sample, with_labels = false) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/svm/scaler.rb', line 37 def scale(sample, with_labels = false) # If the sample does't have label it should be accessed # one more position to the left offset = with_labels ? 0 : -1 (1..num_features).each do |i| new_value = sample[i+offset].to_f - zero[i] new_value = new_value / unit[i] unless unit[i] == 0 sample[i+offset] = new_value end sample end |
#scale! ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/svm/scaler.rb', line 23 def scale! (1..num_features).each do |i| max = values_for_feature(i).compact.max min = values_for_feature(i).compact.min diff = (max - min).to_f unit[i] = diff/2.0 zero[i] = min + unit[i] end data.collect { |sample| scale(sample, true) } end |
#unit ⇒ Object
59 60 61 |
# File 'lib/svm/scaler.rb', line 59 def unit @unit ||= [] end |
#values_for_feature(i) ⇒ Object
51 52 53 |
# File 'lib/svm/scaler.rb', line 51 def values_for_feature(i) @data.collect { |sample| sample[i] } end |
#zero ⇒ Object
63 64 65 |
# File 'lib/svm/scaler.rb', line 63 def zero @zero ||= [] end |