Class: Weka::Core::Instances
- Inherits:
-
Object
- Object
- Weka::Core::Instances
- Defined in:
- lib/weka/core/instances.rb
Constant Summary collapse
- DEFAULT_RELATION_NAME =
'Instances'.freeze
Class Method Summary collapse
- .from_arff(file) ⇒ Object
-
.from_c45(file) ⇒ Object
Loads instances based on a given *.names file (holding the attribute values) or a given *.data file (holding the attribute values).
- .from_csv(file) ⇒ Object
- .from_json(file) ⇒ Object
Instance Method Summary collapse
- #add_attributes(&block) ⇒ Object (also: #with_attributes)
-
#add_instance(instance_or_values, weight: 1.0) ⇒ Object
Add new instance.
- #add_instances(data, weight: 1.0) ⇒ Object
- #apply_filter(filter) ⇒ Object
- #apply_filters(*filters) ⇒ Object
- #attribute_names(include_class_attribute: false) ⇒ Object
- #attributes(include_class_attribute: false) ⇒ 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
-
#has_attribute_type?(type) ⇒ Boolean
Check if the instances has any attribute of the given type.
-
#initialize(relation_name: DEFAULT_RELATION_NAME, attributes: []) ⇒ Instances
constructor
A new instance of Instances.
- #instances ⇒ Object
-
#internal_values_of(values) ⇒ Array, Hash
Retrieve the internal floating point values used to represent the attributes.
- #merge(*instances) ⇒ 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_c45(file) ⇒ Object
Creates a file with the istances’s attribute values and a *.data file with the actual data.
- #to_csv(file) ⇒ Object
- #to_json(file) ⇒ Object
-
#to_m ⇒ Matrix
Get the all instances’s values as Matrix.
Methods included from Weka::Concerns::Serializable
Methods included from Weka::Concerns::Persistent
Constructor Details
#initialize(relation_name: DEFAULT_RELATION_NAME, attributes: []) ⇒ Instances
Returns a new instance of Instances.
43 44 45 46 47 48 |
# File 'lib/weka/core/instances.rb', line 43 def initialize(relation_name: DEFAULT_RELATION_NAME, attributes: []) 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
20 21 22 |
# File 'lib/weka/core/instances.rb', line 20 def from_arff(file) Loader.load_arff(file) end |
.from_c45(file) ⇒ Object
Loads instances based on a given *.names file (holding the attribute values) or a given *.data file (holding the attribute values). The respective other file is loaded from the same directory.
See www.cs.washington.edu/dm/vfml/appendixes/c45.htm for more information about the C4.5 file format.
38 39 40 |
# File 'lib/weka/core/instances.rb', line 38 def from_c45(file) Loader.load_c45(file) end |
Instance Method Details
#add_attributes(&block) ⇒ Object Also known as: with_attributes
68 69 70 71 |
# File 'lib/weka/core/instances.rb', line 68 def add_attributes(&block) instance_eval(&block) if block self end |
#add_instance(instance_or_values, weight: 1.0) ⇒ Object
Add new instance
218 219 220 221 |
# File 'lib/weka/core/instances.rb', line 218 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
223 224 225 |
# File 'lib/weka/core/instances.rb', line 223 def add_instances(data, weight: 1.0) data.each { |values| add_instance(values, weight: weight) } end |
#apply_filter(filter) ⇒ Object
247 248 249 |
# File 'lib/weka/core/instances.rb', line 247 def apply_filter(filter) filter.filter(self) end |
#apply_filters(*filters) ⇒ Object
251 252 253 254 255 |
# File 'lib/weka/core/instances.rb', line 251 def apply_filters(*filters) filters.inject(self) do |filtered_instances, filter| filter.filter(filtered_instances) end end |
#attribute_names(include_class_attribute: false) ⇒ Object
64 65 66 |
# File 'lib/weka/core/instances.rb', line 64 def attribute_names(include_class_attribute: false) attributes(include_class_attribute: include_class_attribute).map(&:name) end |
#attributes(include_class_attribute: false) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/weka/core/instances.rb', line 54 def attributes(include_class_attribute: false) attrs = enumerate_attributes.to_a if include_class_attribute && class_attribute_defined? attrs.insert(class_index, class_attribute) end attrs end |
#class_attribute ⇒ Object
188 189 190 |
# File 'lib/weka/core/instances.rb', line 188 def class_attribute classAttribute if class_attribute_defined? end |
#class_attribute=(name) ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/weka/core/instances.rb', line 174 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
196 197 198 |
# File 'lib/weka/core/instances.rb', line 196 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
168 169 170 171 172 |
# File 'lib/weka/core/instances.rb', line 168 def date(name, format: 'yyyy-MM-dd HH:mm', class_attribute: false) attribute = Attribute.new_date(name, format) add_attribute(attribute) self.class_attribute = name if class_attribute end |
#each ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/weka/core/instances.rb', line 97 def each if block_given? enumerate_instances.each { |instance| yield(instance) } else enumerate_instances end end |
#each_attribute ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/weka/core/instances.rb', line 111 def each_attribute if block_given? enumerate_attributes.each { |attribute| yield(attribute) } else enumerate_attributes end end |
#each_attribute_with_index ⇒ Object
119 120 121 122 123 |
# File 'lib/weka/core/instances.rb', line 119 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
105 106 107 108 109 |
# File 'lib/weka/core/instances.rb', line 105 def each_with_index enumerate_instances.each_with_index do |instance, index| yield(instance, index) if block_given? end end |
#has_attribute_type?(type) ⇒ Boolean
Check if the instances has any attribute of the given type
92 93 94 95 |
# File 'lib/weka/core/instances.rb', line 92 def has_attribute_type?(type) type = map_attribute_type(type) unless type.is_a?(Integer) check_for_attribute_type(type) end |
#instances ⇒ Object
50 51 52 |
# File 'lib/weka/core/instances.rb', line 50 def instances enumerate_instances.to_a end |
#internal_values_of(values) ⇒ Array, Hash
Retrieve the internal floating point values used to represent
the attributes.
235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/weka/core/instances.rb', line 235 def internal_values_of(values) use_hash = values.is_a?(Hash) values = attribute_values_from_hash(values) if use_hash values = values.map.with_index do |value, index| attribute(index).internal_value_of(value) end values = attribute_values_to_hash(values) if use_hash values end |
#merge(*instances) ⇒ Object
257 258 259 260 261 |
# File 'lib/weka/core/instances.rb', line 257 def merge(*instances) instances.inject(self) do |merged_instances, dataset| self.class.merge_instances(merged_instances, dataset) end end |
#nominal(name, values:, class_attribute: false) ⇒ Object Also known as: add_nominal_attribute
156 157 158 159 160 |
# File 'lib/weka/core/instances.rb', line 156 def nominal(name, values:, class_attribute: false) attribute = Attribute.new_nominal(name, values) add_attribute(attribute) self.class_attribute = name if class_attribute end |
#numeric(name, class_attribute: false) ⇒ Object Also known as: add_numeric_attribute
150 151 152 153 154 |
# File 'lib/weka/core/instances.rb', line 150 def numeric(name, class_attribute: false) attribute = Attribute.new_numeric(name) add_attribute(attribute) self.class_attribute = name if class_attribute end |
#reset_class_attribute ⇒ Object
192 193 194 |
# File 'lib/weka/core/instances.rb', line 192 def reset_class_attribute set_class_index(-1) end |
#string(name, class_attribute: false) ⇒ Object Also known as: add_string_attribute
162 163 164 165 166 |
# File 'lib/weka/core/instances.rb', line 162 def string(name, class_attribute: false) attribute = Attribute.new_string(name) add_attribute(attribute) self.class_attribute = name if class_attribute end |
#to_arff(file) ⇒ Object
125 126 127 |
# File 'lib/weka/core/instances.rb', line 125 def to_arff(file) Saver.save_arff(file: file, instances: self) end |
#to_c45(file) ⇒ Object
Creates a file with the istances’s attribute values and a *.data file with the actual data.
You should choose another file extension than .data (preferably *.names) for the file, else it will just be overwritten with the automatically created *.data file.
See www.cs.washington.edu/dm/vfml/appendixes/c45.htm for more information about the C4.5 file format.
146 147 148 |
# File 'lib/weka/core/instances.rb', line 146 def to_c45(file) Saver.save_c45(file: file, instances: self) end |
#to_csv(file) ⇒ Object
129 130 131 |
# File 'lib/weka/core/instances.rb', line 129 def to_csv(file) Saver.save_csv(file: file, instances: self) end |
#to_json(file) ⇒ Object
133 134 135 |
# File 'lib/weka/core/instances.rb', line 133 def to_json(file) Saver.save_json(file: file, instances: self) end |
#to_m ⇒ Matrix
Get the all instances’s values as Matrix.
266 267 268 |
# File 'lib/weka/core/instances.rb', line 266 def to_m Matrix[*instances.map(&:values)] end |