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

test Gaussian ART #39

Merged
merged 1 commit into from
Mar 12, 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
8 changes: 5 additions & 3 deletions elementary/GaussianART.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ def category_choice(self, i: np.ndarray, w: np.ndarray, params: dict) -> tuple[f
n = w[-1]
sig = np.diag(np.multiply(sigma,sigma))
dist = mean-i
exp_dist_sig_dist = np.exp(-0.5*np.matmul(dist.T, np.matmul((1/sig), dist)))
exp_dist_sig_dist = np.exp(-0.5*np.matmul(dist.T, np.matmul(np.linalg.inv(sig), dist)))
cache = {
"exp_dist_sig_dist": exp_dist_sig_dist
}
p_i_cj = exp_dist_sig_dist/np.sqrt((self.pi2**self.dim_)*np.linalg.det(sig))
p_cj = n/np.sum(w_[-1] for w_ in self.W)

return p_i_cj*p_cj, cache
activation = p_i_cj*p_cj

return activation, cache


def match_criterion(self, i: np.ndarray, w: np.ndarray, params: dict, cache: Optional[dict] = None) -> float:
Expand Down Expand Up @@ -60,4 +62,4 @@ def update(self, i: np.ndarray, w: np.ndarray, params, cache: Optional[dict] = N


def new_weight(self, i: np.ndarray, params: dict) -> np.ndarray:
return np.concatenate[i, params["sigma_init"], [1]]
return np.concatenate([i, params["sigma_init"], [1.]])
39 changes: 39 additions & 0 deletions examples/test_gaussian_art.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
import path
import sys

# directory reach
directory = path.Path(__file__).abspath()

print(directory.parent)
# setting path
sys.path.append(directory.parent.parent)

from elementary.GaussianART import GaussianART
from common.utils import normalize
import numpy as np


def cluster_blobs():
data, target = make_blobs(n_samples=150, centers=3, cluster_std=0.50, random_state=0, shuffle=False)
print("Data has shape:", data.shape)

X = normalize(data)
print("Prepared data has shape:", X.shape)

params = {
"rho": 0.15,
"sigma_init": np.array([0.5, 0.5]),
}
cls = GaussianART(params)
y = cls.fit_predict(X)

print(f"{cls.n_clusters} clusters found")

cls.visualize(X, y)
plt.show()


if __name__ == "__main__":
cluster_blobs()