rslearn-ML 1.0.0__py3-none-any.whl

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.
rslearn/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ from . import linear_model, metrics, preprocessing, model_selection
2
+
3
+ __all__ = ["linear_model", "metrics", "preprocessing", "model_selection"]
@@ -0,0 +1,242 @@
1
+ """
2
+ Things : -
3
+
4
+ # it is linear regression
5
+ y = m1x1 + m1x2 + m3x3 + ... + MnXn + b
6
+
7
+ y = prediction
8
+ m = weight
9
+ x = value
10
+ b = bias
11
+
12
+ loss = prediction - real_val
13
+ dw = gradient descent of weight
14
+ db = gradient descenf of bias
15
+
16
+ It uses Gradients so, Use `StandardScaler` or `MinMaxScaler` for better result
17
+
18
+ Scalers...
19
+ >>> from rslearn.preprocessing import StandardScaler, MinMaxScaler
20
+ Read READNE.md or Documentation for More Information about their Functions
21
+ """
22
+
23
+ import numpy as np
24
+ from rslearn.metrics import mse
25
+
26
+ class LinearRegression():
27
+
28
+
29
+ def __init__(self, regulization=None, alpha : float = 0.1, l1_ratio=0.5):
30
+
31
+ """
32
+ Linear Regression
33
+ ------------------------
34
+
35
+ linear Regression for 1D and 2D metrics arrays using gradient descents and regulization
36
+ use Scalers like MinMaxScaler or StandardScaler before fitting for haldle large value
37
+
38
+ Example
39
+ --------
40
+ regulization: regulizing option to avoid overfitting
41
+ options: `l1` for Lasso
42
+ `l2` for Ridge
43
+ `elastic_net` for elastic_net
44
+
45
+ Default: None, For No regulization.
46
+
47
+ alpha: alpha value for Ridge, Lasso, ElasticNet
48
+ Default: 0.1
49
+
50
+ l1_ratio: Lasso Ratio for Better ElasticNet Gradient and MSE
51
+ Default: 0.5
52
+
53
+ Functions
54
+ ---------
55
+ fit()
56
+ Function for Train Model | use MinMaxScaler for good Computation and Prediction
57
+ Parameters - given in Function
58
+
59
+ get_weight_bias()
60
+ Returns Selected weight and Bias for minimum loss
61
+
62
+ predict()
63
+ Prediction generator from Model
64
+
65
+
66
+ Example
67
+ -------
68
+
69
+ >>> from rslearn.linear_model import LinearRegression
70
+ >>> Model = LinearRegression()
71
+ >>> X = np.array([10, 20, 30]) # List also works.
72
+ >>> y = np.array([5, 10, 15])
73
+ >>> Model.fit(X, y) # You can change learning_rate too
74
+ >>> print(f"Weight & Bias: {Model.get_weight_bias()}")
75
+ >>> prediction = Model.predict(np.array([40, 50]))
76
+ >>> print(f"Prediction: {prediction}")
77
+
78
+
79
+
80
+ """
81
+
82
+ self.weights = None
83
+ self.bias = None
84
+
85
+ valid_params = {"l1", "l2", "elastic_net", None}
86
+ if regulization not in valid_params:
87
+ raise ValueError(f"regulization parameter is not supported, supported Parameters {valid_params}")
88
+
89
+ self.caclucate_error = self._regulizing_linear_helper(regulization=regulization, alpha=alpha, l1_ratio=l1_ratio)
90
+
91
+
92
+
93
+
94
+ def fit(self,
95
+ X ,
96
+ y ,
97
+ weights= None,
98
+ bias = None,
99
+ learning_rate : float = 0.01,
100
+ min_loss : float = 0.2,
101
+ max_itr : int = 18000
102
+ ):
103
+ """
104
+
105
+ Input Param*
106
+ __________
107
+ X = Data to Train 1D array, Dtype = np.array
108
+
109
+ Y = True value a.k.a. original prediction 1D array, Dtype = np.array
110
+
111
+ max_itr = loop to update weight and bias, Dtype = int and default = 18000 | No Input need
112
+
113
+ learning_rate = how fast weights should update, Dtype = float, Default = 0.01
114
+
115
+ weights = enter custom weight | optional
116
+
117
+ bias = enter custom bias | optional
118
+
119
+ min_loss = minimum loss where to stop the loop Default = 0.2 and its almost best for gradient descent
120
+ -----------------
121
+
122
+ Change the `learning_rate` or Use `Scalers` if output or weights contains 'e' e.g -1.8038873e+163
123
+ """
124
+
125
+
126
+
127
+ X = np.array(X)
128
+ y = np.array(y).reshape(-1)
129
+
130
+ if X.ndim == 1:
131
+ X = X.reshape(-1, 1)
132
+
133
+ n_samples, n_feature = X.shape
134
+
135
+ np.random.seed(7)
136
+ if weights is None:
137
+ weights = np.random.uniform(0.2, 3, n_feature)
138
+
139
+ if bias is None:
140
+ bias = 0
141
+
142
+ iteration = 0
143
+
144
+ while iteration < max_itr:
145
+ pred = np.dot(X, weights) + bias # prediction
146
+
147
+ mse_error = self.caclucate_error.get_error(y_true=y, y_pred=pred, weights=weights)
148
+
149
+ if mse_error <= min_loss:
150
+ print(f"Model Succesfully Fitted at #{iteration} iteration")
151
+ break
152
+
153
+ loss = pred - y # Loss for Gradients
154
+ dw = (2/n_samples) * np.dot(X.T, loss) + self.caclucate_error.get_weight_gradient(weights=weights)
155
+ db = (2/n_samples) * np.sum(loss)
156
+
157
+ weights -= learning_rate * dw
158
+ bias -= learning_rate * db
159
+
160
+ if np.isnan(weights).any() or np.isnan(bias):
161
+ print("NaN detected, stopping training, Use Scalers to avoid it")
162
+ break
163
+
164
+ iteration += 1
165
+
166
+
167
+
168
+ self.weights = weights
169
+ self.bias = bias
170
+
171
+ def get_weight_bias(self) -> np.array:
172
+ """Input = None,
173
+ O/P - (np.array, float64)
174
+ >>> weights, bias = Model.get_weight_bias()
175
+ """
176
+
177
+ return (self.weights, self.bias)
178
+
179
+ def predict(self, new_data : np.array) -> np.array:
180
+ """
181
+ Input Format = 1D or 2D np.array
182
+ Output Format = 1D np.array
183
+ """
184
+ if len(new_data) == 0:
185
+ raise ValueError("Got Empty Array")
186
+
187
+ new_data = np.array(new_data)
188
+
189
+ if new_data.ndim == 1:
190
+ new_data = new_data.reshape(-1, 1)
191
+
192
+ return (np.dot(new_data, self.weights) + self.bias).round(2)
193
+
194
+ class _regulizing_linear_helper:
195
+ def __init__(self, alpha=0.1, regulization=None, l1_ratio = 0.5):
196
+ self.alpha = alpha
197
+ self.regulization = regulization
198
+ self.l1_ratio = l1_ratio
199
+
200
+ def get_error(self, y_true, y_pred, weights):
201
+ mse_error = mse(y_true, y_pred)
202
+ if self.regulization is None:
203
+ return mse_error
204
+
205
+ if self.regulization == "l1":
206
+ reg = self.alpha * np.sum(np.abs(weights))
207
+ return mse_error + reg
208
+
209
+ if self.regulization == "l2":
210
+ reg = self.alpha * np.sum(np.square(weights))
211
+ return mse_error + reg
212
+
213
+ if self.regulization == "elastic_net":
214
+ l1 = self.alpha * self.l1_ratio
215
+ l2 = self.alpha * (1 - self.l1_ratio)
216
+
217
+ reg = l1 * np.sum(np.abs(weights)) + l2 * np.sum(np.square(weights))
218
+ return mse_error + reg
219
+
220
+ def get_weight_gradient(self, weights):
221
+ if self.regulization == "l1":
222
+ return self.alpha * np.sign(weights)
223
+
224
+ if self.regulization == "l2":
225
+ return 2 * self.alpha * weights
226
+
227
+ if self.regulization == "elastic_net":
228
+ l1 = self.alpha * self.l1_ratio
229
+ l2 = self.alpha * (1 - self.l1_ratio)
230
+
231
+ return l1 * np.sign(weights) + 2 * l2 * weights
232
+
233
+ return 0
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+ if __name__ == "__main__":
242
+ Model = LinearRegression()
@@ -0,0 +1,212 @@
1
+ """
2
+ Logistic Regression implementation using Gradient Descent.
3
+
4
+ Notes
5
+ -----
6
+ - This implementation uses gradient descent for optimization.
7
+ - Feature scaling is highly recommended for better convergence and performance.
8
+
9
+ Recommended preprocessing:
10
+ from rslearn.preprocessing import StandardScaler, MinMaxScaler
11
+
12
+ Example:
13
+ scaler = StandardScaler()
14
+ X = scaler.fit_transform(X)
15
+
16
+ - Without scaling, the model may converge slowly or produce suboptimal results.
17
+ """
18
+
19
+ import numpy as np
20
+
21
+
22
+ class LogisticRegression:
23
+
24
+ """
25
+ Logistic Regression
26
+ -------------------
27
+ Logistic Regression for 1D and 2D metrics both with binary and Catogirical classification both supported
28
+
29
+ use any Scaler for better result and accuracy specially in catogirical classification
30
+
31
+ Parameters
32
+ ----------
33
+
34
+ solver: liblinear/solver/auto
35
+ liblinear for Binary Classification
36
+ saga for Catogirical Classification
37
+ auto for automaticly chooses (Default)
38
+
39
+ lr: learning_rate
40
+ Default `0.01`
41
+
42
+ Methords
43
+ --------
44
+ fit: function for fitting the model
45
+ Parameters in function doc string
46
+
47
+ predict: function for prediction after fitting
48
+
49
+ Example
50
+ -------
51
+ >>> from rslearn.linear_model import LogisticRegression
52
+ >>> Model = LogisticRegression()
53
+ >>> Model.fit(X, y)
54
+ >>> pred = Model.predict(X_test)
55
+
56
+
57
+ """
58
+
59
+ def __init__(self, solver="auto", lr = 0.01):
60
+ if solver not in ["saga", "liblinear", "auto"]:
61
+ raise ValueError(f"Solver Must be saga or liblinear {solver}")
62
+
63
+ self.solver = solver
64
+ self.lr = lr
65
+ self.weights = None
66
+ self.bias = None
67
+
68
+ # Probablity predictor for catogirical classification
69
+ def predict_proba(self, X):
70
+ X = np.asarray(X)
71
+
72
+ if self.solver == "liblinear":
73
+ z = X @ self.weights + self.bias
74
+ probs_1 = 1 / (1 + np.exp(-z))
75
+ probs_0 = 1 - probs_1
76
+ return np.vstack((probs_0, probs_1)).T
77
+
78
+ else:
79
+ probs = [m.predict_proba(X)[:, 1] for m in self._cato_model.models]
80
+ probs = np.vstack(probs).T
81
+
82
+ # normalize
83
+ probs = probs / probs.sum(axis=1, keepdims=True)
84
+ return probs
85
+
86
+ def fit(self, X, y, max_itr = 1000, ):
87
+
88
+ """
89
+ Function for fitting Logistic Regression Model
90
+
91
+ Parameters
92
+ ----------
93
+ X: feature set for model training
94
+ 2D or 1D metrics | `np.array`, `DataFrame`
95
+
96
+ y: correct value for X features set
97
+ 1D array | `np.array`
98
+
99
+ max_itr: maximum itration to loop though
100
+ Default `1000`
101
+
102
+ Returns
103
+ -------
104
+ None
105
+ """
106
+
107
+ X = np.asarray(X)
108
+ y = np.asarray(y)
109
+ y = y.reshape(-1)
110
+
111
+ if len(X) != len(y):
112
+ raise ValueError(f"X and y are diffrent size {(len(X), len(y))}")
113
+
114
+ # Handling solvers in auto mode
115
+ if self.solver == "auto":
116
+ unique = np.unique(y)
117
+ if len(unique) == 2:
118
+ self.solver = "liblinear"
119
+ else:
120
+ self.solver = "saga"
121
+
122
+ # Diffrent condition for fit
123
+ if self.solver == "liblinear":
124
+ Model = _binary_fit(X=X, y=y, lr=self.lr)
125
+ self.weights, self.bias = Model.fit()
126
+
127
+ else:
128
+ Model = _catogirical_fit(X=X, y=y)
129
+ Model.fit()
130
+ self._cato_model = Model
131
+
132
+ def predict(self, X):
133
+
134
+ """
135
+ Function for predict for Logistic Regression
136
+
137
+ Parameter
138
+ --------
139
+ X: new Data for prediction
140
+ n_sample, n_features of X should be same as data on which model trained
141
+ preferd `np.array`, `DataFrame`
142
+ """
143
+
144
+ X = np.asarray(X)
145
+
146
+
147
+ probs = self.predict_proba(X)
148
+
149
+ if probs.shape[1] == 2:
150
+ return (probs[:, 1] >= 0.5).astype(int)
151
+
152
+ else:
153
+ return np.argmax(probs, axis=1)
154
+
155
+
156
+
157
+ # For liblinear (Default)
158
+ class _binary_fit:
159
+ def __init__(self,X , y, lr, max_itr : int = 1000):
160
+ self.lr = lr
161
+ self.X = X
162
+ self.y = y
163
+ self.max_itr = max_itr
164
+
165
+ def _sigmoid(self, z):
166
+ return 1 / (1 + np.exp(-z))
167
+
168
+ def fit(self):
169
+ X = self.X
170
+ y = self.y
171
+ n_rows, n_features = X.shape
172
+
173
+ weights = np.zeros(n_features)
174
+ bias = 0
175
+
176
+ for _ in range(self.max_itr):
177
+ z = np.dot(X, weights) + bias
178
+ y_pred = self._sigmoid(z)
179
+
180
+ # Gradients
181
+ dw = (1/n_rows) * np.dot(X.T, (y_pred - y))
182
+ db = (1/n_rows) * np.sum(y_pred - y)
183
+
184
+ # update
185
+ weights -= self.lr * dw
186
+ bias -= self.lr * db
187
+
188
+ return weights, bias
189
+
190
+
191
+ # For Catogrical
192
+ class _catogirical_fit:
193
+ def __init__(self, X, y):
194
+ self.X = X
195
+ self.y = y
196
+
197
+ def fit(self):
198
+ X = self.X
199
+ y = self.y
200
+ self.models = []
201
+ self.classes = np.unique(y)
202
+
203
+ for c in self.classes:
204
+ model = LogisticRegression(solver="liblinear")
205
+ y_bin = (y == c).astype(int)
206
+ model.fit(X, y_bin)
207
+ self.models.append(model)
208
+
209
+ def predict(self, X):
210
+ probs = [m.predict_proba(X) for m in self.models]
211
+ probs = np.vstack(probs).T
212
+ return np.argmax(probs, axis=1)
@@ -0,0 +1,4 @@
1
+ from ._LinearRegression import LinearRegression
2
+ from ._LogisticRegression import LogisticRegression
3
+ from ._regulizations import Ridge, Lasso, ElasticNet
4
+ __all__ = ["LinearRegression", "LogisticRegression", "Ridge", "Lasso", "ElasticNet"]
@@ -0,0 +1,231 @@
1
+ from rslearn.linear_model import LinearRegression
2
+
3
+ """
4
+ This File Contains regulizing algorithams to avoid overfitting though the model
5
+
6
+ Algorithams Contains
7
+ ----------------------
8
+ - `Lasso`
9
+ - `Ridge`
10
+ - `ElasticNet`
11
+ """
12
+
13
+ class Lasso:
14
+
15
+ """
16
+ Lasso l1 regulization for Avoid Overfitting though abs()
17
+ NOTE: It uses LinearRegression Internly So make Sure to Scale youre Data and enter False in Scale Parameter in `fit()`
18
+
19
+ Parameters
20
+ -----------
21
+
22
+ alpha: alpha value for Ridge, Lasso, ElasticNet
23
+ Default: 0.1
24
+
25
+ l1_ratio: Lasso Ratio for Better ElasticNet Gradient and MSE
26
+ Default: 0.5
27
+
28
+ Returns
29
+ -------
30
+ None
31
+
32
+ Example
33
+ -------
34
+
35
+ >>> from rslearn.linear_model import Lasso
36
+ >>> LassoR = Lasso() # Using Default Parameters
37
+ >>> LassoR.fit(X, y, Scale=True) # Auto Scales Basicly for better performence use StandardScaler
38
+ >>> LassoR.predict(X_new)
39
+
40
+ """
41
+
42
+ def __init__(self, alpha=0.1, l1_score=0.5):
43
+ self.alpha = alpha
44
+ self.l1_score = l1_score
45
+ self.weights, self.bias = None
46
+
47
+ def fit(self, X, y, Scale=True):
48
+
49
+ """
50
+ `fit()` Function For `Lasso` to Train The Model
51
+
52
+ Parameters
53
+ ----------
54
+
55
+ X: array-like, 1D or 2D metrics for train model
56
+
57
+ y: array-like, 1D or 2D metrics of Real Value for X features
58
+
59
+ Scaler: option to Scale Automaticly
60
+ Default: True, Use `StandardScaler` and select `False` For better results
61
+
62
+ Returns
63
+ -------
64
+ None
65
+ """
66
+
67
+ model = LinearRegression(regulization="l1", alpha=self.alpha, l1_ratio=self.l1_score)
68
+
69
+ if Scale: # Use Scalers for better performance
70
+ X = X/max(X)
71
+ y = y/max(y)
72
+
73
+
74
+ model.fit(X, y)
75
+
76
+ self.weights, self.bias = model.get_weight_bias()
77
+ self.model = model
78
+
79
+ def predict(self, X_new):
80
+ prediction = self.model.predict(X_new)
81
+ return prediction
82
+
83
+ def get_weight_bias(self):
84
+ return (self.weights, self.bias)
85
+
86
+
87
+ class Ridge:
88
+ """
89
+ Ridge `l2` regulization for Avoid Overfitting though square()
90
+ NOTE: It uses LinearRegression Internly So make Sure to Scale youre Data and enter False in Scale Parameter in `fit()`
91
+
92
+ Parameters
93
+ -----------
94
+
95
+ alpha: alpha value for Ridge, Lasso, ElasticNet
96
+ Default: 0.1
97
+
98
+ l1_ratio: Lasso Ratio for Better ElasticNet Gradient and MSE
99
+ Default: 0.5
100
+
101
+ Returns
102
+ -------
103
+ None
104
+
105
+ Example
106
+ -------
107
+
108
+ >>> from rslearn.linear_model import Ridge
109
+ >>> RidgeR = Ridge() # Using Default Parameters
110
+ >>> RidgeR.fit(X, y, Scale=True) # Auto Scales Basicly for better performence use StandardScaler
111
+ >>> RidgeR.predict(X_new)
112
+
113
+ """
114
+
115
+
116
+ def __init__(self, alpha=0.1, l1_score=0.5):
117
+ self.alpha = alpha
118
+ self.l1_score = l1_score
119
+ self.weights, self.bias = None
120
+
121
+ def fit(self, X, y, Scale=True):
122
+ """
123
+ `fit()` Function For `Ridge` to Train The Model
124
+
125
+ Parameters
126
+ ----------
127
+
128
+ X: array-like, 1D or 2D metrics for train model
129
+
130
+ y: array-like, 1D or 2D metrics of Real Value for X features
131
+
132
+ Scaler: option to Scale Automaticly
133
+ Default: True, Use `StandardScaler` and select `False` For better results
134
+
135
+ Returns
136
+ -------
137
+ None
138
+ """
139
+
140
+
141
+ model = LinearRegression(regulization="l2", alpha=self.alpha, l1_ratio=self.l1_score)
142
+
143
+ if Scale: # Use Scalers for better performance
144
+ X = X/max(X)
145
+ y = y/max(y)
146
+
147
+
148
+ model.fit(X, y)
149
+
150
+ self.weights, self.bias = model.get_weight_bias()
151
+ self.model = model
152
+
153
+ def predict(self, X_new):
154
+ prediction = self.model.predict(X_new)
155
+ return prediction
156
+
157
+ def get_weight_bias(self):
158
+ return (self.weights, self.bias)
159
+
160
+ class ElasticNet:
161
+ """
162
+ `ElasticNet` regulization for Avoid Overfitting by Combination of `l1`, `l2`
163
+ NOTE: It uses LinearRegression Internly So make Sure to Scale youre Data and enter False in Scale Parameter in `fit()`
164
+
165
+ Parameters
166
+ -----------
167
+
168
+ alpha: alpha value for Ridge, Lasso, ElasticNet
169
+ Default: 0.1
170
+
171
+ l1_ratio: Lasso Ratio for Better ElasticNet Gradient and MSE
172
+ Default: 0.5
173
+
174
+ Returns
175
+ -------
176
+ None
177
+
178
+ Example
179
+ -------
180
+
181
+ >>> from rslearn.linear_model import ElasticNet
182
+ >>> En = ElasticNet() # Using Default Parameters
183
+ >>> En.fit(X, y, Scale=True) # Auto Scales Basicly for better performence use StandardScaler
184
+ >>> En.predict(X_new)
185
+
186
+ """
187
+
188
+
189
+ def __init__(self, alpha=0.1, l1_score=0.5):
190
+ self.alpha = alpha
191
+ self.l1_score = l1_score
192
+ self.weights, self.bias = None
193
+
194
+ def fit(self, X, y, Scale=True):
195
+ """
196
+ `fit()` Function For `ElasticNet` to Train The Model
197
+
198
+ Parameters
199
+ ----------
200
+
201
+ X: array-like, 1D or 2D metrics for train model
202
+
203
+ y: array-like, 1D or 2D metrics of Real Value for X features
204
+
205
+ Scaler: option to Scale Automaticly
206
+ Default: True, Use `StandardScaler` and select `False` For better results
207
+
208
+ Returns
209
+ -------
210
+ None
211
+ """
212
+
213
+
214
+ model = LinearRegression(regulization="elastic_net", alpha=self.alpha, l1_ratio=self.l1_score)
215
+
216
+ if Scale: # Use Scalers for better performance
217
+ X = X/max(X)
218
+ y = y/max(y)
219
+
220
+
221
+ model.fit(X, y)
222
+
223
+ self.weights, self.bias = model.get_weight_bias()
224
+ self.model = model
225
+
226
+ def predict(self, X_new):
227
+ prediction = self.model.predict(X_new)
228
+ return prediction
229
+
230
+ def get_weight_bias(self):
231
+ return (self.weights, self.bias)
@@ -0,0 +1,3 @@
1
+ from ._regression import r2_score, mse, mae, rmse
2
+
3
+ __all__ = ["r2_score", "mse", "mae", "rmse"]