ML_Ruby 0.1.1 → 0.1.3

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: 64fda0d7d407a58ec1df7fa697db1e5a08d9fe8b4ca4b28e5533c50d985fa1c6
4
- data.tar.gz: 26b26aac3b3260c77eda5aca6b4e04f3aa1320dd1c725a29d3d25b0e98a7c467
3
+ metadata.gz: 6d7da3da13e82053288079280a0a180b29129a1147329cb951d32ac3c34fc431
4
+ data.tar.gz: 15708947a613b924d67b7f0c67906d65c0a08cd05bd2b0aa517d4d993bf82871
5
5
  SHA512:
6
- metadata.gz: 55dc5ada8223599c639067eaf2cc301491b3c5fccf4d9a6d37ffeab286bec206cdd1154d9bd858f17a2002c9a31255e672660bf4e53f16b7214652796b8e2ca2
7
- data.tar.gz: 1c9c36e421b9295ae086619c06fa333595d910128e0c0e4e4b4257360f2f9b84621742eb20bac257553fdbb31c1a273062e3a8ae955db92092778330b234160a
6
+ metadata.gz: 00f00c1a7bdfe410bc5975fccc91d4abbe3502ae95bf5e9f61a194318110e0485577511895766e54b3063b9962a7241078dfe1e4d6566a1febb6c008e11ca150
7
+ data.tar.gz: fdcb73dfccbc76118e21c66896b1d9518f0c1af1935f6903a73235c0da2413bd2db1f352bc17612e8696ad2bdcb096fd257053116ce21b496f8d07f124478897
data/ML_Ruby.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.email = ["barek2k2@gmail.com"]
10
10
 
11
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."
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, identifying patient's potential findings(like Diabetes), 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 Logistic Regression, Random Forest and Linear Regression algorithms."
13
13
  spec.homepage = "https://github.com/barek2k2/ML_Ruby"
14
14
  spec.required_ruby_version = ">= 2.6.0"
15
15
 
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, forecasting upcoming sales orders, identifying patient's potential findings(like Diabetes), 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 Logistic Regression, 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
 
@@ -42,6 +42,24 @@ bundle install
42
42
  puts prediction
43
43
  ```
44
44
 
45
+ - ### Logistic Regression Algorithm - Diabetes detection Example in Health Industry
46
+
47
+ Imagine you possess some patients data encompassing vital attributes such as Blood Pressure, Glucose Level, and Age, meticulously arranged as input features, accompanied by corresponding predictions for diabetes as target variables. Now you can effortlessly predict the likelihood of diabetes for any new patient.
48
+ ```
49
+ ml = MLRuby::LogisticRegression::Model.new(
50
+ [
51
+ [120, 80, 32],
52
+ [140, 90, 28],
53
+ [160, 75, 35],
54
+ [135, 88, 30],
55
+ [145, 92, 38]
56
+ ],
57
+ [1, 0, 1, 0, 1]
58
+ )
59
+ predictions = ml.predict([[130, 85, 30], [80, 80, 90]])
60
+ puts predictions
61
+ ```
62
+
45
63
  - ### Random Forest Regression Algorithm - Real Estate House Pricing Example
46
64
 
47
65
  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.
@@ -163,6 +181,29 @@ training_messages = [
163
181
  ]
164
182
  predictions = ml.predict(new_messages)
165
183
  ```
184
+
185
+ - ### Natural Language Processing(NLP): Support Vector Machine Algorithm - Categorize/Classify Comments/Texts/Documents/Articles
186
+
187
+ 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.
188
+ ```
189
+ training_documents = [
190
+ "Machine learning techniques include neural networks and decision trees.",
191
+ "Web development skills are essential for building modern websites.",
192
+ "Natural language processing (NLP) is a subfield of artificial intelligence.",
193
+ "Data science involves data analysis and statistical modeling.",
194
+ "Computer vision is used in image and video processing applications."
195
+ ]
196
+ categories = ["Machine Learning", "Web Development", "Artificial Intelligence", "Data Science", "Computer Vision"]
197
+ ml = MLRuby::NaturalLanguageProcessing::SupportVectorMachine::Model.new(training_documents, categories)
198
+ new_documents = [
199
+ "I am Ruby On Rails Expert",
200
+ "I am interested in understanding natural language processing.",
201
+ "I want to pursue an academic degree on neural networks.",
202
+ "I have more than 12 years of professional working experience in JavaScript stack"
203
+ ]
204
+ predictions = ml.predict(new_documents)
205
+ ```
206
+
166
207
  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.
167
208
  ## Contributing
168
209
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MLRuby
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
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 LogisticRegression
23
+ class Model
24
+ def initialize(x, y)
25
+ @x = x
26
+ @y = y
27
+ end
28
+ def predict(nexts)
29
+ script_path = "#{Gem.loaded_specs['ML_Ruby'].gem_dir}/lib/python/logistic_regression.py"
30
+ result = `#{MLRuby::PYTHON_PATH} #{script_path} "#{@x}, #{@y}, #{nexts}"`
31
+ result.gsub("\n","").gsub("[","").gsub("]","").split(" ").map(&:to_i)
32
+ end
33
+ end
34
+ end
35
+
22
36
  module RandomForestRegression
23
37
  class Model
24
38
  def initialize(x, y)
@@ -74,6 +88,19 @@ module MLRuby
74
88
  end
75
89
  end
76
90
  end
91
+ module SupportVectorMachine
92
+ class Model
93
+ def initialize(training_data=[], categories=[])
94
+ @training_data = training_data
95
+ @categories = categories
96
+ end
97
+ def predict(new_data=[])
98
+ script_path = "#{Gem.loaded_specs['ML_Ruby'].gem_dir}/lib/python/natural_language_processing/support_vector_machine.py"
99
+ result = `#{MLRuby::PYTHON_PATH} #{script_path} '#{@training_data}' '#{@categories}' '#{new_data}'`
100
+ result.gsub("\n", "").gsub(/[\[\]]/, '').split("' '").map { |element| element.gsub(/'/, '') }
101
+ end
102
+ end
103
+ end
77
104
  end
78
105
 
79
106
  end
@@ -0,0 +1,27 @@
1
+ import numpy as np
2
+ from sklearn.linear_model import LogisticRegression
3
+ from sklearn.metrics import classification_report, confusion_matrix
4
+ import sys
5
+ import ast
6
+
7
+ class LogisticRegressionModel:
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
+
15
+ def train(self):
16
+ self.model = LogisticRegression()
17
+ self.model.fit(self.X, self.y)
18
+
19
+ def predict(self, new_predictions):
20
+ return self.model.predict(new_predictions)
21
+
22
+ data = ast.literal_eval(sys.argv[1])
23
+ logistic_regression_model = LogisticRegressionModel(data)
24
+ logistic_regression_model.process_data()
25
+ logistic_regression_model.train()
26
+ predictions = data[2]
27
+ print(logistic_regression_model.predict(predictions))
@@ -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)
metadata CHANGED
@@ -1,21 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ML_Ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdul Barek
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-18 00:00:00.000000000 Z
11
+ date: 2023-09-23 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
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
15
+ next month's billing, forecasting upcoming sales orders, identifying patient's potential
16
+ findings(like Diabetes), determining user approval status, classifying text, generating
17
+ similarity scores, and making recommendations. It uses Python3 under the hood, powered
18
+ by popular machine learning techniques including NLP(Natural Language Processing),
19
+ Decision Tree, K-Nearest Neighbors and Logistic Regression, Random Forest and Linear
19
20
  Regression algorithms.
20
21
  email:
21
22
  - barek2k2@gmail.com
@@ -32,8 +33,9 @@ files:
32
33
  - lib/python/decision_tree_classifier.py
33
34
  - lib/python/k_nearest_neighbors.py
34
35
  - lib/python/linear_regression.py
36
+ - lib/python/logistic_regression.py
37
+ - lib/python/natural_language_processing/support_vector_machine.py
35
38
  - lib/python/natural_language_processing/text_classifier.py
36
- - lib/python/natural_language_processing/text_summary.py
37
39
  - lib/python/random_forest.py
38
40
  - sig/ML_Ruby.rbs
39
41
  homepage: https://github.com/barek2k2/ML_Ruby
@@ -1,60 +0,0 @@
1
- import requests
2
-
3
- # Replace 'YOUR_API_KEY' with your actual Hugging Face API key
4
- api_key = 'YOUR_API_KEY'
5
-
6
- # Text to be summarized
7
- text = """
8
- Freedom of expression is a fundamental human right that holds immense importance in any democratic society. It serves as a platform for individuals to voice their opinions, share their thoughts and ideas, and engage in meaningful discussions and debates. However, the extent to which freedom of expression is upheld and practised varies from one country to another. In Bangladesh's case, although freedom of expression is enshrined in the constitution, its implementation faces significant challenges.
9
-
10
- At first glance, it may appear that people in Bangladesh enjoy the freedom to express themselves without fear of persecution. The media also seems relatively free to report on matters of public interest. However, a closer examination reveals the existence of substantial limitations to this freedom.
11
-
12
- One of the serious issues with freedom of expression in Bangladesh is its heavy bias towards certain individuals and groups. While some individuals, particularly those who are part of the ruling elite or possess political influence, can freely criticise the government and its policies, others do not have the same privilege. Those who lack political connections or are not part of the ruling elite face restrictions in expressing their opinions. They often become targets of persecution when they dare to speak out against the government or its policies.
13
-
14
- Furthermore, freedom of expression is confined to specific topics in the country. People are allowed to discuss issues that do not directly affect the larger population, but they are prohibited from being critical of the government's failures or the shortcomings of its institutions. To achieve this, the government employs various tactics such as intimidation, harassment, imprisonment, and even violence. The media is not immune to such suppression, as journalists are frequently targeted and attacked for reporting on sensitive issues. Such restrictions do not align with the principles of democracy.
15
-
16
- In Bangladesh, a unique style has emerged, favouring those who have political connections or are part of the ruling elite. This creates a significant power imbalance, silencing the voices of the majority while allowing the minority with access to power to dominate the conversation.
17
-
18
- How did the country arrive at this point? Is it solely due to the efforts of successive governments to silence dissidents, or have the institutions tasked with protecting the rights of the people also succumbed to vested interests?
19
-
20
- While it is valid to discuss the failure of institutions, a sizable portion of the blame falls on the media in Bangladesh as well. Impartial and unbiased journalism is increasingly limited in the country – not only due to informal embargoes and formal legal restrictions but also due to other factors. One crucial factor is the prevalence of media and journalists being aligned with partisan politics. While journalists having political alignments is not inherently wrong, it becomes problematic when they forget the fundamental principle of journalism: presenting the facts. Journalists with political biases or perceived biases often manipulate or distort information to support a specific political agenda or gain personal advantages. They tend to align themselves with the political party in power, compromising the freedom of expression that is crucial in a democratic society.
21
-
22
- Another concerning trend in Bangladesh is the influence of businesses or business conglomerates that own media outlets. These entities often leverage their media ownership to protect their business interests by eliminating competitors or evading accountability for crimes and wrongdoing. In such cases, the media outlets tend to align closely with the power structure, serving the interests of those in power, rather than speaking up for the people.
23
-
24
- Even in the presence of formal and informal restrictions imposed by the state, journalists should be able to report freely on matters of public interest. This freedom is essential for the press to remain relevant and create a platform for open dialogue among the people, their representatives, and public institutions. By doing so, the press can uphold the citizens' right to freedom of expression
25
-
26
- Journalists and media organisations in Bangladesh must reaffirm their commitment to impartiality, independence, and the presentation of facts. By adhering to these principles, they can counteract the influence of partisan politics and business interests on journalism and contribute to the preservation of press freedom in the country. Additionally, efforts should be made to foster a media environment that encourages diverse perspectives and safeguards the integrity of journalism.
27
-
28
- In a democracy, freedom of expression encompasses the right to criticise the government and its policies, and the press is expected to be free from bias. If Bangladesh aims to progress towards a more democratic society, it must ensure that freedom of expression is guaranteed to all its citizens, irrespective of their political affiliations or social status. But the question remains: how can the country do so?
29
-
30
- """
31
-
32
- # Define the endpoint URL
33
- endpoint = 'https://api-inference.huggingface.co/models/facebook/bart-large-cnn'
34
-
35
- # Define the headers with your API key
36
- api_key = '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'
37
- headers = {
38
- 'Authorization': f'Bearer {api_key}',
39
- }
40
-
41
- # Define the payload with the text to be summarized
42
- payload = {
43
- 'inputs': text,
44
- 'options': {
45
- 'task': 'summarization',
46
- },
47
- }
48
-
49
- # Make a POST request to the Hugging Face API
50
- response = requests.post(endpoint, json=payload, headers=headers)
51
-
52
- # Check if the request was successful
53
- if response.status_code == 200:
54
- result = response.json()
55
- print(result)
56
- #summary = result['summary']
57
- #print("Summary:")
58
- #print(summary)
59
- else:
60
- print("Error:", response.status_code, response.text)