prosemble 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.
- Prosemble-0.0.1/LICENSE +21 -0
- Prosemble-0.0.1/PKG-INFO +54 -0
- Prosemble-0.0.1/README.md +36 -0
- Prosemble-0.0.1/setup.cfg +4 -0
- Prosemble-0.0.1/setup.py +30 -0
- Prosemble-0.0.1/src/Prosemble.egg-info/PKG-INFO +54 -0
- Prosemble-0.0.1/src/Prosemble.egg-info/SOURCES.txt +9 -0
- Prosemble-0.0.1/src/Prosemble.egg-info/dependency_links.txt +1 -0
- Prosemble-0.0.1/src/Prosemble.egg-info/requires.txt +3 -0
- Prosemble-0.0.1/src/Prosemble.egg-info/top_level.txt +1 -0
- Prosemble-0.0.1/src/Prosemble.py +479 -0
Prosemble-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) [2022] [Nana Abeka Otoo]
|
|
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.
|
Prosemble-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: Prosemble
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Prototype based ensemble classifier
|
|
5
|
+
Home-page: https://github.com/naotoo1/Prosemble
|
|
6
|
+
Author: Nana Abeka Otoo
|
|
7
|
+
License: UNKNOWN
|
|
8
|
+
Keywords: ensemble,Prototype ensemble
|
|
9
|
+
Platform: UNKNOWN
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Requires-Python: >=3.9
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
|
|
17
|
+
## Description
|
|
18
|
+
[](https://www.python.org/downloads/release/python-390/)
|
|
19
|
+
[](https://github.com/naotoo1/Prosemble)
|
|
20
|
+
[](https://pypi.org/project/prosemble)
|
|
21
|
+
[](https://opensource.org/licenses/MIT)
|
|
22
|
+
|
|
23
|
+
A Prototype based classifier which utilizes learned prototypes from LVQs in ensemble learning by soft and hard voting
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
```python
|
|
27
|
+
pip install prosemble
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
## Source code
|
|
32
|
+
```python
|
|
33
|
+
git clone https://github.com/naotoo1/Prosemble.git
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
## Bibtex
|
|
38
|
+
If you would like to cite the package, please use this:
|
|
39
|
+
```python
|
|
40
|
+
@misc{Otoo_Prosemble_2022,
|
|
41
|
+
author = {Otoo, Nana Abeka},
|
|
42
|
+
title = {Prosemble},
|
|
43
|
+
year = {2022},
|
|
44
|
+
publisher = {GitHub},
|
|
45
|
+
journal = {GitHub repository},
|
|
46
|
+
howpublished= {\url{https://github.com/naotoo1/Prosemble}},
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
## Description
|
|
2
|
+
[](https://www.python.org/downloads/release/python-390/)
|
|
3
|
+
[](https://github.com/naotoo1/Prosemble)
|
|
4
|
+
[](https://pypi.org/project/prosemble)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
A Prototype based classifier which utilizes learned prototypes from LVQs in ensemble learning by soft and hard voting
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
```python
|
|
11
|
+
pip install prosemble
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## Source code
|
|
16
|
+
```python
|
|
17
|
+
git clone https://github.com/naotoo1/Prosemble.git
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
## Bibtex
|
|
22
|
+
If you would like to cite the package, please use this:
|
|
23
|
+
```python
|
|
24
|
+
@misc{Otoo_Prosemble_2022,
|
|
25
|
+
author = {Otoo, Nana Abeka},
|
|
26
|
+
title = {Prosemble},
|
|
27
|
+
year = {2022},
|
|
28
|
+
publisher = {GitHub},
|
|
29
|
+
journal = {GitHub repository},
|
|
30
|
+
howpublished= {\url{https://github.com/naotoo1/Prosemble}},
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
Prosemble-0.0.1/setup.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import setuptools
|
|
2
|
+
|
|
3
|
+
with open("README.md", "r") as fh:
|
|
4
|
+
long_description = fh.read()
|
|
5
|
+
|
|
6
|
+
setuptools.setup(
|
|
7
|
+
name="Prosemble",
|
|
8
|
+
version="0.0.1",
|
|
9
|
+
author="Nana Abeka Otoo",
|
|
10
|
+
description="Prototype based ensemble classifier",
|
|
11
|
+
url="https://github.com/naotoo1/Prosemble",
|
|
12
|
+
long_description=long_description,
|
|
13
|
+
long_description_content_type="text/markdown",
|
|
14
|
+
packages=setuptools.find_packages(),
|
|
15
|
+
keywords=['ensemble', 'Prototype ensemble'],
|
|
16
|
+
classifiers=[
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
],
|
|
21
|
+
python_requires='>=3.9',
|
|
22
|
+
py_modules=["Prosemble"],
|
|
23
|
+
package_dir={'': 'src'},
|
|
24
|
+
install_requires=[
|
|
25
|
+
'numpy',
|
|
26
|
+
'scikit-learn',
|
|
27
|
+
'scipy'
|
|
28
|
+
|
|
29
|
+
]
|
|
30
|
+
)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: Prosemble
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Prototype based ensemble classifier
|
|
5
|
+
Home-page: https://github.com/naotoo1/Prosemble
|
|
6
|
+
Author: Nana Abeka Otoo
|
|
7
|
+
License: UNKNOWN
|
|
8
|
+
Keywords: ensemble,Prototype ensemble
|
|
9
|
+
Platform: UNKNOWN
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Requires-Python: >=3.9
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
|
|
17
|
+
## Description
|
|
18
|
+
[](https://www.python.org/downloads/release/python-390/)
|
|
19
|
+
[](https://github.com/naotoo1/Prosemble)
|
|
20
|
+
[](https://pypi.org/project/prosemble)
|
|
21
|
+
[](https://opensource.org/licenses/MIT)
|
|
22
|
+
|
|
23
|
+
A Prototype based classifier which utilizes learned prototypes from LVQs in ensemble learning by soft and hard voting
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
```python
|
|
27
|
+
pip install prosemble
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
## Source code
|
|
32
|
+
```python
|
|
33
|
+
git clone https://github.com/naotoo1/Prosemble.git
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
## Bibtex
|
|
38
|
+
If you would like to cite the package, please use this:
|
|
39
|
+
```python
|
|
40
|
+
@misc{Otoo_Prosemble_2022,
|
|
41
|
+
author = {Otoo, Nana Abeka},
|
|
42
|
+
title = {Prosemble},
|
|
43
|
+
year = {2022},
|
|
44
|
+
publisher = {GitHub},
|
|
45
|
+
journal = {GitHub repository},
|
|
46
|
+
howpublished= {\url{https://github.com/naotoo1/Prosemble}},
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Prosemble
|
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from sklearn.metrics import accuracy_score
|
|
3
|
+
from operator import itemgetter
|
|
4
|
+
from scipy.spatial import distance
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class LabelSecurity:
|
|
8
|
+
"""
|
|
9
|
+
Label Security
|
|
10
|
+
:params
|
|
11
|
+
|
|
12
|
+
x_test: array, shape=[num_data,num_features]
|
|
13
|
+
Where num_data is the number of samples and num_features refers to the number of features.
|
|
14
|
+
class_labels: array-like, shape=[num_classes]
|
|
15
|
+
Class labels of prototypes
|
|
16
|
+
|
|
17
|
+
predict_results: array-like, shape=[num_data]
|
|
18
|
+
Predicted labels of the test-set
|
|
19
|
+
|
|
20
|
+
model_prototypes: array-like, shape=[num_prototypes, num_features]
|
|
21
|
+
Prototypes from the trained model using train-set, where num_prototypes refers to the number of prototypes
|
|
22
|
+
|
|
23
|
+
x_dat : array, shape=[num_data, num_features]
|
|
24
|
+
Input data
|
|
25
|
+
|
|
26
|
+
fuzziness_parameter: int, optional(default=2)
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
def __init__(self, x_test, class_labels, predict_results, model_prototypes, x_dat, fuzziness_parameter=2):
|
|
30
|
+
self.x_test = x_test
|
|
31
|
+
self.class_labels = class_labels
|
|
32
|
+
self.predict_results = predict_results
|
|
33
|
+
self.model_prototypes = model_prototypes
|
|
34
|
+
self.x_dat = x_dat
|
|
35
|
+
self.fuzziness_parameter = fuzziness_parameter
|
|
36
|
+
|
|
37
|
+
def label_sec_f(self, x):
|
|
38
|
+
"""
|
|
39
|
+
Computes the labels security of each prediction from the model using the test_set
|
|
40
|
+
|
|
41
|
+
:param x
|
|
42
|
+
predicted labels from the model using test-set
|
|
43
|
+
:return:
|
|
44
|
+
labels with their security
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
security = float
|
|
48
|
+
|
|
49
|
+
# Empty list to populate with certainty of labels
|
|
50
|
+
my_label_sec_list = []
|
|
51
|
+
|
|
52
|
+
# loop through the test_set
|
|
53
|
+
for i in range(self.x_test.shape[0]):
|
|
54
|
+
|
|
55
|
+
# consider respective class labels
|
|
56
|
+
for label in range(self.class_labels.shape[0]):
|
|
57
|
+
|
|
58
|
+
# checks where the predicted label equals the class label
|
|
59
|
+
if self.predict_results[i] == label:
|
|
60
|
+
|
|
61
|
+
# computes the certainty/security per predicted label
|
|
62
|
+
ed_dis: float = distance.euclidean(self.x_test[i, 0:self.x_dat.shape[1]],
|
|
63
|
+
self.model_prototypes[label, 0:self.x_dat.shape[1]])
|
|
64
|
+
sum_dis = 0
|
|
65
|
+
for j in range(self.model_prototypes.shape[0]):
|
|
66
|
+
sum_dis += np.power(ed_dis / distance.euclidean(self.x_test[i, 0:self.x_dat.shape[1]],
|
|
67
|
+
self.model_prototypes[j,
|
|
68
|
+
0:self.x_dat.shape[1]]),
|
|
69
|
+
2 / (self.fuzziness_parameter - 1))
|
|
70
|
+
security = 1 / sum_dis
|
|
71
|
+
|
|
72
|
+
my_label_sec_list.append(np.round(security, 4)) # add the computed label certainty to list above
|
|
73
|
+
my_label_sec_list = np.array(my_label_sec_list)
|
|
74
|
+
my_label_sec_list = my_label_sec_list.reshape(len(my_label_sec_list), 1) # reshape list to 1-D array
|
|
75
|
+
x = np.array(x)
|
|
76
|
+
x = x.reshape(len(x), 1) # reshape predicted labels into 1-D array
|
|
77
|
+
labels_with_certainty = np.concatenate((x, my_label_sec_list), axis=1)
|
|
78
|
+
return labels_with_certainty
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class LabelSecurityM:
|
|
82
|
+
"""
|
|
83
|
+
label security for matrix GLVQ
|
|
84
|
+
:parameters
|
|
85
|
+
|
|
86
|
+
x_test: array, shape=[num_data, num_features]
|
|
87
|
+
Where num_data refers to the number of samples and num_features refers to the number of features
|
|
88
|
+
|
|
89
|
+
class_labels: array-like, shape=[num_classes]
|
|
90
|
+
Class labels of prototypes
|
|
91
|
+
|
|
92
|
+
model_prototypes: array-like, shape=[num_prototypes, num_features]
|
|
93
|
+
Prototypes from the trained model using train-set, where num_prototypes refers to the number of prototypes
|
|
94
|
+
|
|
95
|
+
model_omega: array-like, shape=[dim, num_features]
|
|
96
|
+
Omega_matrix from the trained model, where dim is an int refers to the maximum rank
|
|
97
|
+
|
|
98
|
+
x: array, shape=[num_data, num_features]
|
|
99
|
+
Input data
|
|
100
|
+
|
|
101
|
+
fuzziness_parameter=int, optional(default=2)
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
def __init__(self, x_test, class_labels, model_prototypes, model_omega, x, fuzziness_parameter=2):
|
|
105
|
+
self.x_test = x_test
|
|
106
|
+
self.class_labels = class_labels
|
|
107
|
+
self.model_prototypes = model_prototypes
|
|
108
|
+
self.model_omega = model_omega
|
|
109
|
+
self.x = x
|
|
110
|
+
self.fuzziness_parameter = fuzziness_parameter
|
|
111
|
+
|
|
112
|
+
def label_security_m_f(self, x):
|
|
113
|
+
"""
|
|
114
|
+
Computes the label security of each prediction from the model using the test_set
|
|
115
|
+
:param x: predicted labels from the model using X_test
|
|
116
|
+
:return: labels with their securities
|
|
117
|
+
"""
|
|
118
|
+
security = " "
|
|
119
|
+
# Empty list to populate with the label certainty
|
|
120
|
+
my_label_security_list = []
|
|
121
|
+
# loop through the test set
|
|
122
|
+
for i in range(len(self.x_test)):
|
|
123
|
+
# considers respective class labels of prototypes
|
|
124
|
+
for label in range(len(self.class_labels)):
|
|
125
|
+
# checks if predicted label equals class label of prototypes
|
|
126
|
+
if x[i] == label:
|
|
127
|
+
|
|
128
|
+
# computes the label certainty per predicted label
|
|
129
|
+
standard_ed = (self.x_test[i, 0:self.x.shape[1]]
|
|
130
|
+
- self.model_prototypes[label, 0:self.x.shape[1]])
|
|
131
|
+
squared_ed = standard_ed.T.dot(self.model_omega.T).dot(self.model_omega).dot(standard_ed)
|
|
132
|
+
sum_dis = 0
|
|
133
|
+
for j in range(len(self.model_prototypes)):
|
|
134
|
+
standard_ed1 = (self.x_test[i, 0:self.x.shape[1]] - self.model_prototypes[j, 0:self.x.shape[1]])
|
|
135
|
+
sum_dis += np.power(
|
|
136
|
+
squared_ed / (
|
|
137
|
+
standard_ed1.T.dot(self.model_omega.T).dot(self.model_omega).dot(standard_ed1)),
|
|
138
|
+
1 / (self.fuzziness_parameter - 1))
|
|
139
|
+
security = 1 / sum_dis
|
|
140
|
+
|
|
141
|
+
# adds the computed certainty to the list
|
|
142
|
+
my_label_security_list.append(np.round(security, 4))
|
|
143
|
+
my_label_security_list = np.array(my_label_security_list)
|
|
144
|
+
my_label_security_list = my_label_security_list.reshape(len(my_label_security_list), 1) # 1-D array reshape
|
|
145
|
+
x = np.array(x)
|
|
146
|
+
x = x.reshape(len(x), 1) # reshape the predicted labels into 1-D array
|
|
147
|
+
labels_with_certainty = np.concatenate((x, my_label_security_list), axis=1)
|
|
148
|
+
return labels_with_certainty
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
class LabelSecurityLM:
|
|
152
|
+
"""
|
|
153
|
+
label security for local matrix GLVQ
|
|
154
|
+
:parameters
|
|
155
|
+
|
|
156
|
+
x_test: array, shape=[num_data, num_features]
|
|
157
|
+
Where num_data refers to the number of samples and num_features refers to the number of features
|
|
158
|
+
|
|
159
|
+
class_labels: array-like, shape=[num_classes]
|
|
160
|
+
Class labels of prototypes
|
|
161
|
+
|
|
162
|
+
model_prototypes: array-like, shape=[num_prototypes, num_features]
|
|
163
|
+
Prototypes from the trained model using train-set, where num_prototypes refers to the number
|
|
164
|
+
of prototypes
|
|
165
|
+
|
|
166
|
+
model_omega: array-like, shape=[dim, num_features]
|
|
167
|
+
Omega_matrix from the trained model, where dim is an int refers to the maximum rank
|
|
168
|
+
|
|
169
|
+
x: array, shape=[num_data, num_features]
|
|
170
|
+
Input data
|
|
171
|
+
|
|
172
|
+
fuzziness_parameter=int, optional(default=2)
|
|
173
|
+
"""
|
|
174
|
+
|
|
175
|
+
def __init__(self, x_test, class_labels, model_prototypes, model_omega, x, fuzziness_parameter=2):
|
|
176
|
+
self.x_test = x_test
|
|
177
|
+
self.class_labels = class_labels
|
|
178
|
+
self.model_prototypes = model_prototypes
|
|
179
|
+
self.model_omega = model_omega
|
|
180
|
+
self.x = x
|
|
181
|
+
self.fuzziness_parameter = fuzziness_parameter
|
|
182
|
+
|
|
183
|
+
def label_security_lm_f(self, x):
|
|
184
|
+
"""
|
|
185
|
+
computes the label security of each prediction from the model using the test_set
|
|
186
|
+
and returns only labels their corresponding security.
|
|
187
|
+
:param x: predicted labels from the model using X_test
|
|
188
|
+
:return: labels with security
|
|
189
|
+
"""
|
|
190
|
+
security = " "
|
|
191
|
+
# Empty list to populate with the label security
|
|
192
|
+
my_label_security_list = []
|
|
193
|
+
# loop through the test set
|
|
194
|
+
for i in range(len(self.x_test)):
|
|
195
|
+
# considers respective class labels of prototypes
|
|
196
|
+
for label in range(len(self.class_labels)):
|
|
197
|
+
# checks if predicted label equals class label of prototypes
|
|
198
|
+
if x[i] == label:
|
|
199
|
+
|
|
200
|
+
# computes the label certainty per predicted label
|
|
201
|
+
standard_ed = (self.x_test[i, 0:self.x.shape[1]]
|
|
202
|
+
- self.model_prototypes[label, 0:self.x.shape[1]])
|
|
203
|
+
squared_ed = standard_ed.T.dot(self.model_omega[label].T).dot(self.model_omega[label]) \
|
|
204
|
+
.dot(standard_ed)
|
|
205
|
+
sum_dis = 0
|
|
206
|
+
for j in range(len(self.model_prototypes)):
|
|
207
|
+
standard_ed1 = (
|
|
208
|
+
self.x_test[i, 0:self.x.shape[1]] - self.model_prototypes[j, 0:self.x.shape[1]])
|
|
209
|
+
sum_dis += np.power(
|
|
210
|
+
squared_ed / (
|
|
211
|
+
standard_ed1.T.dot(self.model_omega[j].T).dot(self.model_omega[j]).dot(standard_ed1)),
|
|
212
|
+
1 / (self.fuzziness_parameter - 1))
|
|
213
|
+
security = 1 / sum_dis
|
|
214
|
+
|
|
215
|
+
# adds the computed certainty to the list
|
|
216
|
+
my_label_security_list.append(np.round(security, 4))
|
|
217
|
+
my_label_security_list = np.array(my_label_security_list)
|
|
218
|
+
my_label_security_list = my_label_security_list.reshape(len(my_label_security_list), 1) # 1-D array reshape
|
|
219
|
+
x = np.array(x)
|
|
220
|
+
x = x.reshape(len(x), 1) # reshape the predicted labels into 1-D array
|
|
221
|
+
labels_with_certainty = np.concatenate((x, my_label_security_list), axis=1)
|
|
222
|
+
return labels_with_certainty
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
class Hybrid:
|
|
226
|
+
"""
|
|
227
|
+
A module prototype based ensemble learning based on LVQs
|
|
228
|
+
:param
|
|
229
|
+
new_prototypes: array-like, shape=[num_prototypes, ,num_features]
|
|
230
|
+
Prototypes from the trained model using train-set, where num_prototypes refers to the number of prototypes
|
|
231
|
+
|
|
232
|
+
proto_classes: array-like, shape=[num_prototypes, num_features]
|
|
233
|
+
Prototypes from the trained model using train-set, where num_prototypes refers to the number of prototypes
|
|
234
|
+
|
|
235
|
+
mm: array-like, shape=[num_classes]
|
|
236
|
+
Class labels of prototypes
|
|
237
|
+
|
|
238
|
+
omega_matrix: array-like, shape=[dim, num_features]
|
|
239
|
+
Omega_matrix from the trained model, where dim is an int refers to the maximum rank
|
|
240
|
+
|
|
241
|
+
matrix: "yes" for matrix GLVQ and "no" otherwise , string
|
|
242
|
+
|
|
243
|
+
"""
|
|
244
|
+
|
|
245
|
+
def __init__(self, new_prototypes, proto_classes, mm, omega_matrix, matrix):
|
|
246
|
+
self.new_prototypes = new_prototypes
|
|
247
|
+
self.proto_classes = proto_classes
|
|
248
|
+
self.mm = mm
|
|
249
|
+
self.omega_matrix = omega_matrix
|
|
250
|
+
self.matrix = matrix
|
|
251
|
+
|
|
252
|
+
def distance(self, x, v):
|
|
253
|
+
# computes the distances between each data point and the prototypes
|
|
254
|
+
empty = []
|
|
255
|
+
for i in range(len(x)):
|
|
256
|
+
for j in range(len(v)):
|
|
257
|
+
if self.matrix == "n":
|
|
258
|
+
d = (x[i] - v[j])
|
|
259
|
+
d = d.T.dot(d)
|
|
260
|
+
empty.append([i, j, d])
|
|
261
|
+
if self.matrix == "y":
|
|
262
|
+
d = (x[i] - v[j])
|
|
263
|
+
d = d.T.dot(self.omega_matrix.T).dot(self.omega_matrix).dot(d)
|
|
264
|
+
empty.append([i, j, d])
|
|
265
|
+
return empty
|
|
266
|
+
|
|
267
|
+
def reshape(self, x):
|
|
268
|
+
x = np.array(x)
|
|
269
|
+
x = x.reshape(len(x), len(self.proto_classes))
|
|
270
|
+
return x
|
|
271
|
+
|
|
272
|
+
def min_val(self, l, i): # returns a list of the index of data points with minimum distance from prototypes
|
|
273
|
+
empty = []
|
|
274
|
+
d = (min(enumerate(map(itemgetter(i), l)), key=itemgetter(1)))
|
|
275
|
+
empty.append(d[0])
|
|
276
|
+
return empty # d
|
|
277
|
+
|
|
278
|
+
def max_val_1(self, l, i): # returns a list of the index of data points with maximum distance from the prototypes
|
|
279
|
+
empty = []
|
|
280
|
+
d = (max(enumerate(map(itemgetter(i), l)), key=itemgetter(1)))
|
|
281
|
+
empty.append(d[0])
|
|
282
|
+
return empty # d
|
|
283
|
+
|
|
284
|
+
def predict(self, x_test):
|
|
285
|
+
"""
|
|
286
|
+
Predicts class membership index for each input data
|
|
287
|
+
:param x_test:
|
|
288
|
+
Test set from the data set or the data set to be classified
|
|
289
|
+
:return:
|
|
290
|
+
Predicted class labels.
|
|
291
|
+
"""
|
|
292
|
+
empty = []
|
|
293
|
+
n = 0
|
|
294
|
+
mm = self.mm
|
|
295
|
+
for i in range(len(x_test)):
|
|
296
|
+
d = self.distance(x_test, self.new_prototypes)[n:mm] # try_now[n:mm]
|
|
297
|
+
n = mm
|
|
298
|
+
mm += self.mm
|
|
299
|
+
dd = self.min_val(d, -1) # index of the minimum value
|
|
300
|
+
empty.append(dd)
|
|
301
|
+
dd = np.array(empty)
|
|
302
|
+
dd = dd.flatten()
|
|
303
|
+
return dd
|
|
304
|
+
|
|
305
|
+
def get_security(self, x, y):
|
|
306
|
+
predict = self.predict(x)
|
|
307
|
+
security = LabelSecurity(x_test=x, class_labels=self.proto_classes, predict_results=predict,
|
|
308
|
+
model_prototypes=self.new_prototypes, x_dat=x, fuzziness_parameter=y)
|
|
309
|
+
sec = security.label_sec_f(predict)
|
|
310
|
+
return sec
|
|
311
|
+
|
|
312
|
+
def get_security_m(self, x, y):
|
|
313
|
+
predict = self.predict(x)
|
|
314
|
+
security = LabelSecurityM(x_test=x, class_labels=self.proto_classes, model_prototypes=self.new_prototypes,
|
|
315
|
+
model_omega=self.omega_matrix, x=x, fuzziness_parameter=y)
|
|
316
|
+
sec = security.label_security_m_f(predict)
|
|
317
|
+
return sec
|
|
318
|
+
|
|
319
|
+
def accuracy(self, x, y):
|
|
320
|
+
"""
|
|
321
|
+
Computes the accuracy of the predicted classifications
|
|
322
|
+
:param x: True labels
|
|
323
|
+
y-test
|
|
324
|
+
:param y: classification results using the max votes scheme
|
|
325
|
+
y-pred
|
|
326
|
+
:return: int
|
|
327
|
+
accuracy score
|
|
328
|
+
"""
|
|
329
|
+
d = accuracy_score(x, y)
|
|
330
|
+
return d
|
|
331
|
+
|
|
332
|
+
def spredict_final(self, x, y):
|
|
333
|
+
"""
|
|
334
|
+
computes the soft votes and probabilities of each predicted label for all the models
|
|
335
|
+
:param x: set
|
|
336
|
+
:param y: lists of list containing securities from the models
|
|
337
|
+
:return:
|
|
338
|
+
List containing the label,votes and probabilities of the label
|
|
339
|
+
"""
|
|
340
|
+
empty = []
|
|
341
|
+
for j in range(len(x)):
|
|
342
|
+
for label in self.proto_classes:
|
|
343
|
+
c = 0
|
|
344
|
+
ts_ = 0
|
|
345
|
+
for k in y:
|
|
346
|
+
if k[j][0] == label:
|
|
347
|
+
ts_ += k[j][1]
|
|
348
|
+
c += 1
|
|
349
|
+
try:
|
|
350
|
+
r = ts_ / c
|
|
351
|
+
except ZeroDivisionError:
|
|
352
|
+
r = 0
|
|
353
|
+
empty.append([label, c, r])
|
|
354
|
+
return empty
|
|
355
|
+
|
|
356
|
+
def predict_final(self, x, y):
|
|
357
|
+
"""
|
|
358
|
+
computes the hard votes and probabilities of each predicted label for all the models
|
|
359
|
+
:param x: set
|
|
360
|
+
:param y: lists of list containing predictions from the models
|
|
361
|
+
|
|
362
|
+
:return:
|
|
363
|
+
List containing the label, votes and probabilities of the label
|
|
364
|
+
"""
|
|
365
|
+
empty = []
|
|
366
|
+
z = len(y)
|
|
367
|
+
for j in range(len(x)):
|
|
368
|
+
for label in self.proto_classes:
|
|
369
|
+
c = 0
|
|
370
|
+
for i in y:
|
|
371
|
+
if i[j] == label:
|
|
372
|
+
c += 1
|
|
373
|
+
r = c / z
|
|
374
|
+
empty.append([label, c, r])
|
|
375
|
+
return empty
|
|
376
|
+
|
|
377
|
+
def predict_sprob(self, x, y):
|
|
378
|
+
"""
|
|
379
|
+
|
|
380
|
+
:param x: Test-set
|
|
381
|
+
:param y: list containing all the securities from the model
|
|
382
|
+
:return: List of the probabilities of the predicted labels
|
|
383
|
+
"""
|
|
384
|
+
|
|
385
|
+
empty = []
|
|
386
|
+
p = len(self.proto_classes)
|
|
387
|
+
t = self.spredict_final(x, y)
|
|
388
|
+
for i in t:
|
|
389
|
+
z = i[2]
|
|
390
|
+
empty.append(z)
|
|
391
|
+
new = np.array(empty)
|
|
392
|
+
new = new.reshape(len(x), p)
|
|
393
|
+
return new
|
|
394
|
+
|
|
395
|
+
def predict_prob(self, x, y):
|
|
396
|
+
"""
|
|
397
|
+
|
|
398
|
+
:param x: Test-set
|
|
399
|
+
:param y: list containing all the predictions from the model
|
|
400
|
+
:return: List of the probabilities of the predicted labels
|
|
401
|
+
"""
|
|
402
|
+
|
|
403
|
+
empty = []
|
|
404
|
+
p = len(self.proto_classes)
|
|
405
|
+
t = self.predict_final(x, y)
|
|
406
|
+
for i in t:
|
|
407
|
+
z = i[2]
|
|
408
|
+
empty.append(z)
|
|
409
|
+
new = np.array(empty)
|
|
410
|
+
new = new.reshape(len(x), p)
|
|
411
|
+
return new
|
|
412
|
+
|
|
413
|
+
def pred_sprob(self, x, y): # pred ensemble
|
|
414
|
+
"""
|
|
415
|
+
:param x: Test set
|
|
416
|
+
:param y: list containing all the securities from the model
|
|
417
|
+
:return: The classification labels with highest soft votes
|
|
418
|
+
"""
|
|
419
|
+
empty = []
|
|
420
|
+
t = self.predict_sprob(x, y)
|
|
421
|
+
for i in t:
|
|
422
|
+
d = max(enumerate(i), key=itemgetter(1))[0]
|
|
423
|
+
empty.append(d)
|
|
424
|
+
dd = np.array(empty)
|
|
425
|
+
dd = dd.flatten()
|
|
426
|
+
return dd
|
|
427
|
+
|
|
428
|
+
def pred_prob(self, x, y): # pred ensemble
|
|
429
|
+
"""
|
|
430
|
+
:param x: Test set
|
|
431
|
+
:param y: list containing all the predictions from the model
|
|
432
|
+
:return: The classification labels with maximum votes
|
|
433
|
+
"""
|
|
434
|
+
empty = []
|
|
435
|
+
t = self.predict_prob(x, y)
|
|
436
|
+
for i in t:
|
|
437
|
+
d = max(enumerate(i), key=itemgetter(1))[0]
|
|
438
|
+
empty.append(d)
|
|
439
|
+
dd = np.array(empty)
|
|
440
|
+
dd = dd.flatten()
|
|
441
|
+
return dd
|
|
442
|
+
|
|
443
|
+
def sprob(self, x, y):
|
|
444
|
+
"""
|
|
445
|
+
|
|
446
|
+
:param x: Test set
|
|
447
|
+
:param y: list containing all the securities from the model
|
|
448
|
+
:return: returns the probabilities of the classification based on soft voting
|
|
449
|
+
"""
|
|
450
|
+
empty = []
|
|
451
|
+
t = self.predict_sprob(x, y)
|
|
452
|
+
for i in t:
|
|
453
|
+
d1 = max(enumerate(i), key=itemgetter(1))[1]
|
|
454
|
+
d1 = np.round(d1, 4)
|
|
455
|
+
empty.append(d1)
|
|
456
|
+
dd = np.array(empty)
|
|
457
|
+
dd = dd.flatten()
|
|
458
|
+
return dd
|
|
459
|
+
|
|
460
|
+
def prob(self, x, y):
|
|
461
|
+
"""
|
|
462
|
+
|
|
463
|
+
:param x: Test set
|
|
464
|
+
:param y: list containing all the predictions from the model
|
|
465
|
+
:return: returns the probabilities of the classification based on hard voting
|
|
466
|
+
"""
|
|
467
|
+
empty = []
|
|
468
|
+
t = self.predict_prob(x, y)
|
|
469
|
+
for i in t:
|
|
470
|
+
d1 = max(enumerate(i), key=itemgetter(1))[1]
|
|
471
|
+
d1 = np.round(d1, 4)
|
|
472
|
+
empty.append(d1)
|
|
473
|
+
dd = np.array(empty)
|
|
474
|
+
dd = dd.flatten()
|
|
475
|
+
return dd
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
if __name__ == '__main__':
|
|
479
|
+
print('import module to use')
|