jackofalltrades 0.0.1__tar.gz
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.
- jackofalltrades-0.0.1/LICENSE.txt +17 -0
- jackofalltrades-0.0.1/MANIFEST.in +1 -0
- jackofalltrades-0.0.1/PKG-INFO +65 -0
- jackofalltrades-0.0.1/README.txt +39 -0
- jackofalltrades-0.0.1/jackofalltrades/Errors/__init__.py +1 -0
- jackofalltrades-0.0.1/jackofalltrades/Errors/error.py +195 -0
- jackofalltrades-0.0.1/jackofalltrades/Models/Regression.py +198 -0
- jackofalltrades-0.0.1/jackofalltrades/Models/__init__.py +1 -0
- jackofalltrades-0.0.1/jackofalltrades/__init__.py +0 -0
- jackofalltrades-0.0.1/jackofalltrades/datasets/BTC-USD.csv +3229 -0
- jackofalltrades-0.0.1/jackofalltrades/datasets/Real estate.csv +415 -0
- jackofalltrades-0.0.1/jackofalltrades/datasets/__init__.py +1 -0
- jackofalltrades-0.0.1/jackofalltrades/datasets/all_fuels_data.csv +27016 -0
- jackofalltrades-0.0.1/jackofalltrades/datasets/dataset.py +50 -0
- jackofalltrades-0.0.1/jackofalltrades/datasets/london_house_prices.csv +980 -0
- jackofalltrades-0.0.1/jackofalltrades/test/__init__.py +1 -0
- jackofalltrades-0.0.1/jackofalltrades/test/tests.py +76 -0
- jackofalltrades-0.0.1/jackofalltrades.egg-info/PKG-INFO +65 -0
- jackofalltrades-0.0.1/jackofalltrades.egg-info/SOURCES.txt +23 -0
- jackofalltrades-0.0.1/jackofalltrades.egg-info/dependency_links.txt +1 -0
- jackofalltrades-0.0.1/jackofalltrades.egg-info/requires.txt +6 -0
- jackofalltrades-0.0.1/jackofalltrades.egg-info/top_level.txt +1 -0
- jackofalltrades-0.0.1/pyproject.toml +42 -0
- jackofalltrades-0.0.1/setup.cfg +4 -0
- jackofalltrades-0.0.1/setup.py +66 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
MIT license
|
|
2
|
+
Copyright 2024 Sane Punk
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
|
5
|
+
associated documentation files (the “Software”), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
|
|
8
|
+
following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
11
|
+
portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
|
14
|
+
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
15
|
+
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
16
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
17
|
+
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
include README.txt
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: jackofalltrades
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Tired of complex machine learning libraries? Introducing jackofalltrades, a streamlined Python package designed to make machine learning accessible for everyone.
|
|
5
|
+
Home-page: https://github.com/lazy-punk/jackofalltrades
|
|
6
|
+
Author: Sane Punk
|
|
7
|
+
Author-email: Sane Punk <punk00pp@gmail.com>
|
|
8
|
+
License: MIT
|
|
9
|
+
Project-URL: homepage, https://github.com/lazy-punk/jackofalltrades
|
|
10
|
+
Project-URL: Documentation, https://github.com/lazy-punk/jackofalltrades
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: Education
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
18
|
+
Description-Content-Type: text/plain
|
|
19
|
+
License-File: LICENSE.txt
|
|
20
|
+
Requires-Dist: pandas>=2.2.2
|
|
21
|
+
Requires-Dist: numpy>=1.26.4
|
|
22
|
+
Requires-Dist: scikit-learn>=1.5.0
|
|
23
|
+
Requires-Dist: matplotlib>=3.9.0
|
|
24
|
+
Requires-Dist: jax[cpu]>=0.4.28
|
|
25
|
+
Requires-Dist: tqdm>=4.66.4
|
|
26
|
+
|
|
27
|
+
Jack of All Trades: A Simple and User-Friendly Machine Learning Toolkit
|
|
28
|
+
Tired of complex machine learning libraries? Introducing jackofalltrades, a streamlined Python package designed to make machine learning accessible for everyone. Whether you're a beginner eager to learn the fundamentals, an experienced user seeking a simpler alternative for quick experimentation, or an educator looking for a user-friendly teaching tool, jackofalltrades is here to empower you.
|
|
29
|
+
What sets jackofalltrades apart?
|
|
30
|
+
• Simplicity: We prioritize clear, concise functions with minimal parameters and intuitive interfaces. No more struggling to decipher complex syntax or documentation.
|
|
31
|
+
• Ease of Use: Get started quickly with our well-documented functions and a focus on straightforward implementation. You'll be building and evaluating machine learning models in no time.
|
|
32
|
+
• Core Machine Learning Algorithms: Built-in implementations for essential algorithms like linear regression (and more to come!). Experiment with foundational concepts and solve real-world problems efficiently.
|
|
33
|
+
• Compatibility: Seamlessly work with data formats used by popular libraries like scikit-learn. Integrate jackofalltrades into your existing machine learning workflows effortlessly.
|
|
34
|
+
Key Features:
|
|
35
|
+
• Intuitive API: Designed with ease of use in mind, jackofalltrades promotes efficient code writing and rapid learning.
|
|
36
|
+
• Focus on Core Concepts: Gain a solid understanding of machine learning fundamentals without getting bogged down in advanced complexities.
|
|
37
|
+
• Lightweight and Efficient: Streamlined implementation ensures minimal overhead for your projects.
|
|
38
|
+
• Active Development: We're continuously working to expand the library's capabilities and enhance user experience.
|
|
39
|
+
Installation
|
|
40
|
+
Install jackofalltrades using pip:
|
|
41
|
+
# Bash
|
|
42
|
+
```pip install jackofalltrades```
|
|
43
|
+
Usage Example (Linear Regression)
|
|
44
|
+
Here's a quick example demonstrating how to use jackofalltrades for linear regression:
|
|
45
|
+
# Python
|
|
46
|
+
```import jackofalltrades as joft
|
|
47
|
+
from jackofalltrades.datasets import get_data
|
|
48
|
+
|
|
49
|
+
# Load data and split into training and testing sets
|
|
50
|
+
ldset = get_dataset()
|
|
51
|
+
X, y = ldset.get_btc()
|
|
52
|
+
|
|
53
|
+
# Train a linear regression model
|
|
54
|
+
model = joft.Models.LinearRegression(X, y)
|
|
55
|
+
model.fit()
|
|
56
|
+
|
|
57
|
+
# Make predictions and evaluate performance
|
|
58
|
+
y_predicted = model.predict(X)
|
|
59
|
+
model.evaluate(y, y_predicted)```
|
|
60
|
+
For more in-depth explanations, advanced usage examples, and API documentation, please refer to the detailed documentation (coming soon).
|
|
61
|
+
Contributing
|
|
62
|
+
We welcome contributions to jackofalltrades! If you'd like to get involved, please refer to the contribution guidelines (coming soon).
|
|
63
|
+
License
|
|
64
|
+
This project is licensed under the MIT License. See the LICENSE file for details.
|
|
65
|
+
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
Jack of All Trades: A Simple and User-Friendly Machine Learning Toolkit
|
|
2
|
+
Tired of complex machine learning libraries? Introducing jackofalltrades, a streamlined Python package designed to make machine learning accessible for everyone. Whether you're a beginner eager to learn the fundamentals, an experienced user seeking a simpler alternative for quick experimentation, or an educator looking for a user-friendly teaching tool, jackofalltrades is here to empower you.
|
|
3
|
+
What sets jackofalltrades apart?
|
|
4
|
+
• Simplicity: We prioritize clear, concise functions with minimal parameters and intuitive interfaces. No more struggling to decipher complex syntax or documentation.
|
|
5
|
+
• Ease of Use: Get started quickly with our well-documented functions and a focus on straightforward implementation. You'll be building and evaluating machine learning models in no time.
|
|
6
|
+
• Core Machine Learning Algorithms: Built-in implementations for essential algorithms like linear regression (and more to come!). Experiment with foundational concepts and solve real-world problems efficiently.
|
|
7
|
+
• Compatibility: Seamlessly work with data formats used by popular libraries like scikit-learn. Integrate jackofalltrades into your existing machine learning workflows effortlessly.
|
|
8
|
+
Key Features:
|
|
9
|
+
• Intuitive API: Designed with ease of use in mind, jackofalltrades promotes efficient code writing and rapid learning.
|
|
10
|
+
• Focus on Core Concepts: Gain a solid understanding of machine learning fundamentals without getting bogged down in advanced complexities.
|
|
11
|
+
• Lightweight and Efficient: Streamlined implementation ensures minimal overhead for your projects.
|
|
12
|
+
• Active Development: We're continuously working to expand the library's capabilities and enhance user experience.
|
|
13
|
+
Installation
|
|
14
|
+
Install jackofalltrades using pip:
|
|
15
|
+
# Bash
|
|
16
|
+
```pip install jackofalltrades```
|
|
17
|
+
Usage Example (Linear Regression)
|
|
18
|
+
Here's a quick example demonstrating how to use jackofalltrades for linear regression:
|
|
19
|
+
# Python
|
|
20
|
+
```import jackofalltrades as joft
|
|
21
|
+
from jackofalltrades.datasets import get_data
|
|
22
|
+
|
|
23
|
+
# Load data and split into training and testing sets
|
|
24
|
+
ldset = get_dataset()
|
|
25
|
+
X, y = ldset.get_btc()
|
|
26
|
+
|
|
27
|
+
# Train a linear regression model
|
|
28
|
+
model = joft.Models.LinearRegression(X, y)
|
|
29
|
+
model.fit()
|
|
30
|
+
|
|
31
|
+
# Make predictions and evaluate performance
|
|
32
|
+
y_predicted = model.predict(X)
|
|
33
|
+
model.evaluate(y, y_predicted)```
|
|
34
|
+
For more in-depth explanations, advanced usage examples, and API documentation, please refer to the detailed documentation (coming soon).
|
|
35
|
+
Contributing
|
|
36
|
+
We welcome contributions to jackofalltrades! If you'd like to get involved, please refer to the contribution guidelines (coming soon).
|
|
37
|
+
License
|
|
38
|
+
This project is licensed under the MIT License. See the LICENSE file for details.
|
|
39
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .error import Error
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import jax.numpy as np
|
|
2
|
+
from sklearn.metrics import confusion_matrix
|
|
3
|
+
|
|
4
|
+
class Error:
|
|
5
|
+
"""
|
|
6
|
+
Class to calculate various error metrics for regression and classification tasks.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
def __init__(self, y_true, y_predicted) -> None:
|
|
10
|
+
"""
|
|
11
|
+
Initialize the Error class with true and predicted values.
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
y_true (array-like): True values.
|
|
15
|
+
y_predicted (array-like): Predicted values.
|
|
16
|
+
"""
|
|
17
|
+
try:
|
|
18
|
+
self.y_true, self.y_predicted = np.array(y_true, dtype=np.float32), np.array(y_predicted, dtype=np.float32)
|
|
19
|
+
except Exception as e:
|
|
20
|
+
raise Exception(f"Error: {e}")
|
|
21
|
+
|
|
22
|
+
def MSE(self) -> np.float32:
|
|
23
|
+
"""
|
|
24
|
+
Calculate the Mean Squared Error (MSE).
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
np.float32: The calculated MSE value.
|
|
28
|
+
"""
|
|
29
|
+
try:
|
|
30
|
+
return np.mean(np.square((self.y_true - self.y_predicted)))
|
|
31
|
+
except Exception as e:
|
|
32
|
+
raise Exception(f"Error: {e}")
|
|
33
|
+
|
|
34
|
+
def RMSE(self) -> np.float32:
|
|
35
|
+
"""
|
|
36
|
+
Calculate the Root Mean Squared Error (RMSE).
|
|
37
|
+
|
|
38
|
+
Returns:
|
|
39
|
+
np.float32: The calculated RMSE value.
|
|
40
|
+
"""
|
|
41
|
+
try:
|
|
42
|
+
return np.sqrt(np.mean(np.square((self.y_true - self.y_predicted))))
|
|
43
|
+
except Exception as e:
|
|
44
|
+
raise Exception(f"Error: {e}")
|
|
45
|
+
|
|
46
|
+
def MAE(self) -> np.float32:
|
|
47
|
+
"""
|
|
48
|
+
Calculate the Mean Absolute Error (MAE).
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
np.float32: The calculated MAE value.
|
|
52
|
+
"""
|
|
53
|
+
try:
|
|
54
|
+
return np.mean(np.abs((self.y_true - self.y_predicted)))
|
|
55
|
+
except Exception as e:
|
|
56
|
+
raise Exception(f"Error: {e}")
|
|
57
|
+
|
|
58
|
+
def SOAE(self) -> np.float32:
|
|
59
|
+
"""
|
|
60
|
+
Calculate the Sum of Absolute Errors (SOAE).
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
np.float32: The calculated SOAE value.
|
|
64
|
+
"""
|
|
65
|
+
try:
|
|
66
|
+
return np.abs(np.sum(self.y_true - self.y_predicted))
|
|
67
|
+
except Exception as e:
|
|
68
|
+
raise Exception(f"Error: {e}")
|
|
69
|
+
|
|
70
|
+
def SOE(self) -> np.float32:
|
|
71
|
+
"""
|
|
72
|
+
Calculate the Sum of Errors (SOE).
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
np.float32: The calculated SOE value.
|
|
76
|
+
"""
|
|
77
|
+
try:
|
|
78
|
+
return np.sum(self.y_true - self.y_predicted)
|
|
79
|
+
except Exception as e:
|
|
80
|
+
raise Exception(f"Error: {e}")
|
|
81
|
+
|
|
82
|
+
def RSquared(self) -> np.float32:
|
|
83
|
+
"""
|
|
84
|
+
Calculate the R-squared value.
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
np.float32: The calculated R-squared value.
|
|
88
|
+
"""
|
|
89
|
+
try:
|
|
90
|
+
return 1 - (np.sum(np.square(self.y_true - self.y_predicted)) / np.sum(np.square(self.y_true - np.mean(self.y_true))))
|
|
91
|
+
except Exception as e:
|
|
92
|
+
raise Exception(f"Error: {e}")
|
|
93
|
+
|
|
94
|
+
def MAPE(self) -> np.float32:
|
|
95
|
+
"""
|
|
96
|
+
Calculate the Mean Absolute Percentage Error (MAPE).
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
np.float32: The calculated MAPE value.
|
|
100
|
+
"""
|
|
101
|
+
try:
|
|
102
|
+
return np.mean(np.abs((self.y_true - self.y_predicted) / self.y_true)) * 100
|
|
103
|
+
except Exception as e:
|
|
104
|
+
raise Exception(f"Error: {e}")
|
|
105
|
+
|
|
106
|
+
def AdjustedRSquared(self, n, p) -> np.float32:
|
|
107
|
+
"""
|
|
108
|
+
Calculate the Adjusted R-squared value.
|
|
109
|
+
|
|
110
|
+
Args:
|
|
111
|
+
n (int): Number of observations.
|
|
112
|
+
p (int): Number of predictors.
|
|
113
|
+
|
|
114
|
+
Returns:
|
|
115
|
+
np.float32: The calculated Adjusted R-squared value.
|
|
116
|
+
"""
|
|
117
|
+
try:
|
|
118
|
+
return 1 - ((1 - self.RSquared()) * (n - 1) / (n - p - 1))
|
|
119
|
+
except Exception as e:
|
|
120
|
+
raise Exception(f"Error: {e}")
|
|
121
|
+
|
|
122
|
+
def Accuracy(self) -> np.float32:
|
|
123
|
+
"""
|
|
124
|
+
Calculate the Accuracy.
|
|
125
|
+
|
|
126
|
+
Returns:
|
|
127
|
+
np.float32: The calculated Accuracy value.
|
|
128
|
+
"""
|
|
129
|
+
try:
|
|
130
|
+
cm = confusion_matrix(self.y_true, self.y_predicted)
|
|
131
|
+
TN, FP, FN, TP = cm.ravel()
|
|
132
|
+
return (TP + TN) / (TP + TN + FP + FN)
|
|
133
|
+
except Exception as e:
|
|
134
|
+
raise Exception(f"Error: {e}")
|
|
135
|
+
|
|
136
|
+
def Precision(self) -> np.float32:
|
|
137
|
+
"""
|
|
138
|
+
Calculate the Precision.
|
|
139
|
+
|
|
140
|
+
Returns:
|
|
141
|
+
np.float32: The calculated Precision value.
|
|
142
|
+
"""
|
|
143
|
+
try:
|
|
144
|
+
cm = confusion_matrix(self.y_true, self.y_predicted)
|
|
145
|
+
TN, FP, FN, TP = cm.ravel()
|
|
146
|
+
return TP / (TP + FP)
|
|
147
|
+
except Exception as e:
|
|
148
|
+
raise Exception(f"Error: {e}")
|
|
149
|
+
|
|
150
|
+
def Recall(self) -> np.float32:
|
|
151
|
+
"""
|
|
152
|
+
Calculate the Recall.
|
|
153
|
+
|
|
154
|
+
Returns:
|
|
155
|
+
np.float32: The calculated Recall value.
|
|
156
|
+
"""
|
|
157
|
+
try:
|
|
158
|
+
cm = confusion_matrix(self.y_true, self.y_predicted)
|
|
159
|
+
TN, FP, FN, TP = cm.ravel()
|
|
160
|
+
return TP / (TP + FN)
|
|
161
|
+
except Exception as e:
|
|
162
|
+
raise Exception(f"Error: {e}")
|
|
163
|
+
|
|
164
|
+
def F1Score(self) -> np.float32:
|
|
165
|
+
"""
|
|
166
|
+
Calculate the F1 Score.
|
|
167
|
+
|
|
168
|
+
Returns:
|
|
169
|
+
np.float32: The calculated F1 Score value.
|
|
170
|
+
"""
|
|
171
|
+
try:
|
|
172
|
+
cm = confusion_matrix(self.y_true, self.y_predicted)
|
|
173
|
+
TN, FP, FN, TP = cm.ravel()
|
|
174
|
+
return 2 * TP / (2 * TP + FP + FN)
|
|
175
|
+
except Exception as e:
|
|
176
|
+
raise Exception(f"Error: {e}")
|
|
177
|
+
|
|
178
|
+
def Cross_Entropy(self) -> np.float32:
|
|
179
|
+
"""
|
|
180
|
+
Calculate the Cross Entropy.
|
|
181
|
+
|
|
182
|
+
Returns:
|
|
183
|
+
np.float32: The calculated Cross Entropy value.
|
|
184
|
+
"""
|
|
185
|
+
try:
|
|
186
|
+
return (1 - self.y_true) * np.log(1 - self.y_predicted) + self.y_true * np.log(self.y_predicted)
|
|
187
|
+
except Exception as e: # Use the actual exception object 'e'
|
|
188
|
+
# Consider a more informative error message or handling strategy
|
|
189
|
+
print(f"Error during Cross Entropy calculation: {e}")
|
|
190
|
+
return np.nan # Or a more appropriate default value
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pandas as pd
|
|
3
|
+
from sklearn.model_selection import train_test_split
|
|
4
|
+
from matplotlib import pyplot as plt
|
|
5
|
+
from tqdm import tqdm as tqdm
|
|
6
|
+
from sklearn.preprocessing import StandardScaler
|
|
7
|
+
|
|
8
|
+
import sklearn.metrics as metrics
|
|
9
|
+
|
|
10
|
+
# Class for implementing Linear Regression
|
|
11
|
+
class LinearRegression:
|
|
12
|
+
def __init__(self, learning_rate : float = 0.03, epochs : int = 10000, regularization_strength: float = 0.1, data_regularization = True) -> None:
|
|
13
|
+
"""
|
|
14
|
+
Initialize the LinearRegression object.
|
|
15
|
+
|
|
16
|
+
Parameters:
|
|
17
|
+
- learning_rate: Learning rate for gradient descent (default = 0.03).
|
|
18
|
+
- epochs: Number of training iterations (default = 10000).
|
|
19
|
+
- regularization_strength (default = 0.1)
|
|
20
|
+
"""
|
|
21
|
+
try:
|
|
22
|
+
self.data_regularization = data_regularization
|
|
23
|
+
self.regularization_strength = regularization_strength
|
|
24
|
+
self.learning_rate = learning_rate
|
|
25
|
+
self.epochs = epochs
|
|
26
|
+
except Exception as e:
|
|
27
|
+
print("An error occurred during initialization:", str(e))
|
|
28
|
+
|
|
29
|
+
def Standard(self, X):
|
|
30
|
+
SS = StandardScaler()
|
|
31
|
+
g = SS.fit(X)
|
|
32
|
+
g = SS.transform(X)
|
|
33
|
+
return g
|
|
34
|
+
|
|
35
|
+
def fit(self, X: pd.DataFrame , y: pd.Series) -> None:
|
|
36
|
+
"""
|
|
37
|
+
Train the linear regression model using gradient descent.
|
|
38
|
+
Parameters:
|
|
39
|
+
- X: Input features as a pandas DataFrame.
|
|
40
|
+
- y: Target variable as a pandas Series.
|
|
41
|
+
"""
|
|
42
|
+
try:
|
|
43
|
+
self.X, self.y = np.array(X, dtype= np.float32), np.array(y, dtype = np.float32)
|
|
44
|
+
if self.data_regularization == True:
|
|
45
|
+
self.X = self.Standard(self.X)
|
|
46
|
+
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(self.X, self.y, test_size=0.01, random_state=42)
|
|
47
|
+
self.m, self.n = self.X_train.shape
|
|
48
|
+
self.w = np.random.normal(size=self.n)
|
|
49
|
+
self.b = 0
|
|
50
|
+
y_pred = np.dot(self.X_train, self.w) + self.b
|
|
51
|
+
self.cost = []
|
|
52
|
+
self.epoch = []
|
|
53
|
+
description = tqdm(range(self.epochs))
|
|
54
|
+
for i in description:
|
|
55
|
+
y_pred = np.dot(self.X_train, self.w) + self.b
|
|
56
|
+
dw = (1/self.m) * np.dot(self.X_train.T, (y_pred - self.y_train)) + (2 * self.regularization_strength / self.m) * self.w
|
|
57
|
+
db = (1/self.m) * np.sum(y_pred - self.y_train)
|
|
58
|
+
self.w -= self.learning_rate * dw
|
|
59
|
+
self.b -= self.learning_rate * db
|
|
60
|
+
self.cost.append(np.mean(np.square(y_pred - self.y_train)))
|
|
61
|
+
self.epoch.append(i)
|
|
62
|
+
description.set_description(f"Cost: {self.cost[-1]}")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
except Exception as e:
|
|
66
|
+
print("An error occurred during fitting:", str(e))
|
|
67
|
+
|
|
68
|
+
def predict(self, X_test: pd.DataFrame) -> np.ndarray:
|
|
69
|
+
"""
|
|
70
|
+
Predict the target variable for the given input features.
|
|
71
|
+
|
|
72
|
+
Parameters:
|
|
73
|
+
- X_test: Input features for prediction as a pandas DataFrame.
|
|
74
|
+
|
|
75
|
+
Returns:
|
|
76
|
+
- Predicted target variable as a numpy array.
|
|
77
|
+
"""
|
|
78
|
+
if self.data_regularization == True:
|
|
79
|
+
X_test = self.Standard(X_test)
|
|
80
|
+
return np.dot(np.array(X_test, dtype = np.float32), self.w) + self.b
|
|
81
|
+
|
|
82
|
+
def plot_cost(self) -> None:
|
|
83
|
+
"""
|
|
84
|
+
Plot the cost function over training iterations.
|
|
85
|
+
"""
|
|
86
|
+
plt.plot(self.cost ,self.epoch)
|
|
87
|
+
plt.show()
|
|
88
|
+
|
|
89
|
+
def evaluate(self, y_true : np.ndarray, y_pred : np.ndarray) -> None:
|
|
90
|
+
"""
|
|
91
|
+
Evaluate the model using the R-squared metric.
|
|
92
|
+
|
|
93
|
+
Parameters:
|
|
94
|
+
- X_test: Test input features as a numpy array.
|
|
95
|
+
- y_test: Test target variable as a numpy array.
|
|
96
|
+
"""
|
|
97
|
+
print(metrics.r2_score(y_true, y_pred))
|
|
98
|
+
|
|
99
|
+
class LogisticRegression:
|
|
100
|
+
|
|
101
|
+
def __init__(self, learning_rate : float = 0.03, epochs : int = 10000, regularization_strength: float = 0.1, data_regularization = True) -> None:
|
|
102
|
+
"""
|
|
103
|
+
Initialize the LogisticRegression object.
|
|
104
|
+
|
|
105
|
+
Parameters:
|
|
106
|
+
- learning_rate: Learning rate for gradient descent (default = 0.03).
|
|
107
|
+
- epochs: Number of training iterations (default = 10000).
|
|
108
|
+
- regularization_strength (default = 0.1)
|
|
109
|
+
"""
|
|
110
|
+
try:
|
|
111
|
+
self.data_regularization = data_regularization
|
|
112
|
+
self.regularization_strength = regularization_strength
|
|
113
|
+
self.learning_rate = learning_rate
|
|
114
|
+
self.epochs = epochs
|
|
115
|
+
except Exception as e:
|
|
116
|
+
print("An error occurred during initialization:", str(e))
|
|
117
|
+
|
|
118
|
+
def Standard(self, X):
|
|
119
|
+
SS = StandardScaler()
|
|
120
|
+
g = SS.fit(X)
|
|
121
|
+
g = SS.transform(X)
|
|
122
|
+
return g
|
|
123
|
+
|
|
124
|
+
def sigmoid(self, z: np.ndarray) -> np.ndarray:
|
|
125
|
+
"""
|
|
126
|
+
Calculate the sigmoid function.
|
|
127
|
+
|
|
128
|
+
Parameters:
|
|
129
|
+
- z: Input value.
|
|
130
|
+
|
|
131
|
+
Returns:
|
|
132
|
+
- Sigmoid value.
|
|
133
|
+
"""
|
|
134
|
+
return 1 / (1 + np.exp(-z))
|
|
135
|
+
|
|
136
|
+
def fit(self, X: pd.DataFrame , y: pd.Series) -> None:
|
|
137
|
+
"""
|
|
138
|
+
Train the logistic regression model using gradient descent.
|
|
139
|
+
Parameters:
|
|
140
|
+
- X: Input features as a pandas DataFrame.
|
|
141
|
+
- y: Target variable as a pandas Series.
|
|
142
|
+
"""
|
|
143
|
+
try:
|
|
144
|
+
self.X, self.y = np.array(X, dtype= np.float32), np.array(y, dtype = np.float32)
|
|
145
|
+
if self.data_regularization == True:
|
|
146
|
+
self.X = self.Standard(self.X)
|
|
147
|
+
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(self.X / np.max(self.X), self.y, test_size=0.01, random_state=42)
|
|
148
|
+
self.m, self.n = self.X_train.shape
|
|
149
|
+
self.w = np.zeros(self.n)
|
|
150
|
+
self.b = 0
|
|
151
|
+
y_pred = self.sigmoid(np.dot(self.X_train, self.w) + self.b)
|
|
152
|
+
self.cost = []
|
|
153
|
+
self.epoch = []
|
|
154
|
+
description = tqdm(range(self.epochs))
|
|
155
|
+
for i in description:
|
|
156
|
+
y_pred = self.sigmoid(np.dot(self.X_train, self.w) + self.b)
|
|
157
|
+
loss = -1/self.m * np.sum(self.y_train * np.log(y_pred) + (1 - self.y_train) * np.log(1 - y_pred))
|
|
158
|
+
dw = (1/self.m) * np.dot(self.X_train.T, (y_pred - self.y_train)) + (2 * self.regularization_strength / self.m) * self.w
|
|
159
|
+
db = (1/self.m) * np.sum(y_pred - self.y_train)
|
|
160
|
+
self.w -= self.learning_rate * dw
|
|
161
|
+
self.b -= self.learning_rate * db
|
|
162
|
+
self.cost.append(loss)
|
|
163
|
+
self.epoch.append(i)
|
|
164
|
+
description.set_description(f"Cost: {self.cost[-1]}")
|
|
165
|
+
|
|
166
|
+
except Exception as e:
|
|
167
|
+
print("An error occurred during fitting:", str(e))
|
|
168
|
+
|
|
169
|
+
def predict(self, X_test: pd.DataFrame) -> np.ndarray:
|
|
170
|
+
"""
|
|
171
|
+
Predict the target variable for the given input features.
|
|
172
|
+
|
|
173
|
+
Parameters:
|
|
174
|
+
- X_test: Input features for prediction as a pandas DataFrame.
|
|
175
|
+
|
|
176
|
+
Returns:
|
|
177
|
+
- Predicted target variable as a numpy array.
|
|
178
|
+
"""
|
|
179
|
+
if self.data_regularization == True:
|
|
180
|
+
X_test = self.Standard(X_test)
|
|
181
|
+
return (self.sigmoid(np.dot(np.array(X_test , dtype = np.float32) / np.max(self.X), self.w) + self.b) > 0.5).astype(int)
|
|
182
|
+
|
|
183
|
+
def plot_cost(self) -> None:
|
|
184
|
+
"""
|
|
185
|
+
Plot the cost function over training iterations.
|
|
186
|
+
"""
|
|
187
|
+
plt.plot(self.cost ,self.epoch)
|
|
188
|
+
plt.show()
|
|
189
|
+
|
|
190
|
+
def evaluate(self, y_true : np.ndarray, y_predicted : np.ndarray) -> None:
|
|
191
|
+
"""
|
|
192
|
+
Evaluate the model using the R-squared metric.
|
|
193
|
+
|
|
194
|
+
Parameters:
|
|
195
|
+
- X_test: Test input features as a numpy array.
|
|
196
|
+
- y_test: Test target variable as a numpy array.
|
|
197
|
+
"""
|
|
198
|
+
print(metrics.accuracy_score(y_true, (y_predicted > 0.5).astype(int)))
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .Regression import LinearRegression, LogisticRegression
|
|
File without changes
|