Class: Weka::Core::Instances
- Inherits:
-
Object
- Object
- Weka::Core::Instances
- Includes:
- Weka::Concerns::Serializable
- 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
- #apply_filters(*filters) ⇒ 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.
31 32 33 34 35 36 |
# File 'lib/weka/core/instances.rb', line 31 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
18 19 20 |
# File 'lib/weka/core/instances.rb', line 18 def from_arff(file) Loader.load_arff(file) end |
Instance Method Details
#add_attributes(&block) ⇒ Object Also known as: with_attributes
50 51 52 53 |
# File 'lib/weka/core/instances.rb', line 50 def add_attributes(&block) self.instance_eval(&block) if block self end |
#add_instance(instance_or_values, weight: 1.0) ⇒ Object
149 150 151 152 |
# File 'lib/weka/core/instances.rb', line 149 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
154 155 156 |
# File 'lib/weka/core/instances.rb', line 154 def add_instances(data, weight: 1.0) data.each { |values| add_instance(values, weight: weight) } end |
#apply_filter(filter) ⇒ Object
164 165 166 |
# File 'lib/weka/core/instances.rb', line 164 def apply_filter(filter) filter.filter(self) end |
#apply_filters(*filters) ⇒ Object
168 169 170 171 172 |
# File 'lib/weka/core/instances.rb', line 168 def apply_filters(*filters) filters.inject(self) do |filtered_instances, filter| filter.filter(filtered_instances) end end |
#attribute_names ⇒ Object
46 47 48 |
# File 'lib/weka/core/instances.rb', line 46 def attribute_names attributes.map(&:name) end |
#attributes ⇒ Object
42 43 44 |
# File 'lib/weka/core/instances.rb', line 42 def attributes enumerate_attributes.to_a end |
#class_attribute ⇒ Object
137 138 139 |
# File 'lib/weka/core/instances.rb', line 137 def class_attribute classAttribute if class_attribute_defined? end |
#class_attribute=(name) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/weka/core/instances.rb', line 123 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
145 146 147 |
# File 'lib/weka/core/instances.rb', line 145 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
117 118 119 120 121 |
# File 'lib/weka/core/instances.rb', line 117 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
59 60 61 62 63 64 65 |
# File 'lib/weka/core/instances.rb', line 59 def each if block_given? enumerate_instances.each { |instance| yield(instance) } else enumerate_instances end end |
#each_attribute ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/weka/core/instances.rb', line 73 def each_attribute if block_given? enumerate_attributes.each { |attribute| yield(attribute) } else enumerate_attributes end end |
#each_attribute_with_index ⇒ Object
81 82 83 84 85 |
# File 'lib/weka/core/instances.rb', line 81 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
67 68 69 70 71 |
# File 'lib/weka/core/instances.rb', line 67 def each_with_index enumerate_instances.each_with_index do |instance, index| yield(instance, index) if block_given? end end |
#instances ⇒ Object
38 39 40 |
# File 'lib/weka/core/instances.rb', line 38 def instances enumerate_instances.to_a end |
#internal_values_of(values) ⇒ Object
158 159 160 161 162 |
# File 'lib/weka/core/instances.rb', line 158 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
105 106 107 108 109 |
# File 'lib/weka/core/instances.rb', line 105 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
99 100 101 102 103 |
# File 'lib/weka/core/instances.rb', line 99 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
141 142 143 |
# File 'lib/weka/core/instances.rb', line 141 def reset_class_attribute set_class_index(-1) end |
#string(name, class_attribute: false) ⇒ Object Also known as: add_string_attribute
111 112 113 114 115 |
# File 'lib/weka/core/instances.rb', line 111 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
87 88 89 |
# File 'lib/weka/core/instances.rb', line 87 def to_arff(file) Saver.save_arff(file: file, instances: self) end |