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
- #copy ⇒ Object
- #date(name, format: 'yyyy-MM-dd HH:mm', class_attribute: false) ⇒ Object (also: #add_date_attribute)
- #each(&block) ⇒ Object
- #each_attribute(&block) ⇒ Object
- #each_attribute_with_index(&block) ⇒ Object
- #each_with_index(&block) ⇒ 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.
-
#instance_from(instance_or_values, weight: 1.0) ⇒ Instance
Wrap the attribute values for the instance to be added with an Instance object, if needed.
- #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
72 73 74 75 |
# File 'lib/weka/core/instances.rb', line 72 def add_attributes(&block) instance_eval(&block) if block self end |
#add_instance(instance_or_values, weight: 1.0) ⇒ Object
Add new instance
226 227 228 229 |
# File 'lib/weka/core/instances.rb', line 226 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
231 232 233 |
# File 'lib/weka/core/instances.rb', line 231 def add_instances(data, weight: 1.0) data.each { |values| add_instance(values, weight: weight) } end |
#apply_filter(filter) ⇒ Object
255 256 257 |
# File 'lib/weka/core/instances.rb', line 255 def apply_filter(filter) filter.filter(self) end |
#apply_filters(*filters) ⇒ Object
259 260 261 262 263 |
# File 'lib/weka/core/instances.rb', line 259 def apply_filters(*filters) filters.inject(self) do |filtered_instances, filter| filter.filter(filtered_instances) end end |
#attribute_names(include_class_attribute: false) ⇒ Object
68 69 70 |
# File 'lib/weka/core/instances.rb', line 68 def attribute_names(include_class_attribute: false) attributes(include_class_attribute: include_class_attribute).map(&:name) end |
#attributes(include_class_attribute: false) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/weka/core/instances.rb', line 59 def attributes(include_class_attribute: false) attrs = enumerate_attributes.to_a class_available = include_class_attribute && class_attribute_defined? attrs.insert(class_index, class_attribute) if class_available attrs end |
#class_attribute ⇒ Object
196 197 198 |
# File 'lib/weka/core/instances.rb', line 196 def class_attribute classAttribute if class_attribute_defined? end |
#class_attribute=(name) ⇒ Object
182 183 184 185 186 187 188 189 |
# File 'lib/weka/core/instances.rb', line 182 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
204 205 206 |
# File 'lib/weka/core/instances.rb', line 204 def class_attribute_defined? class_index >= 0 end |
#copy ⇒ Object
50 51 52 53 |
# File 'lib/weka/core/instances.rb', line 50 def copy constructor = Instances.java_class.declared_constructor(Instances) constructor.new_instance(self).to_java(Instances) end |
#date(name, format: 'yyyy-MM-dd HH:mm', class_attribute: false) ⇒ Object Also known as: add_date_attribute
176 177 178 179 180 |
# File 'lib/weka/core/instances.rb', line 176 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(&block) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/weka/core/instances.rb', line 101 def each(&block) if block_given? enumerate_instances.each(&block) else enumerate_instances end end |
#each_attribute(&block) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/weka/core/instances.rb', line 117 def each_attribute(&block) if block_given? enumerate_attributes.each(&block) else enumerate_attributes end end |
#each_attribute_with_index(&block) ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/weka/core/instances.rb', line 125 def each_attribute_with_index(&block) if block_given? enumerate_attributes.each_with_index(&block) else enumerate_attributes end end |
#each_with_index(&block) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/weka/core/instances.rb', line 109 def each_with_index(&block) if block_given? enumerate_instances.each_with_index(&block) else enumerate_instances end end |
#has_attribute_type?(type) ⇒ Boolean
Check if the instances has any attribute of the given type
96 97 98 99 |
# File 'lib/weka/core/instances.rb', line 96 def has_attribute_type?(type) type = map_attribute_type(type) unless type.is_a?(Integer) check_for_attribute_type(type) end |
#instance_from(instance_or_values, weight: 1.0) ⇒ Instance
Wrap the attribute values for the instance to be added with
an Instance object, if needed. The Instance object is
assigned with the given weight.
290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/weka/core/instances.rb', line 290 def instance_from(instance_or_values, weight: 1.0) dataset = string_free_structure if instance_or_values.is_a?(Java::WekaCore::Instance) instance = instance_or_values instance.weight = weight else data = instance_data(instance_or_values) instance = DenseInstance.new(data, weight: weight) end dataset.add(instance) dataset.first end |
#instances ⇒ Object
55 56 57 |
# File 'lib/weka/core/instances.rb', line 55 def instances enumerate_instances.to_a end |
#internal_values_of(values) ⇒ Array, Hash
Retrieve the internal floating point values used to represent
the attributes.
243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/weka/core/instances.rb', line 243 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
265 266 267 268 269 |
# File 'lib/weka/core/instances.rb', line 265 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
164 165 166 167 168 |
# File 'lib/weka/core/instances.rb', line 164 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
158 159 160 161 162 |
# File 'lib/weka/core/instances.rb', line 158 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
200 201 202 |
# File 'lib/weka/core/instances.rb', line 200 def reset_class_attribute set_class_index(-1) end |
#string(name, class_attribute: false) ⇒ Object Also known as: add_string_attribute
170 171 172 173 174 |
# File 'lib/weka/core/instances.rb', line 170 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
133 134 135 |
# File 'lib/weka/core/instances.rb', line 133 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.
154 155 156 |
# File 'lib/weka/core/instances.rb', line 154 def to_c45(file) Saver.save_c45(file: file, instances: self) end |
#to_csv(file) ⇒ Object
137 138 139 |
# File 'lib/weka/core/instances.rb', line 137 def to_csv(file) Saver.save_csv(file: file, instances: self) end |
#to_json(file) ⇒ Object
141 142 143 |
# File 'lib/weka/core/instances.rb', line 141 def to_json(file) Saver.save_json(file: file, instances: self) end |
#to_m ⇒ Matrix
Get the all instances’s values as Matrix.
274 275 276 |
# File 'lib/weka/core/instances.rb', line 274 def to_m Matrix[*instances.map(&:values)] end |