溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python?sklearn中的make_blobs()函數(shù)怎么使用

發(fā)布時間:2023-02-22 14:16:18 來源:億速云 閱讀:121 作者:iii 欄目:開發(fā)技術(shù)

這篇“Python sklearn中的make_blobs()函數(shù)怎么使用”文章的知識點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Python sklearn中的make_blobs()函數(shù)怎么使用”文章吧。

一、介紹

make_blobs() 是 sklearn.datasets中的一個函數(shù)。

主要是產(chǎn)生聚類數(shù)據(jù)集,產(chǎn)生一個數(shù)據(jù)集和相應(yīng)的標(biāo)簽。

函數(shù)的源代碼如下:

def make_blobs(n_samples = 100, n_features = 2, centers = 3, cluster_std = 1.0,
               center_box = (-10.0, 10.0), shuffle = True, random_state = None):
    """Generate isotropic Gaussian blobs for clustering.

    Read more in the :ref:`User Guide <sample_generators>`.

    Parameters
    ----------
    n_samples : int, optional (default=100)
        The total number of points equally divided among clusters.

    n_features : int, optional (default=2)
        The number of features for each sample.

    centers : int or array of shape [n_centers, n_features], optional
        (default=3)
        The number of centers to generate, or the fixed center locations.

    cluster_std: float or sequence of floats, optional (default=1.0)
        The standard deviation of the clusters.

    center_box: pair of floats (min, max), optional (default=(-10.0, 10.0))
        The bounding box for each cluster center when centers are
        generated at random.

    shuffle : boolean, optional (default=True)
        Shuffle the samples.

    random_state : int, RandomState instance or None, optional (default=None)
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`.

    Returns
    -------
    X : array of shape [n_samples, n_features]
        The generated samples.

    y : array of shape [n_samples]
        The integer labels for cluster membership of each sample.

    Examples
    --------
    >>> from sklearn.datasets.samples_generator import make_blobs
    >>> X, y = make_blobs(n_samples=10, centers=3, n_features=2,
    ...                   random_state=0)
    >>> print(X.shape)
    (10, 2)
    >>> y
    array([0, 0, 1, 0, 2, 2, 2, 1, 1, 0])

    See also
    --------
    make_classification: a more intricate variant
    """
    generator = check_random_state(random_state)

    if isinstance(centers, numbers.Integral):
        centers = generator.uniform(center_box[0], center_box[1],
                                    size=(centers, n_features))
    else:
        centers = check_array(centers)
        n_features = centers.shape[1]

    if isinstance(cluster_std, numbers.Real):
        cluster_std = np.ones(len(centers)) * cluster_std

    X = []
    y = []

    n_centers = centers.shape[0]
    n_samples_per_center = [int(n_samples // n_centers)] * n_centers

    for i in range(n_samples % n_centers):
        n_samples_per_center[i] += 1

    for i, (n, std) in enumerate(zip(n_samples_per_center, cluster_std)):
        X.append(centers[i] + generator.normal(scale = std,
                                               size = (n, n_features)))
        y += [i] * n

    X = np.concatenate(X)
    y = np.array(y)

    if shuffle:
        indices = np.arange(n_samples)
        generator.shuffle(indices)
        X = X[indices]
        y = y[indices]

    return X, y

二、函數(shù)的使用

make_blobs(n_samples = 100, n_features = 2, centers = 3, cluster_std = 1.0, center_box = (-10.0, 10.0), shuffle = True, random_state = None)

可以看到它有 7 個參數(shù):

  • n_samples = 100 ,表示數(shù)據(jù)樣本點(diǎn)個數(shù),默認(rèn)值100;

  • n_features = 2 ,是每個樣本的特征(或?qū)傩裕?shù),也表示數(shù)據(jù)的維度,默認(rèn)值是2;

  • centers = 3 ,表示類別數(shù)(標(biāo)簽的種類數(shù)),默認(rèn)值3;

  • cluster_std = 1.0 ,表示每個類別的方差,例如我們希望生成2類數(shù)據(jù),其中一類比另一類具有更大的方差,可以將cluster_std設(shè)置為[1.0, 3.0],浮點(diǎn)數(shù)或者浮點(diǎn)數(shù)序列,默認(rèn)值1.0;

  • center_box = (-10.0, 10.0) ,中心確定之后的數(shù)據(jù)邊界,默認(rèn)值(-10.0, 10.0);

  • shuffle = True ,將數(shù)據(jù)進(jìn)行洗亂,默認(rèn)值是True;

  • random_state = None ,官網(wǎng)解釋是隨機(jī)生成器的種子,可以固定生成的數(shù)據(jù),給定數(shù)之后,每次生成的數(shù)據(jù)集就是固定的。若不給定值,則由于隨機(jī)性將導(dǎo)致每次運(yùn)行程序所獲得的的結(jié)果可能有所不同。在使用數(shù)據(jù)生成器練習(xí)機(jī)器學(xué)習(xí)算法練習(xí)或python練習(xí)時建議給定數(shù)值。

以上就是關(guān)于“Python sklearn中的make_blobs()函數(shù)怎么使用”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI