ML_Ruby 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 34dae184f4e016ff1bb096a526d3d6c0b90c258611764d878937421dff40588d
4
- data.tar.gz: d04bc3f0684709b0facc19789493ab4fecad51c549fc0c059143c4e74f3ee029
3
+ metadata.gz: 95869d52047e12b4e821f8955856b4dbe0d4dff4efa797a44300fed365103b7a
4
+ data.tar.gz: 6130da39a94674ee68cc5d7c2e834b104f664e82cc19d4d14f9b97b3af297f29
5
5
  SHA512:
6
- metadata.gz: 8d537746a497cd52caf070ee0c7ee429cbe910d6d870a7f8deb5608cb481131c14e95ae5b80fa7e1d0dbd3f81f0880ac132440f8351cbdd9d77e356f8d67067e
7
- data.tar.gz: 1c25a49ff36920de1a361bacf64dd3b27f18aaa1d0117dec0f103fbe7a4673372c3f49137c5633adf1961d7ed4fc817eed442c8245ec6b34563ba4d064b2a6de
6
+ metadata.gz: 0bf46c64cd162b99fa821dd2e51c48ed4559b294d3a6d2ac33941f4fc7672b28eb20ac975085dbb7dfa2a278e633bca3760ce1f2d6d2811fdbb07b9372679a61
7
+ data.tar.gz: 63a6abaa30bc00fe04b2937188e7bf22ce67eff41878d77dcaef3e985c26bda05af1eaf4dd6a563a006f6f44d942c21a26354e90f6788b9a9287075cc7488851
data/README.md CHANGED
@@ -1,14 +1,14 @@
1
1
  # MLRuby
2
2
 
3
- This Ruby gem leverages Machine Learning(ML) techniques to make predictions(forecasts) and classifications in various applications. It provides capabilities such as predicting next month's billing, forecasting upcoming sales orders, determining user approval status, classifying text, generating similarity scores, and making recommendations. It uses Python3 under the hood, powered by popular machine learning techniques including NLP(Natural Language Processing), Decision Tree, K-Nearest Neighbors and Linear Regression algorithms.
3
+ This Ruby gem leverages Machine Learning(ML) techniques to make predictions(forecasts) and classifications in various applications. It provides capabilities such as predicting next month's billing, generating new house/apartment prices, forecasting upcoming sales orders, determining user approval status, classifying text, generating similarity scores, and making recommendations. It uses Python3 under the hood, powered by popular machine learning techniques including NLP(Natural Language Processing), Decision Tree, K-Nearest Neighbors, Random Forest and Linear Regression algorithms.
4
4
 
5
5
 
6
6
  # Pre-requisite
7
- 1. Please make sure you have Python3 installed in your Machine. The gem will run `which python3` to locate your installed python3 in your Machine. Usually it is installed at `/usr/bin/python3`
7
+ 1. Please make sure you have Python3 installed in your Machine. The gem will run `which python3` command internally in order to locate your installed python3 in your Machine. Usually it is installed at `/usr/bin/python3`
8
8
 
9
- 2. Please make sure you have `scikit-learn` and `pandas` python libraries are installed in Machine.
9
+ 2. Please make sure you have `scikit-learn` and `pandas` python libraries are installed in your Machine.
10
10
 
11
- Here are examples of how to install these python libraries via the command line in MacOS. Install `nltk` if you really need to work with Natural Language Processing(NLP)
11
+ Here are examples of how to install these python libraries via the command line in MacOS and Linux. Install `nltk` if you really need to work with Natural Language Processing(NLP)
12
12
 
13
13
  `/usr/bin/python3 -m pip install scikit-learn`
14
14
 
@@ -20,6 +20,18 @@ Here are examples of how to install these python libraries via the command line
20
20
 
21
21
  $ gem install ML_Ruby
22
22
 
23
+ To include the "ML_Ruby" gem in your Ruby on Rails application, simply add the following line to your Gemfile:
24
+
25
+ ```
26
+ gem 'ML_Ruby'
27
+ ```
28
+
29
+ After adding the gem, run the bundle install command to install it:
30
+
31
+ ```
32
+ bundle install
33
+ ```
34
+
23
35
  # Usage
24
36
  - ### Linear Regression Algorithm - Sales Order Prediction Example
25
37
 
@@ -30,6 +42,28 @@ Here are examples of how to install these python libraries via the command line
30
42
  puts prediction
31
43
  ```
32
44
 
45
+ - ### Random Forest Regression Algorithm - Real Estate House Pricing Example
46
+
47
+ Consider yourself in the dynamic field of the Real Estate market, you have some apartments/houses data(number of bed rooms, price, approval_status) represented as input features and their corresponding prices.
48
+
49
+ ```
50
+ apartment_features = [
51
+ [3, 1500, 0],
52
+ [2, 1200, 1],
53
+ [4, 1800, 0],
54
+ [3, 1600, 1],
55
+ [5, 2200, 1]
56
+ ]
57
+ prices = [300000, 250000, 400000, 350000, 500000]
58
+ ```
59
+ Now if you would like to predict any new apartment's price, you can do so as below:
60
+ ```
61
+ ml = MLRuby::RandomForestRegression::Model.new(apartment_features,prices)
62
+ two_new_apartment_features = [[4, 5068, 0], [3, 1760, 1]]
63
+ prediction = ml.predict(two_new_apartment_features)
64
+ puts prediction
65
+ ```
66
+
33
67
  - ### Decision Tree Algorithm - User Approval Status Example
34
68
 
35
69
  Suppose you have a dataset that includes features such as social credit score, yearly income, and approval status (where 1 represents approval, and 0 represents non-approval). Now, you want to classify the approval status of a new person.
@@ -129,6 +163,29 @@ training_messages = [
129
163
  ]
130
164
  predictions = ml.predict(new_messages)
131
165
  ```
166
+
167
+ - ### Natural Language Processing(NLP): Support Vector Machine Algorithm - Categorize/Classify Comments/Texts/Documents/Articles
168
+
169
+ Imagine you're managing a customer feedback system, and you want to categorize customer comments effectively. With the capabilities of this gem, you can seamlessly classify comments/texts/documents/articles into their appropriate categories.
170
+ ```
171
+ training_documents = [
172
+ "Machine learning techniques include neural networks and decision trees.",
173
+ "Web development skills are essential for building modern websites.",
174
+ "Natural language processing (NLP) is a subfield of artificial intelligence.",
175
+ "Data science involves data analysis and statistical modeling.",
176
+ "Computer vision is used in image and video processing applications."
177
+ ]
178
+ categories = ["Machine Learning", "Web Development", "Artificial Intelligence", "Data Science", "Computer Vision"]
179
+ ml = MLRuby::NaturalLanguageProcessing::SupportVectorMachine::Model.new(training_documents, categories)
180
+ new_documents = [
181
+ "I am Ruby On Rails Expert",
182
+ "I am interested in understanding natural language processing.",
183
+ "I want to pursue an academic degree on neural networks.",
184
+ "I have more than 12 years of professional working experience in JavaScript stack"
185
+ ]
186
+ predictions = ml.predict(new_documents)
187
+ ```
188
+
132
189
  It's important to note that the size of your training dataset plays a significant role in enhancing the accuracy of the model's predictions. By incorporating real-world, authentic data and expanding the amount of training data for the model, it gains a better understanding of patterns and trends within the data which leads to more precise and reliable predictions.
133
190
  ## Contributing
134
191
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MLRuby
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/ML_Ruby.rb CHANGED
@@ -19,6 +19,20 @@ module MLRuby
19
19
  end
20
20
  end
21
21
 
22
+ module RandomForestRegression
23
+ class Model
24
+ def initialize(x, y)
25
+ @x = x
26
+ @y = y
27
+ end
28
+ def predict(next_x)
29
+ script_path = "#{Gem.loaded_specs['ML_Ruby'].gem_dir}/lib/python/random_forest.py"
30
+ result = `#{MLRuby::PYTHON_PATH} #{script_path} "#{@x}, #{@y}, #{next_x}"`
31
+ result.gsub(/\[|\]/, '').split(' ').map(&:strip).map(&:to_f)
32
+ end
33
+ end
34
+ end
35
+
22
36
  module DecisionTreeClassifier
23
37
  class Model
24
38
  def initialize(data)
@@ -60,6 +74,19 @@ module MLRuby
60
74
  end
61
75
  end
62
76
  end
77
+ module SupportVectorMachine
78
+ class Model
79
+ def initialize(training_data=[], categories=[])
80
+ @training_data = training_data
81
+ @categories = categories
82
+ end
83
+ def predict(new_data=[])
84
+ script_path = "#{Gem.loaded_specs['ML_Ruby'].gem_dir}/lib/python/natural_language_processing/support_vector_machine.py"
85
+ result = `#{MLRuby::PYTHON_PATH} #{script_path} '#{@training_data}' '#{@categories}' '#{new_data}'`
86
+ result.gsub("\n", "").gsub(/[\[\]]/, '').split("' '").map { |element| element.gsub(/'/, '') }
87
+ end
88
+ end
89
+ end
63
90
  end
64
91
 
65
92
  end
@@ -0,0 +1,29 @@
1
+ import numpy as np
2
+ from sklearn.feature_extraction.text import TfidfVectorizer
3
+ from sklearn.svm import SVC
4
+ from sklearn.metrics import classification_report
5
+ import sys
6
+ import ast
7
+
8
+ class SupportVectorMachine:
9
+ def __init__(self, documents, categories):
10
+ self.documents = documents
11
+ self.categories = categories
12
+
13
+ def fit_and_transform(self):
14
+ self.vectorizer = TfidfVectorizer(max_features=1000, stop_words='english')
15
+ X = self.vectorizer.fit_transform(self.documents)
16
+ self.classifier = SVC(kernel='linear')
17
+ self.classifier.fit(X, self.categories)
18
+
19
+
20
+ documents = ast.literal_eval(sys.argv[1])
21
+ categories = ast.literal_eval(sys.argv[2])
22
+ new_documents = ast.literal_eval(sys.argv[3])
23
+
24
+ support_vector_machine_model = SupportVectorMachine(documents, categories)
25
+ support_vector_machine_model.fit_and_transform()
26
+
27
+ new_text_vectorized = support_vector_machine_model.vectorizer.transform(new_documents)
28
+ predicted_topics = support_vector_machine_model.classifier.predict(new_text_vectorized)
29
+ print(predicted_topics)
@@ -0,0 +1,30 @@
1
+ import numpy as np
2
+ from sklearn.ensemble import RandomForestRegressor
3
+
4
+ import sys
5
+ import ast
6
+
7
+ class RandomForestModel:
8
+ def __init__(self, data):
9
+ self.data = data
10
+
11
+ def process_data(self):
12
+ self.X = self.data[0]
13
+ self.y = self.data[1]
14
+ self.new_prediction = self.data[2]
15
+
16
+ def train(self):
17
+ self.model = RandomForestRegressor(n_estimators=100, random_state=42)
18
+ self.model.fit(self.X, self.y)
19
+
20
+ def predict(self):
21
+ prediction = self.model.predict(self.new_prediction)
22
+ return prediction
23
+
24
+
25
+ data = ast.literal_eval(sys.argv[1])
26
+ random_forest_regression_model = RandomForestModel(data)
27
+ random_forest_regression_model.process_data()
28
+ random_forest_regression_model.train()
29
+ prediction = random_forest_regression_model.predict()
30
+ print(prediction)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ML_Ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdul Barek
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-04 00:00:00.000000000 Z
11
+ date: 2023-09-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This Ruby gem leverages Machine Learning(ML) techniques to make predictions(forecasts)
14
14
  and classifications in various applications. It provides capabilities such as predicting
@@ -32,7 +32,9 @@ files:
32
32
  - lib/python/decision_tree_classifier.py
33
33
  - lib/python/k_nearest_neighbors.py
34
34
  - lib/python/linear_regression.py
35
+ - lib/python/natural_language_processing/support_vector_machine.py
35
36
  - lib/python/natural_language_processing/text_classifier.py
37
+ - lib/python/random_forest.py
36
38
  - sig/ML_Ruby.rbs
37
39
  homepage: https://github.com/barek2k2/ML_Ruby
38
40
  licenses: []
@@ -41,7 +43,7 @@ metadata:
41
43
  homepage_uri: https://github.com/barek2k2/ML_Ruby
42
44
  source_code_uri: https://github.com/barek2k2/ML_Ruby
43
45
  changelog_uri: https://github.com/barek2k2/ML_Ruby
44
- post_install_message:
46
+ post_install_message:
45
47
  rdoc_options: []
46
48
  require_paths:
47
49
  - lib
@@ -56,8 +58,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
58
  - !ruby/object:Gem::Version
57
59
  version: '0'
58
60
  requirements: []
59
- rubygems_version: 3.0.3
60
- signing_key:
61
+ rubygems_version: 3.1.6
62
+ signing_key:
61
63
  specification_version: 4
62
64
  summary: Ruby gem uses Machine Learning(ML) techniques to make predictions and classifications,
63
65
  and it's powered by Python3 under the hood.