mirror of
https://github.com/titanscouting/tra-analysis.git
synced 2024-11-09 22:44:44 +00:00
added dbscan and spectral to Clustering.py
This commit is contained in:
parent
4923881829
commit
3e99869d5d
@ -5,6 +5,7 @@ from sklearn import metrics
|
||||
from tra_analysis import Analysis as an
|
||||
from tra_analysis import Array
|
||||
from tra_analysis import ClassificationMetric
|
||||
from tra_analysis import Clustering
|
||||
from tra_analysis import CorrelationTest
|
||||
from tra_analysis import Fit
|
||||
from tra_analysis import KNN
|
||||
@ -231,3 +232,13 @@ def test_equation():
|
||||
}
|
||||
for key in list(correctParse.keys()):
|
||||
assert parser.eval(key) == correctParse[key]
|
||||
|
||||
def test_clustering():
|
||||
|
||||
data = X = np.array([[1, 2], [2, 2], [2, 3], [8, 7], [8, 8], [25, 80]])
|
||||
|
||||
assert Clustering.dbscan(data, eps=3, min_samples=2).tolist() == [0, 0, 0, 1, 1, -1]
|
||||
|
||||
data = np.array([[1, 1], [2, 1], [1, 0], [4, 7], [3, 5], [3, 6]])
|
||||
|
||||
assert Clustering.spectral(data, n_clusters=2, assign_labels='discretize', random_state=0).tolist() == [1, 1, 1, 0, 0, 0]
|
@ -4,10 +4,13 @@
|
||||
# this should be imported as a python module using 'from tra_analysis import Clustering'
|
||||
# setup:
|
||||
|
||||
__version__ = "1.0.0"
|
||||
__version__ = "2.0.0"
|
||||
|
||||
# changelog should be viewed using print(analysis.__changelog__)
|
||||
__changelog__ = """changelog:
|
||||
2.0.0:
|
||||
- added dbscan clustering algo
|
||||
- added spectral clustering algo
|
||||
1.0.0:
|
||||
- created this submodule
|
||||
- copied kmeans clustering from Analysis
|
||||
@ -18,8 +21,13 @@ __author__ = (
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"kmeans",
|
||||
"dbscan",
|
||||
"spectral",
|
||||
]
|
||||
|
||||
import sklearn
|
||||
|
||||
def kmeans(data, n_clusters=8, init="k-means++", n_init=10, max_iter=300, tol=0.0001, precompute_distances="auto", verbose=0, random_state=None, copy_x=True, n_jobs=None, algorithm="auto"):
|
||||
|
||||
kernel = sklearn.cluster.KMeans(n_clusters = n_clusters, init = init, n_init = n_init, max_iter = max_iter, tol = tol, precompute_distances = precompute_distances, verbose = verbose, random_state = random_state, copy_x = copy_x, n_jobs = n_jobs, algorithm = algorithm)
|
||||
@ -28,3 +36,15 @@ def kmeans(data, n_clusters=8, init="k-means++", n_init=10, max_iter=300, tol=0.
|
||||
centers = kernel.cluster_centers_
|
||||
|
||||
return centers, predictions
|
||||
|
||||
def dbscan(data, eps=0.5, min_samples=5, metric='euclidean', metric_params=None, algorithm='auto', leaf_size=30, p=None, n_jobs=None):
|
||||
|
||||
model = sklearn.cluster.DBSCAN(eps = eps, min_samples = min_samples, metric = metric, metric_params = metric_params, algorithm = algorithm, leaf_size = leaf_size, p = p, n_jobs = n_jobs).fit(data)
|
||||
|
||||
return model.labels_
|
||||
|
||||
def spectral(data, n_clusters=8, eigen_solver=None, n_components=None, random_state=None, n_init=10, gamma=1.0, affinity='rbf', n_neighbors=10, eigen_tol=0.0, assign_labels='kmeans', degree=3, coef0=1, kernel_params=None, n_jobs=None, verbose=False):
|
||||
|
||||
model = sklearn.cluster.SpectralClustering(n_clusters = n_clusters, eigen_solver = eigen_solver, n_components = n_components, random_state = random_state, n_init = n_init, gamma = gamma, affinity = affinity, n_neighbors = n_neighbors, eigen_tol = eigen_tol, assign_labels = assign_labels, degree = degree, coef0 = coef0, kernel_params = kernel_params, n_jobs = n_jobs).fit(data)
|
||||
|
||||
return model.labels_
|
@ -59,6 +59,7 @@ __all__ = [
|
||||
from . import Analysis as Analysis
|
||||
from .Array import Array
|
||||
from .ClassificationMetric import ClassificationMetric
|
||||
from . import Clustering
|
||||
from . import CorrelationTest
|
||||
from .equation import Expression
|
||||
from . import Fit
|
||||
|
Loading…
Reference in New Issue
Block a user