ML_Ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 34dae184f4e016ff1bb096a526d3d6c0b90c258611764d878937421dff40588d
4
+ data.tar.gz: d04bc3f0684709b0facc19789493ab4fecad51c549fc0c059143c4e74f3ee029
5
+ SHA512:
6
+ metadata.gz: 8d537746a497cd52caf070ee0c7ee429cbe910d6d870a7f8deb5608cb481131c14e95ae5b80fa7e1d0dbd3f81f0880ac132440f8351cbdd9d77e356f8d67067e
7
+ data.tar.gz: 1c25a49ff36920de1a361bacf64dd3b27f18aaa1d0117dec0f103fbe7a4673372c3f49137c5633adf1961d7ed4fc817eed442c8245ec6b34563ba4d064b2a6de
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/ML_Ruby.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/ML_Ruby/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ML_Ruby"
7
+ spec.version = MLRuby::VERSION
8
+ spec.authors = ["Abdul Barek"]
9
+ spec.email = ["barek2k2@gmail.com"]
10
+
11
+ spec.summary = "Ruby gem uses Machine Learning(ML) techniques to make predictions and classifications, and it's powered by Python3 under the hood."
12
+ spec.description = "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."
13
+ spec.homepage = "https://github.com/barek2k2/ML_Ruby"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
17
+
18
+ spec.metadata["homepage_uri"] = "https://github.com/barek2k2/ML_Ruby"
19
+ spec.metadata["source_code_uri"] = "https://github.com/barek2k2/ML_Ruby"
20
+ spec.metadata["changelog_uri"] = "https://github.com/barek2k2/ML_Ruby"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(__dir__) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (File.expand_path(f) == __FILE__) ||
27
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ # spec.add_dependency "example-gem", "~> 1.0"
36
+
37
+ # For more information and examples about making a new gem, check out our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
39
+ end
data/README.md ADDED
@@ -0,0 +1,135 @@
1
+ # MLRuby
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.
4
+
5
+
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`
8
+
9
+ 2. Please make sure you have `scikit-learn` and `pandas` python libraries are installed in Machine.
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)
12
+
13
+ `/usr/bin/python3 -m pip install scikit-learn`
14
+
15
+ `/usr/bin/python3 -m pip install pandas`
16
+
17
+ `/usr/bin/python3 -m pip install nltk`
18
+
19
+ # Installation
20
+
21
+ $ gem install ML_Ruby
22
+
23
+ # Usage
24
+ - ### Linear Regression Algorithm - Sales Order Prediction Example
25
+
26
+ Imagine you have three days' worth of sales order data represented as input features [1, 2, 3] and the corresponding sales amounts [100, 400, 430] as target variables. Now, you want to predict your sales order for day 4.
27
+ ```
28
+ ml = MLRuby::LinearRegression::Model.new([[1],[2],[3]], [[100], [400], [430]])
29
+ prediction = ml.predict([[4]])
30
+ puts prediction
31
+ ```
32
+
33
+ - ### Decision Tree Algorithm - User Approval Status Example
34
+
35
+ 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.
36
+
37
+ ```
38
+ data = [[720, 60000, 1],
39
+ [650, 40000, 0],
40
+ [780, 80000, 1],
41
+ [600, 30000, 0],
42
+ [700, 55000, 1],
43
+ [750, 70000, 1]]
44
+
45
+ ml = MLRuby::DecisionTreeClassifier::Model.new(data)
46
+ prediction1 = ml.predict([[180, 10000]])
47
+ prediction2 = ml.predict([[5000, 50000]])
48
+ ```
49
+ - ### K-Nearest Neighbors Algorithm - Example on Recommended/Similar products in E-Commerce based application
50
+
51
+ Imagine you have a training dataset representing various products in an e-commerce platform, each characterized by specific features. Now, you want to find similar products to a given product (let's say, product ID 4) based on these features.
52
+
53
+ ```
54
+ products = [
55
+ {
56
+ "id": 1,
57
+ "name": "iPhone 12",
58
+ "price": 799,
59
+ "screen_size": 6.1,
60
+ "camera_quality": 12,
61
+ "battery_capacity": 2815
62
+ },
63
+ {
64
+ "id": 2,
65
+ "name": "Samsung Galaxy S21",
66
+ "price": 799,
67
+ "screen_size": 6.2,
68
+ "camera_quality": 12,
69
+ "battery_capacity": 4000
70
+ },
71
+ {
72
+ "id": 3,
73
+ "name": "Google Pixel 6",
74
+ "price": 699,
75
+ "screen_size": 6.0,
76
+ "camera_quality": 16,
77
+ "battery_capacity": 3700
78
+ },
79
+ {
80
+ "id": 4,
81
+ "name": "OnePlus 9 Pro",
82
+ "price": 799,
83
+ "screen_size": 6.7,
84
+ "camera_quality": 16,
85
+ "battery_capacity": 4500
86
+ },
87
+ {
88
+ "id": 5,
89
+ "name": "Xiaomi Mi 11",
90
+ "price": 699,
91
+ "screen_size": 6.81,
92
+ "camera_quality": 12,
93
+ "battery_capacity": 4600
94
+ }
95
+ ]
96
+ ```
97
+ ```
98
+ feature_names = ["price", "screen_size", "camera_quality", "battery_capacity"]
99
+ ml = MLRuby::KNearestNeighbors::Model.new(products, feature_names, 2) # 2 is the maximum number of nearest similar/recommended items
100
+ similar_products = ml.similar_with(4)
101
+ ```
102
+
103
+ ```
104
+ feature_names = ["price", "camera_quality"]
105
+ ml = MLRuby::KNearestNeighbors::Model.new(products, feature_names, 2)
106
+ similar_products = ml.similar_with(4)
107
+ ```
108
+
109
+ - ### Natural Language Processing(NLP): Naive Bayes Algorithm - Spam Detection in a Messaging System
110
+
111
+ In a messaging system, it's essential to identify and filter out spam text messages to ensure a smooth and secure user experience. With the capabilities of this gem, you can effectively detect spam text and take appropriate actions.
112
+ ```
113
+ training_messages = [
114
+ ["Hey, congratulations! You have won a free iPhone.", "spam"],
115
+ ["Meeting canceled, see you later.", "not_spam"],
116
+ ["Buy one get one free. Limited time offer!", "spam"],
117
+ ["Can you please send me the report?", "not_spam"],
118
+ ["Meeting at 3 PM today.", "not_spam"],
119
+ ["Claim your prize now. You have won $1000!", "spam"],
120
+ ["Please reschedule the meeting on the next following day", "not_spam"],
121
+ ]
122
+
123
+ ml = MLRuby::NaturalLanguageProcessing::TextClassifier::Model.new(training_messages)
124
+ new_messages = [
125
+ "Welcome!, you have won 2.5 million dollars",
126
+ "Hello, can we schedule a meeting?",
127
+ "Important report attached.",
128
+ "Have your 50% discount on the next deal!",
129
+ ]
130
+ predictions = ml.predict(new_messages)
131
+ ```
132
+ 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
+ ## Contributing
134
+
135
+ Bug reports and pull requests are welcome on GitHub at https://github.com/barek2k2/ML_Ruby/.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MLRuby
4
+ VERSION = "0.1.0"
5
+ end
data/lib/ML_Ruby.rb ADDED
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ML_Ruby/version"
4
+ require 'json'
5
+
6
+ module MLRuby
7
+ PYTHON_PATH = `which python3`.gsub("\n","")
8
+ module LinearRegression
9
+ class Model
10
+ def initialize(x, y)
11
+ @x = x
12
+ @y = y
13
+ end
14
+ def predict(next_x)
15
+ script_path = "#{Gem.loaded_specs['ML_Ruby'].gem_dir}/lib/python/linear_regression.py"
16
+ result = `#{MLRuby::PYTHON_PATH} #{script_path} "#{@x}, #{@y}, #{next_x}"`
17
+ result.to_f
18
+ end
19
+ end
20
+ end
21
+
22
+ module DecisionTreeClassifier
23
+ class Model
24
+ def initialize(data)
25
+ @data = data
26
+ end
27
+ def predict(next_x)
28
+ script_path = "#{Gem.loaded_specs['ML_Ruby'].gem_dir}/lib/python/decision_tree_classifier.py"
29
+ result = `#{MLRuby::PYTHON_PATH} #{script_path} "#{@data}, #{next_x}"`
30
+ result.to_i
31
+ end
32
+ end
33
+ end
34
+
35
+ module KNearestNeighbors
36
+ class Model
37
+ def initialize(items, features=[], n_neighbors=3)
38
+ @items = items.to_json
39
+ @features = features
40
+ @n_neighbors = n_neighbors
41
+ end
42
+ def similar_with(id)
43
+ script_path = "#{Gem.loaded_specs['ML_Ruby'].gem_dir}/lib/python/k_nearest_neighbors.py"
44
+ result = `#{MLRuby::PYTHON_PATH} #{script_path} '#{@items}' '#{@features}' '#{id}' '#{@n_neighbors}'`
45
+ JSON.parse(result.gsub(/'([^']+)'/, '"\1"'))
46
+ end
47
+ end
48
+ end
49
+
50
+ module NaturalLanguageProcessing
51
+ module TextClassifier
52
+ class Model
53
+ def initialize(training_data)
54
+ @training_data = training_data
55
+ end
56
+ def predict(new_data=[])
57
+ script_path = "#{Gem.loaded_specs['ML_Ruby'].gem_dir}/lib/python/natural_language_processing/text_classifier.py"
58
+ result = `#{MLRuby::PYTHON_PATH} #{script_path} '#{@training_data}' '#{new_data}'`
59
+ JSON.parse(result.gsub("'", "\""))
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ end
@@ -0,0 +1,29 @@
1
+ from sklearn.tree import DecisionTreeClassifier
2
+ from sklearn.model_selection import train_test_split
3
+ from sklearn.metrics import accuracy_score
4
+ import sys
5
+ import ast
6
+
7
+ class DecisionTreeClassifierModel:
8
+ def __init__(self, data):
9
+ self.data = data
10
+
11
+ def process_data(self):
12
+ self.X = [row[:2] for row in self.data[0]]
13
+ self.y = [row[2] for row in self.data[0]]
14
+ self.new_prediction = self.data[1]
15
+
16
+ def train(self):
17
+ self.model = DecisionTreeClassifier()
18
+ self.model.fit(self.X, self.y)
19
+
20
+ def predict(self):
21
+ prediction = self.model.predict(self.new_prediction)
22
+ return prediction[0]
23
+
24
+ data = ast.literal_eval(sys.argv[1])
25
+ decision_tree_classifier_model = DecisionTreeClassifierModel(data)
26
+ decision_tree_classifier_model.process_data()
27
+ decision_tree_classifier_model.train()
28
+ predicted_class = decision_tree_classifier_model.predict()
29
+ print(predicted_class)
@@ -0,0 +1,61 @@
1
+ from sklearn.neighbors import NearestNeighbors
2
+ import sys
3
+ import ast
4
+
5
+ class Recommendation:
6
+ def __init__(self, items, feature_names, n_neighbors=3):
7
+ self.n_neighbors = int(n_neighbors)
8
+ self.items = items # List of product JSON objects
9
+ self.feature_names = feature_names # List of feature property names
10
+ self.features = self.extract_features() # Extract features dynamically
11
+ self.nn_model = NearestNeighbors(n_neighbors=self.n_neighbors) # KNN model with k=3 default
12
+ self.nn_model.fit(self.features) # Fit the KNN model during initialization
13
+
14
+ def extract_features(self):
15
+ # Extract features dynamically based on feature property names
16
+ features = []
17
+ for product in self.items:
18
+ feature_vector = [product.get(feature, 0) for feature in self.feature_names]
19
+ features.append(feature_vector)
20
+ return features
21
+
22
+ def find_similar_products(self, product_id):
23
+ # Find the index of the product with the given ID
24
+ product_index = None
25
+ for i, product in enumerate(self.items):
26
+ if product["id"] == product_id:
27
+ product_index = i
28
+ break
29
+
30
+ if product_index is None:
31
+ return None # Product ID not found
32
+
33
+ # Find the k-nearest neighbors to the given product
34
+ distances, indices = self.nn_model.kneighbors([self.features[product_index]])
35
+
36
+ # Create a list of similar items
37
+ similar_products = []
38
+ for i in indices[0]:
39
+ if i != product_index:
40
+ similar_products.append(self.items[i])
41
+
42
+ return similar_products
43
+
44
+ # Sample data as an array of JSON objects
45
+ items = ast.literal_eval(sys.argv[1])
46
+
47
+ # Define the feature property names to be extracted dynamically
48
+ feature_names = ast.literal_eval(sys.argv[2])
49
+
50
+ # id of item
51
+ id = ast.literal_eval(sys.argv[3])
52
+
53
+ # Number of neighbors to fetch from
54
+ n_neighbors = ast.literal_eval(sys.argv[4])
55
+
56
+ # Create a Recommendation instance
57
+ recommendation = Recommendation(items, feature_names, n_neighbors)
58
+
59
+ # Find similar items to a specific product by ID (e.g., ID 2)
60
+ similar_products = recommendation.find_similar_products(id)
61
+ print(similar_products)
@@ -0,0 +1,29 @@
1
+ import pandas as pd
2
+ from sklearn.linear_model import LinearRegression
3
+ import datetime
4
+ import sys
5
+ import ast
6
+
7
+ class LinearRegressionModel:
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 = LinearRegression()
18
+ self.model.fit(self.X, self.y)
19
+
20
+ def predict(self):
21
+ prediction = self.model.predict(self.new_prediction)
22
+ return prediction[0][0]
23
+
24
+ data = ast.literal_eval(sys.argv[1])
25
+ linear_regression_model = LinearRegressionModel(data)
26
+ linear_regression_model.process_data()
27
+ linear_regression_model.train()
28
+ forecast = linear_regression_model.predict()
29
+ print(f"{forecast:.2f}")
@@ -0,0 +1,59 @@
1
+ import nltk
2
+ import random
3
+ from nltk.corpus import stopwords
4
+ from nltk.tokenize import word_tokenize
5
+ import sys
6
+ import ast
7
+
8
+ # Download NLTK data if not already downloaded
9
+ nltk.download('stopwords')
10
+ nltk.download('punkt')
11
+
12
+ class TextClassifier:
13
+ def __init__(self):
14
+ self.vectorizer = None
15
+ self.classifier = None
16
+
17
+ def extract_features(self, text):
18
+ words = set(word_tokenize(text))
19
+ features = {word: (word not in stopwords.words('english')) for word in words}
20
+ return features
21
+
22
+ def train(self, texts):
23
+ random.shuffle(texts)
24
+
25
+ # Create feature sets
26
+ labeled_data = [(self.extract_features(text), label) for text, label in texts]
27
+
28
+ # Train the Naive Bayes classifier
29
+ self.classifier = nltk.NaiveBayesClassifier.train(labeled_data)
30
+
31
+ def test_accuracy(self, texts):
32
+ # Create feature sets
33
+ labeled_data = [(self.extract_features(text), label) for text, label in texts]
34
+
35
+ # Calculate accuracy
36
+ accuracy = nltk.classify.accuracy(self.classifier, labeled_data)
37
+ return accuracy
38
+
39
+ def classify_text(self, text):
40
+ features = self.extract_features(text)
41
+ prediction = self.classifier.classify(features)
42
+ return prediction
43
+
44
+ # Sample dataset: spam and ham texts
45
+ texts = ast.literal_eval(sys.argv[1])
46
+
47
+ # Create and train the text_classifier
48
+ text_classifier = TextClassifier()
49
+ text_classifier.train(texts)
50
+
51
+ # Make predictions on new texts
52
+ new_texts = ast.literal_eval(sys.argv[2])
53
+
54
+ new_prediction = []
55
+ for text in new_texts:
56
+ prediction = text_classifier.classify_text(text)
57
+ new_prediction.append([text, prediction])
58
+
59
+ print(new_prediction)
data/sig/ML_Ruby.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module MLRuby
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ML_Ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Abdul Barek
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-09-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This Ruby gem leverages Machine Learning(ML) techniques to make predictions(forecasts)
14
+ and classifications in various applications. It provides capabilities such as predicting
15
+ next month's billing, forecasting upcoming sales orders, determining user approval
16
+ status, classifying text, generating similarity scores, and making recommendations.
17
+ It uses Python3 under the hood, powered by popular machine learning techniques including
18
+ NLP(Natural Language Processing), Decision Tree, K-Nearest Neighbors and Linear
19
+ Regression algorithms.
20
+ email:
21
+ - barek2k2@gmail.com
22
+ executables: []
23
+ extensions: []
24
+ extra_rdoc_files: []
25
+ files:
26
+ - ".rspec"
27
+ - ML_Ruby.gemspec
28
+ - README.md
29
+ - Rakefile
30
+ - lib/ML_Ruby.rb
31
+ - lib/ML_Ruby/version.rb
32
+ - lib/python/decision_tree_classifier.py
33
+ - lib/python/k_nearest_neighbors.py
34
+ - lib/python/linear_regression.py
35
+ - lib/python/natural_language_processing/text_classifier.py
36
+ - sig/ML_Ruby.rbs
37
+ homepage: https://github.com/barek2k2/ML_Ruby
38
+ licenses: []
39
+ metadata:
40
+ allowed_push_host: https://rubygems.org/
41
+ homepage_uri: https://github.com/barek2k2/ML_Ruby
42
+ source_code_uri: https://github.com/barek2k2/ML_Ruby
43
+ changelog_uri: https://github.com/barek2k2/ML_Ruby
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.6.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.0.3
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Ruby gem uses Machine Learning(ML) techniques to make predictions and classifications,
63
+ and it's powered by Python3 under the hood.
64
+ test_files: []