linearboost 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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) [2024] [Hamidreza Keshavarz, Reza Rawassizadeh]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,20 @@
1
+ Metadata-Version: 2.1
2
+ Name: linearboost
3
+ Version: 0.0.1
4
+ Summary: LinearBoost Python Package
5
+ Author-email: Hamidreza Keshavarz <hamid9@outlook.com>, Reza Rawassizadeh <rezar@bu.edu>
6
+ License: MIT License
7
+ Project-URL: Homepage, https://github.com/LinearBoost/linearboost-classifier
8
+ Project-URL: Documentation, https://linearboost.readthedocs.io
9
+ Project-URL: Issues, https://github.com/LinearBoost/linearboost-classifier/issues
10
+ Keywords: classification,classifier,linear,adaboost,boosting,boost
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Operating System :: OS Independent
14
+ Requires-Python: >=3.8
15
+ Description-Content-Type: text/markdown
16
+ License-File: LICENSE
17
+ Requires-Dist: numpy>=1.24.3
18
+ Requires-Dist: scikit-learn>=1.2.2
19
+
20
+ LinearBoost is a classification algorithm based on boosting a linear classifier named SEFR. It can be used for classification tasks.
@@ -0,0 +1 @@
1
+ LinearBoost is a classification algorithm based on boosting a linear classifier named SEFR. It can be used for classification tasks.
@@ -0,0 +1,30 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "linearboost"
7
+ version = "0.0.1"
8
+ authors = [
9
+ { name="Hamidreza Keshavarz", email="hamid9@outlook.com" },
10
+ { name="Reza Rawassizadeh", email="rezar@bu.edu"}
11
+ ]
12
+ dependencies = [
13
+ "numpy>=1.24.3",
14
+ "scikit-learn>=1.2.2",
15
+ ]
16
+ description = "LinearBoost Python Package"
17
+ readme = "README.md"
18
+ requires-python = ">=3.8"
19
+ classifiers = [
20
+ "Programming Language :: Python :: 3",
21
+ "License :: OSI Approved :: MIT License",
22
+ "Operating System :: OS Independent",
23
+ ]
24
+
25
+ license = {text = "MIT License"}
26
+ keywords = ["classification", "classifier", "linear", "adaboost", "boosting", "boost"]
27
+ [project.urls]
28
+ Homepage = "https://github.com/LinearBoost/linearboost-classifier"
29
+ Documentation = "https://linearboost.readthedocs.io"
30
+ Issues = "https://github.com/LinearBoost/linearboost-classifier/issues"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,166 @@
1
+ import numpy as np
2
+ from sklearn.base import BaseEstimator, ClassifierMixin
3
+ from sklearn.preprocessing import LabelEncoder, MinMaxScaler
4
+ from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier
5
+
6
+ class SEFR(BaseEstimator):
7
+ def __init__(self):
8
+ self.weights = []
9
+ self.bias = 0
10
+ self.classes_ = np.array([0, 1])
11
+ #self.scaler = MinMaxScaler(feature_range=(0, 1))
12
+ self.label_encoder = LabelEncoder()
13
+ self.max = 0
14
+ self.min = 0
15
+
16
+ def fit(self, train_predictors, train_target, sample_weight=None):
17
+ """
18
+ This is used for training the classifier on data.
19
+ Parameters
20
+ ----------
21
+ train_predictors : float, either list or numpy array
22
+ are the main data in DataFrame
23
+ train_target : integer, numpy array
24
+ labels, should consist of 0s and 1s
25
+ """
26
+ #train_predictors = self.scaler.fit_transform(train_predictors)
27
+
28
+ #self.label_encoder.fit(train_target)
29
+ #encoded_labels = self.label_encoder.transform(train_target)
30
+ #print(train_predictors)
31
+
32
+ X = np.array(train_predictors, dtype="float32")
33
+ #y = np.array(encoded_labels, dtype="int32")
34
+ y = np.array(train_target, dtype="int32")
35
+
36
+ if sample_weight is not None:
37
+ sample_weight = np.array(sample_weight, dtype="float32")
38
+ else:
39
+ sample_weight = np.ones(len(y), dtype="float32")
40
+
41
+ pos_labels = np.sign(y) == 1
42
+ neg_labels = np.invert(pos_labels)
43
+
44
+ pos_indices = X[pos_labels, :]
45
+ neg_indices = X[neg_labels, :]
46
+
47
+
48
+
49
+ avg_pos = np.average(pos_indices, axis=0, weights=sample_weight[pos_labels])
50
+ avg_neg = np.average(neg_indices, axis=0, weights=sample_weight[neg_labels])
51
+
52
+ self.weights = (avg_pos - avg_neg) / (avg_pos + avg_neg + 0.0000001)
53
+
54
+ sum_scores = np.dot(X, self.weights)
55
+
56
+ pos_label_count = np.count_nonzero(y)
57
+ neg_label_count = y.shape[0] - pos_label_count
58
+
59
+ pos_score_avg = np.average(sum_scores[y == 1], weights=sample_weight[y == 1])
60
+ neg_score_avg = np.average(sum_scores[y == 0], weights=sample_weight[y == 0])
61
+
62
+ self.bias = (neg_label_count * pos_score_avg + pos_label_count * neg_score_avg) / (neg_label_count + pos_label_count)
63
+
64
+ self.max = max(sum_scores)
65
+ self.min = min(sum_scores)
66
+
67
+
68
+
69
+ #print(sample_weight)
70
+ #print(self.weights, self.bias)
71
+
72
+
73
+
74
+ def predict(self, test_predictors):
75
+ """
76
+ This is for prediction. When the model is trained, it can be applied on the test data.
77
+ Parameters
78
+ ----------
79
+ test_predictors: either list or ndarray, two dimensional
80
+ the data without labels in
81
+ Returns
82
+ ----------
83
+ predictions in numpy array
84
+ """
85
+ #X = self.scaler.transform(test_predictors)
86
+ X = test_predictors
87
+ if isinstance(test_predictors, list):
88
+ X = np.array(test_predictors, dtype="float32")
89
+
90
+ temp = np.dot(X, self.weights)
91
+ preds = np.where(temp <= self.bias, 0 , 1)
92
+ #original_preds = self.label_encoder.inverse_transform(preds)
93
+ #return original_preds
94
+ return preds
95
+
96
+ def predict_proba(self, test_predictors):
97
+ """
98
+ This is for prediction probabilities.
99
+ Parameters
100
+ ----------
101
+ test_predictors: either list or ndarray, two dimensional
102
+ the data without labels in
103
+ Returns
104
+ ----------
105
+ prediction probabilities in numpy array
106
+ """
107
+ X = test_predictors
108
+ if isinstance(test_predictors, list):
109
+ X = np.array(test_predictors, dtype="float32")
110
+
111
+ linear_output = np.dot(X, self.weights) - self.bias
112
+
113
+ #print('llllll', linear_output)
114
+ #exit()
115
+ # =============================================================================
116
+ # pred_proba = 1 / (1 + np.exp(-linear_output))
117
+ # pred_proba = np.exp(linear_output) / (np.exp(linear_output) + np.exp(-linear_output))
118
+ # #print(pred_proba)
119
+ # return np.column_stack((1 - pred_proba, pred_proba))
120
+ # =============================================================================
121
+
122
+
123
+ temp = np.dot(X, self.weights)
124
+ score = (temp - self.bias) / np.linalg.norm(self.weights)
125
+ #score = (temp - self.bias) / self.max
126
+ pred_proba = 1 / (1 + np.exp(-score))
127
+ #pred_proba = (np.tanh(score) + 1) / 2
128
+ #print(pred_proba)
129
+ return np.column_stack((1 - pred_proba, pred_proba))
130
+
131
+
132
+
133
+
134
+ # to be tested later
135
+ #score = ((temp - self.bias) - self.min) / (self.max - self.min)
136
+ #pred_proba = score
137
+ #print(pred_proba)
138
+ #return np.column_stack((1 - pred_proba, pred_proba))
139
+
140
+
141
+ class LinearBoostClassifier(AdaBoostClassifier):
142
+ def __init__(self, n_estimators=200, learning_rate=1.0, algorithm='SAMME', random_state=9):
143
+ self.scaler = MinMaxScaler(feature_range=(0, 1))
144
+ self.label_encoder = LabelEncoder()
145
+ super().__init__(estimator=SEFR(), n_estimators=n_estimators, learning_rate=learning_rate, algorithm=algorithm, random_state=random_state)
146
+
147
+
148
+ def fit(self, X, y, sample_weight=None):
149
+ self.scaler.fit(X)
150
+ X = self.scaler.transform(X)
151
+ self.label_encoder.fit(y)
152
+ y = self.label_encoder.transform(y)
153
+ return super().fit(X, y, sample_weight)
154
+
155
+ def predict(self, X):
156
+ X = self.scaler.transform(X)
157
+ y_pred = super().predict(X)
158
+ return self.label_encoder.inverse_transform(y_pred)
159
+
160
+ def predict_proba(self, X):
161
+ X = self.scaler.transform(X)
162
+ return super().predict_proba(X)
163
+
164
+
165
+ def linboostclassifier(n_estimators=200, random_state=0, algorithm="SAMME"):
166
+ return AdaBoostClassifier(estimator=SEFR(), n_estimators=n_estimators, random_state=random_state, algorithm=algorithm)
@@ -0,0 +1 @@
1
+ from .LinearBoost import LinearBoostClassifier
@@ -0,0 +1,20 @@
1
+ Metadata-Version: 2.1
2
+ Name: linearboost
3
+ Version: 0.0.1
4
+ Summary: LinearBoost Python Package
5
+ Author-email: Hamidreza Keshavarz <hamid9@outlook.com>, Reza Rawassizadeh <rezar@bu.edu>
6
+ License: MIT License
7
+ Project-URL: Homepage, https://github.com/LinearBoost/linearboost-classifier
8
+ Project-URL: Documentation, https://linearboost.readthedocs.io
9
+ Project-URL: Issues, https://github.com/LinearBoost/linearboost-classifier/issues
10
+ Keywords: classification,classifier,linear,adaboost,boosting,boost
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Operating System :: OS Independent
14
+ Requires-Python: >=3.8
15
+ Description-Content-Type: text/markdown
16
+ License-File: LICENSE
17
+ Requires-Dist: numpy>=1.24.3
18
+ Requires-Dist: scikit-learn>=1.2.2
19
+
20
+ LinearBoost is a classification algorithm based on boosting a linear classifier named SEFR. It can be used for classification tasks.
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/linearboost/LinearBoost.py
5
+ src/linearboost/__init__.py
6
+ src/linearboost.egg-info/PKG-INFO
7
+ src/linearboost.egg-info/SOURCES.txt
8
+ src/linearboost.egg-info/dependency_links.txt
9
+ src/linearboost.egg-info/requires.txt
10
+ src/linearboost.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ numpy>=1.24.3
2
+ scikit-learn>=1.2.2
@@ -0,0 +1,2 @@
1
+ linearboost
2
+ tests