ml_ai 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'matrix'
4
+ require_relative 'dataset'
5
+
6
+ module MLAI
7
+ class SimpleLinearRegression
8
+ attr_reader :slope, :intercept
9
+
10
+ def initialize
11
+ @slope = nil
12
+ @intercept = nil
13
+ end
14
+
15
+ # Fit method accepts either x_values and y_values or a Dataset object with specified columns
16
+ def fit(x_values: nil, y_values: nil, dataset: nil, feature_column: nil, target_column: nil)
17
+ if dataset
18
+ unless feature_column && target_column
19
+ raise ArgumentError, "When using a Dataset, you must specify feature_column and target_column"
20
+ end
21
+
22
+ # Extract indices of the specified columns
23
+ feature_index = dataset.headers.index(feature_column)
24
+ target_index = dataset.headers.index(target_column)
25
+
26
+ unless feature_index && target_index
27
+ raise ArgumentError, "Specified feature or target column does not exist in the dataset"
28
+ end
29
+
30
+ # Extract x and y values from the dataset
31
+ x_values = dataset.data.map { |row| row[feature_index] }
32
+ y_values = dataset.data.map { |row| row[target_index] }
33
+ elsif x_values && y_values
34
+ # Use x_values and y_values directly
35
+ else
36
+ raise ArgumentError, "You must provide either x_values and y_values or a dataset with feature_column and target_column"
37
+ end
38
+
39
+ # Ensure that x and y have the same number of observations
40
+ unless x_values.length == y_values.length
41
+ raise ArgumentError, "Input arrays must have the same length"
42
+ end
43
+
44
+ # Calculate means
45
+ mean_x = x_values.sum / x_values.length.to_f
46
+ mean_y = y_values.sum / y_values.length.to_f
47
+
48
+ # Calculate the numerator and denominator for the slope
49
+ numerator = 0.0
50
+ denominator = 0.0
51
+
52
+ x_values.each_with_index do |x, i|
53
+ numerator += (x - mean_x) * (y_values[i] - mean_y)
54
+ denominator += (x - mean_x) ** 2
55
+ end
56
+
57
+ @slope = numerator / denominator
58
+ @intercept = mean_y - @slope * mean_x
59
+ end
60
+
61
+ # Predict method remains unchanged, accepts an array of x_values
62
+ def predict(x_values)
63
+ raise "Model has not been fitted yet" if @slope.nil? || @intercept.nil?
64
+
65
+ x_values.map { |x| @slope * x + @intercept }
66
+ end
67
+
68
+ # Evaluation Metrics
69
+ def mean_squared_error(y_true, y_pred)
70
+ raise "Input arrays must have the same length" unless y_true.length == y_pred.length
71
+
72
+ n = y_true.length
73
+ sum_squared_errors = y_true.each_with_index.map { |y, i| (y - y_pred[i]) ** 2 }.sum
74
+ sum_squared_errors / n.to_f
75
+ end
76
+
77
+ def r_squared(y_true, y_pred)
78
+ raise "Input arrays must have the same length" unless y_true.length == y_pred.length
79
+
80
+ mean_y = y_true.sum / y_true.length.to_f
81
+ ss_total = y_true.map { |y| (y - mean_y) ** 2 }.sum
82
+ ss_residual = y_true.each_with_index.map { |y, i| (y - y_pred[i]) ** 2 }.sum
83
+
84
+ 1 - (ss_residual / ss_total.to_f)
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MLAI
4
+ VERSION = "0.1.0"
5
+ end
data/lib/ml_ai.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ml_ai/version"
4
+ require_relative "ml_ai/simple_linear_regression"
5
+ require_relative "ml_ai/multiple_linear_regression"
6
+ require_relative "ml_ai/dataset"
7
+
8
+ module MLAI
9
+ class Error < StandardError; end
10
+ # Your code goes here...
11
+ end
data/sig/ml_ai.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module MLAI
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ml_ai
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David William Silva
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-08-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Algorithms for machine learning and artificial intelligence in Ruby.
14
+ email:
15
+ - contact@davidwsilva.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rubocop.yml"
21
+ - CHANGELOG.md
22
+ - CODE_OF_CONDUCT.md
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - benchmarks/car_price_prediction_benchmark.py
27
+ - benchmarks/employee_salary_prediction_benchmark.py
28
+ - benchmarks/energy_consumption_prediction_benchmark.py
29
+ - benchmarks/evaluation_metrics.py
30
+ - benchmarks/house_price_prediction_benchmark.py
31
+ - benchmarks/multiple_linear_regression.py
32
+ - benchmarks/simple_linear_regression_benchmark.py
33
+ - data/advertising_revenue.csv
34
+ - data/car_prices.csv
35
+ - data/employee_salaries.csv
36
+ - data/energy_consumption.csv
37
+ - data/house_prices.csv
38
+ - data/multiple_linear_regression_data.csv
39
+ - data/simple_linear_regression_data.csv
40
+ - examples/car_price_prediction.rb
41
+ - examples/employee_salary_prediction.rb
42
+ - examples/energy_consumption_prediction.rb
43
+ - examples/house_price_prediction.rb
44
+ - lib/ml_ai.rb
45
+ - lib/ml_ai/dataset.rb
46
+ - lib/ml_ai/multiple_linear_regression.rb
47
+ - lib/ml_ai/simple_linear_regression.rb
48
+ - lib/ml_ai/version.rb
49
+ - sig/ml_ai.rbs
50
+ homepage: https://github.com/davidwilliam/MLAI
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ allowed_push_host: https://rubygems.org
55
+ homepage_uri: https://github.com/davidwilliam/MLAI
56
+ source_code_uri: https://github.com/davidwilliam/MLAI.git
57
+ changelog_uri: https://github.com/davidwilliam/CHANGELOG.md
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 3.0.0
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.5.11
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: ML and AI algorithms in Ruby.
77
+ test_files: []