ImportError:无法从“sklearn.cluster._kmeans”导入名称“_init_centroids”

Elm*_*lmi 1 python k-means scikit-learn

我正在使用 python 3.8.8,Tensorflow-gpu:2.4.1,scikit-learn 的版本是 0.24.2。

使用旧版本我没有这个问题,但我必须升级我的 python 和tensorflow才能在gpu上运行我的代码。错误是

from sklearn.cluster._kmeans import _init_centroids
ImportError: cannot import name '_init_centroids' from 'sklearn.cluster._kmeans' 
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

Cam*_* M. 5

_init_centroids该模块不再存在该方法_kmeansKMeans现在它是类的一个方法。

>>> from sklearn.cluster import KMeans
>>> KMeans._init_centroids?
    Signature:
    KMeans._init_centroids(
        self,
        X,
        x_squared_norms,
        init,
        random_state,
        init_size=None,
    )
    Docstring:
    Compute the initial centroids.
    
    Parameters
    ----------
    X : {ndarray, sparse matrix} of shape (n_samples, n_features)
        The input samples.
    
    x_squared_norms : ndarray of shape (n_samples,)
        Squared euclidean norm of each data point. Pass it if you have it
        at hands already to avoid it being recomputed here.
    
    init : {'k-means++', 'random'}, callable or ndarray of shape                 (n_clusters, n_features)
        Method for initialization.
    
    random_state : RandomState instance
        Determines random number generation for centroid initialization.
        See :term:`Glossary <random_state>`.
    
    init_size : int, default=None
        Number of samples to randomly sample for speeding up the
        initialization (sometimes at the expense of accuracy).
    
    Returns
    -------
    centers : ndarray of shape (n_clusters, n_features)
    File:      c:\anaconda\lib\site-packages\sklearn\cluster\_kmeans.py
    Type:      function
Run Code Online (Sandbox Code Playgroud)

这意味着,如果您绝对需要使用该方法_init_centroids,则必须从类中使用它KMeans。查看源代码,你会发现_init_centroids里面有一个方法class KMeans(...)。这是一个使用示例

from sklearn.cluster import KMeans
import numpy as np

np.random.seed(0)

X = np.random.randn(100, 2) # random data

# define your model
model = KMeans(n_clusters=2)

# call _init_centroids
centroids = model._init_centroids(X, init='k-means++', 
                                  x_squared_norms=None,
                                  random_state=np.random.RandomState(seed=0))

>>> centroids
    array([[-1.07075262,  1.05445173],
           [ 0.85683061, -0.65102559]])
Run Code Online (Sandbox Code Playgroud)