Let's take as an example the Breast Cancer Dataset from the UCI Machine Learning.
Here are the imports I used
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
This is how it looks
>> _data.head(5)
Age BMI Glucose Insulin HOMA Leptin Adiponectin Resistin \
0 48 23.500000 70 2.707 0.467409 8.8071 9.702400 7.99585
1 83 20.690495 92 3.115 0.706897 8.8438 5.429285 4.06405
2 82 23.124670 91 4.498 1.009651 17.9393 22.432040 9.27715
3 68 21.367521 77 3.226 0.612725 9.8827 7.169560 12.76600
4 86 21.111111 92 3.549 0.805386 6.6994 4.819240 10.57635
MCP.1 Classification
0 417.114 1
1 468.786 1
2 554.697 1
3 928.220 1
4 773.920 1
As you can see, all the columns are numerical. Let's see now, how we can cluster the dataset with K-Means. We don't need the last column which is the Label.
### Get all the features columns except the class
features = list(_data.columns)[:-2]
### Get the features data
data = _data[features]
Now, perform the actual Clustering, simple as that.
clustering_kmeans = KMeans(n_clusters=2, precompute_distances="auto", n_jobs=-1)
data['clusters'] = clustering_kmeans.fit_predict(data)
There is no difference at all with 2 or more features. I just pass the Dataframe with all my numeric columns.
Age BMI Glucose Insulin HOMA Leptin Adiponectin Resistin \
0 48 23.500000 70 2.707 0.467409 8.8071 9.702400 7.99585
1 83 20.690495 92 3.115 0.706897 8.8438 5.429285 4.06405
2 82 23.124670 91 4.498 1.009651 17.9393 22.432040 9.27715
3 68 21.367521 77 3.226 0.612725 9.8827 7.169560 12.76600
4 86 21.111111 92 3.549 0.805386 6.6994 4.819240 10.57635
cluster
0 0
1 0
2 0
3 0
4 0
How you can visualize the clustering now? Well, you cannot do it directly if you have more than 3 columns. However, you can apply a Principal Component Analysis to reduce the space in 2 columns and visualize this instead.
### Run PCA on the data and reduce the dimensions in pca_num_components dimensions
pca_num_components = 2
reduced_data = PCA(n_components=pca_num_components).fit_transform(data)
results = pd.DataFrame(reduced_data,columns=['pca1','pca2'])
sns.scatterplot(x="pca1", y="pca2", hue=data['clusters'], data=results)
plt.title('K-means Clustering with 2 dimensions')
plt.show()
And this is the visualization
