Package: kmeanswithnulls
KmeansWithNulls (KWN) Clustering for Multivariate ImplementationThis repository contains a Python implementation of the KMeans clustering algorithm which includes support for handling missing values in multivariate dataset.
The implementation allows for specifying the number of clusters, maximum iterations, and the random state for reproducibility.FeaturesKMeans clustering.Handles missing data (NaN values) in the dataset.Customizable number of clusters and iterations.Utilizes NumPy for efficient numerical computations.InstallationNo installation is required, just clone this repository using the following command:gitclonehttps://github.com/aasedek/KmeansWithNulls.gitUsageTo use the KmeansWithNulls class, import it into your Python script and create an instance of the class. Then call thefitmethod with your dataset:fromKmeansWithNullsimportKmeansWithNullsimportnumpyasnp# Example datasetdata=np.array([[1,2],[1,4],[1,0],[10,2],[10,4],[10,0]])# Initialize the KmeansWithNulls classkmeans_with_nulls=KmeansWithNulls(n_clusters=2,max_iter=300,random_state=42)# Fit the model to your datakmeans_with_nulls.fit(data)# Predict the clusterslabels=kmeans_with_nulls.predict(data)Example:importnumpyasnpimportpandasaspdimportKmeansWithNulls# Create a synthetic dataset with 100 points and 2 featuresnp.random.seed(42)X_demo=np.random.rand(100,2)*10# Scale the features by 10 for better visualization# Introduce NaN values randomly in the datasetnan_indices=np.random.choice(np.arange(X_demo.size),replace=False,size=10)X_demo.ravel()[nan_indices]=np.nan# Convert the array to a DataFrame for a better view of the NaN valuesX_demo_df=pd.DataFrame(X_demo,columns=['Feature1','Feature2'])# Instantiate the KMeansWithNulls classkmeans_with_nulls=KmeansWithNulls(n_clusters=3)# Fit the model to the data with NaNskmeans_with_nulls.fit(X_demo)# Now let's predict the cluster labels for the datasetpredictions_with_nulls=kmeans_with_nulls.predict(X_demo)# Output the centroids and predictionscentroids_with_nulls=kmeans_with_nulls.centroidspredictions_with_nulls# Display the centroids and the first few predictionsprint("Centroids:\n",centroids_with_nulls)print("\nFirst 10 Predictions:\n",predictions_with_nulls[:10])# Print the first few rows of the DataFrame with NaN valuesX_demo_df.head()# Plotting resultsimportmatplotlib.pyplotasplt# Plotting the clusters with nullsplt.figure(figsize=(8,6))# Plot each cluster with available data, ignoring NaNsforiinrange(kmeans_with_nulls.n_clusters):# Select data points without NaNs that are assigned to the current clustercluster_data=X_demo[~np.isnan(X_demo).any(axis=1)&(predictions_with_nulls==i)]plt.scatter(cluster_data[:,0],cluster_data[:,1],label=f'Cluster{i+1}',s=50)# Plot the centroids, excluding NaNs for plotting purposesforidx,centroidinenumerate(centroids_with_nulls):ifnotnp.isnan(centroid).any():plt.scatter(centroid[0],centroid[1],c='black',s=200,marker='x',label=f'Centroid{idx+1}')plt.title('K-Means Clustering with Nulls')plt.xlabel('Feature 1')plt.ylabel('Feature 2')plt.legend()plt.show()ContributingContributions to improve this implementation are welcome. Before creating a pull request, please ensure your code follows the existing code structure and style.LicenseThis project is open-sourced under the GNU General Public License v3.0. See theLICENSEfile for details.ContactFor questions and feedback, please reach out to me atarthur.sedek@gmail.comAcknowledgementsThis implementation was inspired by the KMeans algorithm from scikit-learn.