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 ⇒ 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.
-
#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
73 74 75 76 |
# File 'lib/weka/core/instances.rb', line 73 def add_attributes(&block) instance_eval(&block) if block self end |
#add_instance(instance_or_values, weight: 1.0) ⇒ Object
Add new instance
223 224 225 226 |
# File 'lib/weka/core/instances.rb', line 223 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
228 229 230 |
# File 'lib/weka/core/instances.rb', line 228 def add_instances(data, weight: 1.0) data.each { |values| add_instance(values, weight: weight) } end |
#apply_filter(filter) ⇒ Object
252 253 254 |
# File 'lib/weka/core/instances.rb', line 252 def apply_filter(filter) filter.filter(self) end |
#apply_filters(*filters) ⇒ Object
256 257 258 259 260 |
# File 'lib/weka/core/instances.rb', line 256 def apply_filters(*filters) filters.inject(self) do |filtered_instances, filter| filter.filter(filtered_instances) end end |
#attribute_names(include_class_attribute: false) ⇒ Object
69 70 71 |
# File 'lib/weka/core/instances.rb', line 69 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 67 |
# File 'lib/weka/core/instances.rb', line 59 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
193 194 195 |
# File 'lib/weka/core/instances.rb', line 193 def class_attribute classAttribute if class_attribute_defined? end |
#class_attribute=(name) ⇒ Object
179 180 181 182 183 184 185 186 |
# File 'lib/weka/core/instances.rb', line 179 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
201 202 203 |
# File 'lib/weka/core/instances.rb', line 201 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
173 174 175 176 177 |
# File 'lib/weka/core/instances.rb', line 173 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
102 103 104 105 106 107 108 |
# File 'lib/weka/core/instances.rb', line 102 def each if block_given? enumerate_instances.each { |instance| yield(instance) } else enumerate_instances end end |
#each_attribute ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/weka/core/instances.rb', line 116 def each_attribute if block_given? enumerate_attributes.each { |attribute| yield(attribute) } else enumerate_attributes end end |
#each_attribute_with_index ⇒ Object
124 125 126 127 128 |
# File 'lib/weka/core/instances.rb', line 124 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
110 111 112 113 114 |
# File 'lib/weka/core/instances.rb', line 110 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
97 98 99 100 |
# File 'lib/weka/core/instances.rb', line 97 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.
287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/weka/core/instances.rb', line 287 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.
240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/weka/core/instances.rb', line 240 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
262 263 264 265 266 |
# File 'lib/weka/core/instances.rb', line 262 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
161 162 163 164 165 |
# File 'lib/weka/core/instances.rb', line 161 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
155 156 157 158 159 |
# File 'lib/weka/core/instances.rb', line 155 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
197 198 199 |
# File 'lib/weka/core/instances.rb', line 197 def reset_class_attribute set_class_index(-1) end |
#string(name, class_attribute: false) ⇒ Object Also known as: add_string_attribute
167 168 169 170 171 |
# File 'lib/weka/core/instances.rb', line 167 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
130 131 132 |
# File 'lib/weka/core/instances.rb', line 130 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.
151 152 153 |
# File 'lib/weka/core/instances.rb', line 151 def to_c45(file) Saver.save_c45(file: file, instances: self) end |
#to_csv(file) ⇒ Object
134 135 136 |
# File 'lib/weka/core/instances.rb', line 134 def to_csv(file) Saver.save_csv(file: file, instances: self) end |
#to_json(file) ⇒ Object
138 139 140 |
# File 'lib/weka/core/instances.rb', line 138 def to_json(file) Saver.save_json(file: file, instances: self) end |
#to_m ⇒ Matrix
Get the all instances’s values as Matrix.
271 272 273 |
# File 'lib/weka/core/instances.rb', line 271 def to_m Matrix[*instances.map(&:values)] end |