Python package for clustering
Project description
myclustering package
Description and Features
The MyClustering package is a Python library that provides implementations of various clustering algorithms, including K-means. It also includes utilities for visualizing clustering results and performing dimensionality reduction using PCA. The package aims to simplify the process of clustering and provide tools for analyzing and interpreting clustering results.
Key features of the myclustering package include:
- K-means clustering algorithm
- Silhouette score calculation and elbow method visualization
- PCA for dimensionality reduction
- Visualization of clustering results using scatter plots
Installation
To install the myclustering package, you can use pip:
pip install myclustering
Usage Examples
Here are some examples of how to use the myclustering package:
K-means Clustering
import numpy as np
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
from myclustering.kmeans.kmeans import KMeans
X, y = make_blobs(centers=3, n_samples=500, n_features=2, shuffle=True, random_state=40)
print(X.shape)
clusters = len(np.unique(y))
print(clusters)
k = KMeans(K=clusters, max_iters=150, plot_steps=False)
y_pred = k.fit(X)
k.plot()
You can identify the best number of cluster for K-means by looking at the silhouette score and elbow method visualization.
from myclustering.kmeans.silhouette import silhouette_score
from myclustering.kmeans.elbow import elbow_method
silhouette_score(X,k)
elbow_method(X)
PCA for dimensionality reduction
from sklearn import datasets
import matplotlib.pyplot as plt
import numpy as np
from myclustering.pca.pca import PCA
data = datasets.load_iris()
X = data.data
y = data.target
# Project the data onto the 2 primary principal components
pca = PCA(2)
pca.fit(X)
X_projected = pca.transform(X)
print('Shape of X:', X.shape)
print('Shape of transformed X:', X_projected.shape)
x1 = X_projected[:, 0]
x2 = X_projected[:, 1]
plt.scatter(x1, x2,
c=y, edgecolor='none', alpha=0.8,
cmap=plt.cm.get_cmap('viridis', 3))
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.colorbar()
plt.show()
DBSCAN visualization with the help of PCA
You can also visalize the results of your DBSCAN algorithm and identify the outliers in you data.
from myclustering.dbscan.visualization import plot_dbscan_pca
plot_dbscan_pca(X, epsilon = 0.3, min_points = 5)
Customer segmentation
One of the common applications of clustering is customer segmentation, where customers are grouped into distinct segments based on their behavior, preferences, or characteristics. The MyClustering package can be used for customer segmentation tasks.
Here's an example of how the myclustering package can be used for customer segmentation:
import pandas as pd
from myclustering.kmeans.kmeans import KMeans
from myclustering.pca.pca import PCA
# Load customer data
data = pd.read_csv('customer_data.csv')
# Preprocess the data (e.g., remove missing values, scale features)
# Apply PCA for dimensionality reduction
pca = PCA(n_components=2)
pca.fit(data)
X_pca = pca.transform(data)
# Apply K-means clustering
kmeans = KMeans(K=3, max_iters=150, plot_steps=False)
kmeans.fit(data)
# Analyze the clustering results
# (e.g., visualize clusters, identify key features for each cluster)
kmeans.plot()
# Interpret and use the customer segments for targeted marketing, personalized recommendations, etc.
Contributing
Contributions to the MyClustering package are welcome! If you find any issues, have suggestions for improvements, or would like to add new features, feel free to open an issue or submit a pull request on the GitHub repository.
License
The myclustering package is licensed under the MIT License. See the MIT for more information.
Credits
This package was created with Cookiecutter_ and the audreyr/cookiecutter-pypackage_ project template.
.. _Cookiecutter: https://github.com/audreyr/cookiecutter
.. _audreyr/cookiecutter-pypackage: https://github.com/audreyr/cookiecutter-pypackage
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file myclustering-0.1.0.tar.gz.
File metadata
- Download URL: myclustering-0.1.0.tar.gz
- Upload date:
- Size: 19.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6f13469fdc23bc3bc75ed387470bcdcb4cf706b2e5c2b93bf4b2f6d6eb9f497
|
|
| MD5 |
bc675441b1cf474936ec205c3cf428a0
|
|
| BLAKE2b-256 |
3821187c70595197da900e0693dc67d749c9b6e37cab4421f6b06fbbe0b46b33
|
File details
Details for the file myclustering-0.1.0-py2.py3-none-any.whl.
File metadata
- Download URL: myclustering-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8cd8a4a1a59c10fae79638b24c1ca2c6974cfa07c8b666f83db4b3435261f97b
|
|
| MD5 |
0ca5a1f8ef00fad35203960d4e078c0e
|
|
| BLAKE2b-256 |
979e443cc059c7375498285896fe6c79f4f8ab12b237aa9cd62495f5e960b0ec
|