
Security News
Another Round of TEA Protocol Spam Floods npm, But It’s Not a Worm
Recent coverage mislabels the latest TEA protocol spam as a worm. Here’s what’s actually happening.
gapstatistics
Advanced tools
The GapStatistics package provides a Python implementation of the Gap Statistics method for determining the optimal number of clusters in a dataset using K-means clustering derived from Tibshirani et al.. The package is designed to choose the distance metrics agnostically as well as the clustering algorithm. However, the primary usage is the KMeans algorithm.
To install the GapStatistics package, you can use pip:
pip install gapstatistics
This is the basic use case. If you don't define the parameter algorithm parameter, the default clustering technique is KMeans. The returned object optimum is an integer showing the optimal number of clusters for the data.
from gapstatistics import GapStatistics
centers = [[0,0], [0,6], [3,2], [5,0]]
X = make_blobs(n_samples=200, centers=centers, n_features=2, cluster_std=1)
n_iterations = 30
gs = GapStatistics(distance_metric='minkowski')
optimum = Gs.fit_predict(K=10, X=X[0])
print(f'Optimum: {optimum}')
Here is some code that you can use for showing different plots how the gap statistics derives the optimal number of clusters. For this, you must set the return_params to True, so that you can plot them.
from gapstatistics import GapStatistics
from sklearn.datasets import make_blobs
centers = [[0,0], [0,6], [3,2], [5,0]]
X = make_blobs(n_samples=200, centers=centers, n_features=2, cluster_std=1)
n_iterations = 30
gs = GapStatistics(distance_metric='minkowski', return_params=True)
optimum, params = gs.fit_predict(K=10, X=X[0])
gs.plot()
Here is some code that you can use for creating a custom distance metric to provide to the class. The distance metric must have two parameters:
def euclidian_distance(X: np.array, Centroid: np.array) -> np.array:
return np.linalg.norm(X - Centroid, axis=1)
def manhattan_distance(X: np.array, Centroid: np.array) -> np.array:
return np.sum(np.abs(X - Centroid), axis=1)
GapStatistics(distance_metric=manhattan_distance)
__init__(self, algorithm, distance_metric, pca_sampling, return_params)algorithm (Callable): The clustering algorithm to use (default: KMeans). It should be a callable that creates a clustering model. If you want to use your own clustering algorithm, you must provide a callable object that has the following attributes / functions:
distance_metric (str or callable): The distance metric used for clustering. If a string, it should be a valid metric name recognized by sklearn.metrics.DistanceMetric. If a callable, it should accept two arrays and return the distance between them. Examples for strings are:
pca_sampling (bool): Whether to apply Principal Component Analysis (PCA) during bootstrapping (default: True).return_params (bool): Whether to return additional statistics in the fit_predict function (default: False).fit_predict(self, K, X, n_iterations)Perform gap statistics to find the optimal number of clusters (K) for a given dataset.
K (int): The maximum number of clusters (K) to consider for finding the optimal K value.X (list): A list of data points (samples) to be used for clustering. Must have a 2D shape -> (?, 2)n_iterations (int): The number of iterations to perform for simulating Wk's statistics.Returns either the optimal number of clusters (K) or a tuple with the optimal K and additional statistics used in gap analysis.
plot(self, original_labels, colors)Visualize the output of the gap statistics based on the returned parameters.
original_labels (list): The list of the original groundtruth labels to compare against (if accessible).colors (dict): If the optimal value is greater than 10, you must provide an additional color dictionary.Returns a plot consisting of four subplots for showing why the gap statistics decided the optimal number of clusters.
FAQs
A library for calculating the gap statistics for clustering algorithms
We found that gapstatistics demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Recent coverage mislabels the latest TEA protocol spam as a worm. Here’s what’s actually happening.

Security News
PyPI adds Trusted Publishing support for GitLab Self-Managed as adoption reaches 25% of uploads

Research
/Security News
A malicious Chrome extension posing as an Ethereum wallet steals seed phrases by encoding them into Sui transactions, enabling full wallet takeover.