Created
February 19, 2024 17:11
-
-
Save botcs/1fac7e03363cfbdaa1cdeb84e58ae2a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import torch | |
| import time | |
| import numpy as np | |
| from sklearn.datasets import make_blobs | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.metrics import accuracy_score | |
| from microcluster_module import MClassification | |
| def generate_data(n_samples=1000, n_features=2, centers=5): | |
| X, y = make_blobs(n_samples=n_samples, n_features=n_features, centers=centers, random_state=42) | |
| return X, y | |
| def main(): | |
| # Generate synthetic data | |
| X, y = generate_data() | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=42) | |
| # Convert to PyTorch tensors | |
| X_train_tensor = torch.tensor(X_train, dtype=torch.float32) | |
| y_train_tensor = torch.tensor(y_train, dtype=torch.int) | |
| X_test_tensor = torch.tensor(X_test, dtype=torch.float32) | |
| y_test_tensor = torch.tensor(y_test, dtype=torch.int) | |
| # Initialize and train model | |
| model = MClassification(max_radius=0.2, max_clusters=100) | |
| model.fit_initial(X_train_tensor, y_train_tensor) | |
| # Benchmarking on test data | |
| start_time = time.time() | |
| predictions = [model.predict_and_update(x) for x in X_test_tensor] | |
| end_time = time.time() | |
| # Calculate accuracy | |
| accuracy = accuracy_score(y_test_tensor.numpy(), predictions) | |
| # Report results | |
| execution_time = end_time - start_time | |
| print(f'Accuracy: {accuracy*100:.2f}%') | |
| print(f'Execution Time: {execution_time:.2f} seconds') | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment