Class: Ai4r::Classifiers::SimpleLinearRegression
- Inherits:
-
Classifier
- Object
- Classifier
- Ai4r::Classifiers::SimpleLinearRegression
- Defined in:
- lib/ai4r/classifiers/simple_linear_regression.rb
Overview
Introduction
This is an implementation of a Simple Linear Regression Classifier.
For further details regarding Bayes and Naive Bayes Classifier have a look at this link: en.wikipedia.org/wiki/Naive_Bayesian_classification en.wikipedia.org/wiki/Bayes%27_theorem
How to use it
data = DataSet.new.parse_csv_with_labels "autoPrice.csv"
c = SimpleLinearRegression.new.
build data
c.eval([1,158,105.8,192.7,71.4,55.7,2844,136,3.19,3.4,8.5,110,5500,19,25])
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
-
#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.
-
#initialize ⇒ SimpleLinearRegression
constructor
A new instance of SimpleLinearRegression.
Methods inherited from Classifier
Methods included from Data::Parameterizable
#get_parameters, included, #set_parameters
Constructor Details
#initialize ⇒ SimpleLinearRegression
Returns a new instance of SimpleLinearRegression.
38 39 40 41 42 43 |
# File 'lib/ai4r/classifiers/simple_linear_regression.rb', line 38 def initialize @attribute = nil @attribute_index = 0 @slope = 0 @intercept = 0 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
#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
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/ai4r/classifiers/simple_linear_regression.rb', line 56 def build(data) raise "Error instance must be passed" unless data.is_a?(DataSet) raise "Data should not be empty" if data.data_items.length == 0 y_mean = data.get_mean_or_mode[data.num_attributes - 1] # Choose best attribute min_msq = Float::MAX attribute = nil chosen = -1 chosen_slope = 0.0 / 0.0 # Float::NAN chosen_intercept = 0.0 / 0.0 # Float::NAN data.data_labels.each do |attr_name| attr_index = data.get_index attr_name if attr_index != data.num_attributes-1 # Compute slope and intercept x_mean = data.get_mean_or_mode[attr_index] sum_x_diff_squared = 0 sum_y_diff_squared = 0 slope = 0 data.data_items.map do |instance| x_diff = instance[attr_index] - x_mean y_diff = instance[attr_index] - y_mean slope += x_diff * y_diff sum_x_diff_squared += x_diff * x_diff sum_y_diff_squared += y_diff * y_diff end if sum_x_diff_squared == 0 next end numerator = slope slope /= sum_x_diff_squared intercept = y_mean - slope * x_mean msq = sum_y_diff_squared - slope * numerator if msq < min_msq min_msq = msq chosen = attr_index chosen_slope = slope chosen_intercept = intercept end end end if chosen == -1 raise "no useful attribute found" @attribute = nil @attribute_index = 0 @slope = 0 @intercept = y_mean else @attribute = data.data_labels[chosen] @attribute_index = chosen @slope = chosen_slope @intercept = chosen_intercept end return self 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
49 50 51 |
# File 'lib/ai4r/classifiers/simple_linear_regression.rb', line 49 def eval(data) @intercept + @slope * data[@attribute_index] end |