Class: Weka::Core::Instances
- Inherits:
-
Object
- Object
- Weka::Core::Instances
- Defined in:
- lib/weka/core/instances.rb
Constant Summary collapse
- DEFAULT_RELATION_NAME =
'Instances'
Class Method Summary collapse
Instance Method Summary collapse
- #add_attributes(&block) ⇒ Object (also: #with_attributes)
- #add_instance(instance_or_values, weight: 1.0) ⇒ Object
- #add_instances(data, weight: 1.0) ⇒ Object
- #apply_filter(filter) ⇒ Object
- #attribute_names ⇒ Object
- #attributes ⇒ Object
- #class_attribute ⇒ Object
- #class_attribute=(name) ⇒ Object
- #class_attribute_defined? ⇒ Boolean
- #date(name, format: 'yyyy-MM-dd HH:mm', class_attribute: false) ⇒ Object (also: #add_date_attribute)
- #each ⇒ Object
- #each_attribute ⇒ Object
- #each_attribute_with_index ⇒ Object
- #each_with_index ⇒ Object
-
#initialize(relation_name: DEFAULT_RELATION_NAME, attributes: [], &block) ⇒ Instances
constructor
A new instance of Instances.
- #instances ⇒ Object
- #internal_values_of(values) ⇒ Object
- #nominal(name, values:, class_attribute: false) ⇒ Object (also: #add_nominal_attribute)
- #numeric(name, class_attribute: false) ⇒ Object (also: #add_numeric_attribute)
- #reset_class_attribute ⇒ Object
- #string(name, class_attribute: false) ⇒ Object (also: #add_string_attribute)
- #to_arff(file) ⇒ Object
- #to_csv(file) ⇒ Object
- #to_json(file) ⇒ Object
Constructor Details
#initialize(relation_name: DEFAULT_RELATION_NAME, attributes: [], &block) ⇒ Instances
Returns a new instance of Instances.
29 30 31 32 33 34 |
# File 'lib/weka/core/instances.rb', line 29 def initialize(relation_name: DEFAULT_RELATION_NAME, attributes: [], &block) attribute_list = FastVector.new attributes.each { |attribute| attribute_list.add_element(attribute) } super(relation_name.to_s, attribute_list, 0) end |
Class Method Details
.from_arff(file) ⇒ Object
16 17 18 |
# File 'lib/weka/core/instances.rb', line 16 def from_arff(file) Loader.load_arff(file) end |
.from_csv(file) ⇒ Object
20 21 22 |
# File 'lib/weka/core/instances.rb', line 20 def from_csv(file) Loader.load_csv(file) end |
.from_json(file) ⇒ Object
24 25 26 |
# File 'lib/weka/core/instances.rb', line 24 def from_json(file) Loader.load_json(file) end |
Instance Method Details
#add_attributes(&block) ⇒ Object Also known as: with_attributes
48 49 50 51 |
# File 'lib/weka/core/instances.rb', line 48 def add_attributes(&block) self.instance_eval(&block) if block self end |
#add_instance(instance_or_values, weight: 1.0) ⇒ Object
147 148 149 150 |
# File 'lib/weka/core/instances.rb', line 147 def add_instance(instance_or_values, weight: 1.0) instance = instance_from(instance_or_values, weight: weight) add(instance) end |
#add_instances(data, weight: 1.0) ⇒ Object
152 153 154 |
# File 'lib/weka/core/instances.rb', line 152 def add_instances(data, weight: 1.0) data.each { |values| add_instance(values, weight: weight) } end |
#apply_filter(filter) ⇒ Object
162 163 164 |
# File 'lib/weka/core/instances.rb', line 162 def apply_filter(filter) filter.filter(self) end |
#attribute_names ⇒ Object
44 45 46 |
# File 'lib/weka/core/instances.rb', line 44 def attribute_names attributes.map(&:name) end |
#attributes ⇒ Object
40 41 42 |
# File 'lib/weka/core/instances.rb', line 40 def attributes enumerate_attributes.to_a end |
#class_attribute ⇒ Object
135 136 137 |
# File 'lib/weka/core/instances.rb', line 135 def class_attribute classAttribute if class_attribute_defined? end |
#class_attribute=(name) ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/weka/core/instances.rb', line 121 def class_attribute=(name) if name.nil? reset_class_attribute else ensure_attribute_defined!(name) setClass(attribute_with_name(name)) end end |
#class_attribute_defined? ⇒ Boolean
143 144 145 |
# File 'lib/weka/core/instances.rb', line 143 def class_attribute_defined? class_index >= 0 end |
#date(name, format: 'yyyy-MM-dd HH:mm', class_attribute: false) ⇒ Object Also known as: add_date_attribute
115 116 117 118 119 |
# File 'lib/weka/core/instances.rb', line 115 def date(name, format: 'yyyy-MM-dd HH:mm', class_attribute: false) attribute = Attribute.new(name.to_s, format) add_attribute(attribute) self.class_attribute = name if class_attribute end |
#each ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/weka/core/instances.rb', line 57 def each if block_given? enumerate_instances.each { |instance| yield(instance) } else enumerate_instances end end |
#each_attribute ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/weka/core/instances.rb', line 71 def each_attribute if block_given? enumerate_attributes.each { |attribute| yield(attribute) } else enumerate_attributes end end |
#each_attribute_with_index ⇒ Object
79 80 81 82 83 |
# File 'lib/weka/core/instances.rb', line 79 def each_attribute_with_index enumerate_attributes.each_with_index do |attribute, index| yield(attribute, index) if block_given? end end |
#each_with_index ⇒ Object
65 66 67 68 69 |
# File 'lib/weka/core/instances.rb', line 65 def each_with_index enumerate_instances.each_with_index do |instance, index| yield(instance, index) if block_given? end end |
#instances ⇒ Object
36 37 38 |
# File 'lib/weka/core/instances.rb', line 36 def instances enumerate_instances.to_a end |
#internal_values_of(values) ⇒ Object
156 157 158 159 160 |
# File 'lib/weka/core/instances.rb', line 156 def internal_values_of(values) values.each_with_index.map do |value, index| attribute(index).internal_value_of(value) end end |
#nominal(name, values:, class_attribute: false) ⇒ Object Also known as: add_nominal_attribute
103 104 105 106 107 |
# File 'lib/weka/core/instances.rb', line 103 def nominal(name, values:, class_attribute: false) attribute = Attribute.new(name.to_s, Array(values).map(&:to_s)) add_attribute(attribute) self.class_attribute = name if class_attribute end |
#numeric(name, class_attribute: false) ⇒ Object Also known as: add_numeric_attribute
97 98 99 100 101 |
# File 'lib/weka/core/instances.rb', line 97 def numeric(name, class_attribute: false) attribute = Attribute.new(name.to_s) add_attribute(attribute) self.class_attribute = name if class_attribute end |
#reset_class_attribute ⇒ Object
139 140 141 |
# File 'lib/weka/core/instances.rb', line 139 def reset_class_attribute set_class_index(-1) end |
#string(name, class_attribute: false) ⇒ Object Also known as: add_string_attribute
109 110 111 112 113 |
# File 'lib/weka/core/instances.rb', line 109 def string(name, class_attribute: false) attribute = Attribute.new(name.to_s, []) add_attribute(attribute) self.class_attribute = name if class_attribute end |
#to_arff(file) ⇒ Object
85 86 87 |
# File 'lib/weka/core/instances.rb', line 85 def to_arff(file) Saver.save_arff(file: file, instances: self) end |
#to_csv(file) ⇒ Object
89 90 91 |
# File 'lib/weka/core/instances.rb', line 89 def to_csv(file) Saver.save_csv(file: file, instances: self) end |
#to_json(file) ⇒ Object
93 94 95 |
# File 'lib/weka/core/instances.rb', line 93 def to_json(file) Saver.save_json(file: file, instances: self) end |