Clustering with missing data

Viewed 31

I am trying to perform k-means clustering for this data.

It has NaN values. I want to ignore NaN and give clusters to only the remaining columns.

    Col1  Col2  Col3  Col4   Animal  clusters
0   1.0    1.0  2.0   2.0    Rabbit
1   1.0    1.0  NaN   1.0    Cat      NaN
2   1.0    1.0  2.0   2.0    Dog
3   2.0    NaN  2.0   2.0    Cat      NaN
4   2.0    0.0  0.0   3.0    Dog

Clusters are prepared using columns 1,2,3,4 and I need the Animal and Cluster column for further analysis.

This is the code I used. But i do not know how to ignore Nans

clustering_kmeans = KMeans(n_clusters=3, precompute_distances="auto", n_jobs=-1)
data.iloc[:, :-1]['clusters'] = clustering_kmeans.fit_predict(data)

Can anyone help me with this problem?

1 Answers

You need a number in those places (Nan = Not a number) to perform k-means. Some workarounds for this case is to either fill them up with the mean of the feature or assign them to the closest cluster

Related