Class: Ai4r::Classifiers::SimpleLinearRegression
- Inherits:
-
Classifier
- Object
- Classifier
- Ai4r::Classifiers::SimpleLinearRegression
- Defined in:
- lib/ai4r/classifiers/simple_linear_regression.rb
Overview
SimpleLinearRegression performs linear regression on one attribute.
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#attribute_index ⇒ Object
readonly
Returns the value of attribute attribute_index.
-
#intercept ⇒ Object
readonly
Returns the value of attribute intercept.
-
#slope ⇒ Object
readonly
Returns the value of attribute slope.
Instance Method Summary collapse
- #assign_result(data, result) ⇒ Object
-
#build(data) ⇒ Object
Gets the best attribute and does Linear Regression using it to find out the slope and intercept.
-
#eval(data) ⇒ Object
You can evaluate new data, predicting its category.
- #evaluate_all_attributes(data, y_mean) ⇒ Object
- #evaluate_attribute(data, attr_index, y_mean) ⇒ Object
-
#get_rules ⇒ Object
Simple Linear Regression classifiers cannot generate human readable rules.
- #initialize ⇒ Object constructor
- #validate_data(data) ⇒ Object
Methods included from Data::Parameterizable
#get_parameters, included, #set_parameters
Constructor Details
#initialize ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/ai4r/classifiers/simple_linear_regression.rb', line 41 def initialize super() @attribute = nil @attribute_index = 0 @slope = 0 @intercept = 0 @selected_attribute = nil end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
36 37 38 |
# File 'lib/ai4r/classifiers/simple_linear_regression.rb', line 36 def attribute @attribute end |
#attribute_index ⇒ Object (readonly)
Returns the value of attribute attribute_index.
36 37 38 |
# File 'lib/ai4r/classifiers/simple_linear_regression.rb', line 36 def attribute_index @attribute_index end |
#intercept ⇒ Object (readonly)
Returns the value of attribute intercept.
36 37 38 |
# File 'lib/ai4r/classifiers/simple_linear_regression.rb', line 36 def intercept @intercept end |
#slope ⇒ Object (readonly)
Returns the value of attribute slope.
36 37 38 |
# File 'lib/ai4r/classifiers/simple_linear_regression.rb', line 36 def slope @slope end |
Instance Method Details
#assign_result(data, result) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/ai4r/classifiers/simple_linear_regression.rb', line 108 def assign_result(data, result) raise 'no useful attribute found' if result[:chosen] == -1 @attribute = data.data_labels[result[:chosen]] @attribute_index = result[:chosen] @slope = result[:slope] @intercept = result[:intercept] self end |
#build(data) ⇒ Object
Gets the best attribute and does Linear Regression using it to find out the slope and intercept. Parameter data has to be an instance of DataSet
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ai4r/classifiers/simple_linear_regression.rb', line 65 def build(data) validate_data(data) y_mean = data.get_mean_or_mode[data.num_attributes - 1] result = if @selected_attribute evaluate_attribute(data, @selected_attribute, y_mean) else evaluate_all_attributes(data, y_mean) end assign_result(data, result) end |
#eval(data) ⇒ Object
You can evaluate new data, predicting its category. e.g.
c.eval([1,158,105.8,192.7,71.4,55.7,2844,136,3.19,3.4,8.5,110,5500,19,25])
=> 11876.96774193548
56 57 58 |
# File 'lib/ai4r/classifiers/simple_linear_regression.rb', line 56 def eval(data) @intercept + (@slope * data[@attribute_index]) end |
#evaluate_all_attributes(data, y_mean) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ai4r/classifiers/simple_linear_regression.rb', line 94 def evaluate_all_attributes(data, y_mean) result = { chosen: -1, msq: Float::MAX } data.data_labels.each do |attr_name| attr_index = data.get_index attr_name next if attr_index == data.num_attributes - 1 candidate = evaluate_attribute(data, attr_index, y_mean) next unless candidate[:msq] < result[:msq] result = candidate end result end |
#evaluate_attribute(data, attr_index, y_mean) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ai4r/classifiers/simple_linear_regression.rb', line 82 def evaluate_attribute(data, attr_index, y_mean) x_mean = data.get_mean_or_mode[attr_index] slope, x_diff_sq, y_diff_sq = attribute_sums(data, attr_index, x_mean, y_mean) if x_diff_sq.zero? { chosen: attr_index, slope: 0, intercept: y_mean, msq: Float::MAX } else chosen_slope = slope / x_diff_sq intercept = y_mean - (chosen_slope * x_mean) { chosen: attr_index, slope: chosen_slope, intercept: intercept, msq: y_diff_sq - (chosen_slope * slope) } end end |
#get_rules ⇒ Object
Simple Linear Regression classifiers cannot generate human readable rules. This method returns a descriptive string indicating that rule extraction is not supported.
121 122 123 |
# File 'lib/ai4r/classifiers/simple_linear_regression.rb', line 121 def get_rules 'SimpleLinearRegression does not support rule extraction.' end |
#validate_data(data) ⇒ Object
77 78 79 80 |
# File 'lib/ai4r/classifiers/simple_linear_regression.rb', line 77 def validate_data(data) raise 'Error instance must be passed' unless data.is_a?(Ai4r::Data::DataSet) raise 'Data should not be empty' if data.data_items.empty? end |