Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dtype for labels #4

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions elementary/BaseART.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def partial_fit(self, X: np.ndarray, match_reset_func: Optional[Callable] = None

if not hasattr(self, 'W'):
self.W: list[np.ndarray] = []
self.labels_ = np.zeros((X.shape[0], ))
self.labels_ = np.zeros((X.shape[0], ), dtype=int)
j = 0
else:
j = len(self.labels_)
Expand All @@ -122,7 +122,7 @@ def predict(self, X: np.ndarray):
self.validate_data(X)
self.check_dimensions(X)

y = np.zeros((X.shape[0],))
y = np.zeros((X.shape[0],), dtype=int)
for i, x in enumerate(X):
c = self.step_pred(x)
y[i] = c
Expand Down
2 changes: 1 addition & 1 deletion examples/test_smart.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def cluster_blobs():
if j == 0:
layer_colors.append(colors[k])
else:
layer_colors.append(colors[cls.layers[j-1].map_a2b(k)])
layer_colors.append(colors[cls.map_deep(j-1, k)])
cls.modules[j].plot_bounding_boxes(ax, layer_colors)

plt.show()
Expand Down
14 changes: 13 additions & 1 deletion hierarchical/DeepARTMAP.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def __init__(self, modules: list[BaseART]):
def labels_(self):
return self.layers[0].labels_

@property
def labels_deep_(self):
return np.concatenate([layer.labels_ for layer in self.layers]+[self.layers[-1].labels_a])

@property
def n_modules(self):
return len(self.modules)
Expand All @@ -24,6 +28,14 @@ def n_modules(self):
def n_layers(self):
return len(self.layers)

def map_deep(self, level: int, y_a: Union[np.ndarray, int]) -> Union[np.ndarray, int]:
y_b = self.layers[level].map_a2b(y_a)
if level > 0:
return self.map_deep(level-1, y_b)
else:
return y_b


def validate_data(
self,
X: list[np.ndarray],
Expand Down Expand Up @@ -53,7 +65,7 @@ def fit(self, X: list[np.ndarray], y: Optional[np.ndarray] = None, max_iter=1):
self.layers[0] = self.layers[0].fit(X[1], X[0], max_iter=max_iter)

for art_i in range(1, self.n_layers):
y_i = self.layers[art_i-1].labels_
y_i = self.layers[art_i-1].labels_a
self.layers[art_i] = self.layers[art_i].fit(X[art_i], y_i, max_iter=max_iter)

return self
Expand Down
10 changes: 5 additions & 5 deletions supervised/ARTMAP.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def map_a2b(self, y_a: Union[np.ndarray, int]) -> np.ndarray:
if isinstance(y_a, int):
return self.map[y_a]
u, inv = np.unique(y_a, return_inverse=True)
return np.array([self.map[x] for x in u])[inv].reshape(y_a.shape)
return np.array([self.map[x] for x in u], dtype=int)[inv].reshape(y_a.shape)

def validate_data(self, X: np.ndarray, y: np.ndarray):
raise NotImplementedError
Expand Down Expand Up @@ -73,7 +73,7 @@ def fit(self, X: np.ndarray, y: np.ndarray, max_iter=1):
self.labels_ = y
# init module A
self.module_a.W = []
self.module_a.labels_ = np.zeros((X.shape[0],))
self.module_a.labels_ = np.zeros((X.shape[0],), dtype=int)

for _ in range(max_iter):
for i, (x, c_b) in enumerate(zip(X, y)):
Expand All @@ -86,7 +86,7 @@ def partial_fit(self, X: np.ndarray, y: np.ndarray):
if not hasattr(self, 'labels_'):
self.labels_ = y
self.module_a.W = []
self.module_a.labels_ = np.zeros((X.shape[0],))
self.module_a.labels_ = np.zeros((X.shape[0],), dtype=int)
j = 0
else:
j = len(self.labels_)
Expand Down Expand Up @@ -119,8 +119,8 @@ def step_pred(self, x: np.ndarray) -> tuple[int, int]:

def predict(self, X: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
check_is_fitted(self)
y_a = np.zeros((X.shape[0],))
y_b = np.zeros((X.shape[0],))
y_a = np.zeros((X.shape[0],), dtype=int)
y_b = np.zeros((X.shape[0],), dtype=int)
for i, x in enumerate(X):
c_a, c_b = self.step_pred(x)
y_a[i] = c_a
Expand Down
2 changes: 1 addition & 1 deletion topological/TopoART.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def fit(self, X: np.ndarray, match_reset_func: Optional[Callable] = None, max_it
self.check_dimensions(X)

self.W: list[np.ndarray] = []
self.labels_ = np.zeros((X.shape[0], ))
self.labels_ = np.zeros((X.shape[0], ), dtype=int)
for _ in range(max_iter):
for i, x in enumerate(X):
self.step_prune(X)
Expand Down