Scikit-learn: How to run KMeans on a one-dimensional array?

前端 未结 2 1682
暖寄归人
暖寄归人 2020-12-25 12:28

I have an array of 13.876(13,876) values between 0 and 1. I would like to apply sklearn.cluster.KMeans to only this vector to find the different clusters in whi

2条回答
  •  失恋的感觉
    2020-12-25 13:09

    You have many samples of 1 feature, so you can reshape the array to (13,876, 1) using numpy's reshape:

    from sklearn.cluster import KMeans
    import numpy as np
    x = np.random.random(13876)
    
    km = KMeans()
    km.fit(x.reshape(-1,1))  # -1 will be calculated to be 13876 here
    

提交回复
热议问题