您好,登錄后才能下訂單哦!
這篇“Python sklearn中的make_blobs()函數怎么使用”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Python sklearn中的make_blobs()函數怎么使用”文章吧。
make_blobs()
是 sklearn.datasets中的一個函數。
主要是產生聚類數據集,產生一個數據集和相應的標簽。
函數的源代碼如下:
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
二、函數的使用
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 個參數:
n_samples = 100
,表示數據樣本點個數,默認值100;
n_features = 2
,是每個樣本的特征(或屬性)數,也表示數據的維度,默認值是2;
centers = 3
,表示類別數(標簽的種類數),默認值3;
cluster_std = 1.0
,表示每個類別的方差,例如我們希望生成2類數據,其中一類比另一類具有更大的方差,可以將cluster_std設置為[1.0, 3.0],浮點數或者浮點數序列,默認值1.0;
center_box = (-10.0, 10.0)
,中心確定之后的數據邊界,默認值(-10.0, 10.0);
shuffle = True
,將數據進行洗亂,默認值是True;
random_state = None
,官網解釋是隨機生成器的種子,可以固定生成的數據,給定數之后,每次生成的數據集就是固定的。若不給定值,則由于隨機性將導致每次運行程序所獲得的的結果可能有所不同。在使用數據生成器練習機器學習算法練習或python練習時建議給定數值。
以上就是關于“Python sklearn中的make_blobs()函數怎么使用”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。