Python?sklearn?中的?make_blobs()?函數(shù)示例詳解
一、介紹
make_blobs()
是 sklearn.datasets中的一個(gè)函數(shù)。
主要是產(chǎn)生聚類(lèi)數(shù)據(jù)集,產(chǎn)生一個(gè)數(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 個(gè)參數(shù):
n_samples = 100
,表示數(shù)據(jù)樣本點(diǎn)個(gè)數(shù),默認(rèn)值100;n_features = 2
,是每個(gè)樣本的特征(或?qū)傩裕?shù),也表示數(shù)據(jù)的維度,默認(rèn)值是2;centers = 3
,表示類(lèi)別數(shù)(標(biāo)簽的種類(lèi)數(shù)),默認(rèn)值3;cluster_std = 1.0
,表示每個(gè)類(lèi)別的方差,例如我們希望生成2類(lèi)數(shù)據(jù),其中一類(lèi)比另一類(lèi)具有更大的方差,可以將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í)建議給定數(shù)值。
到此這篇關(guān)于Python sklearn 中的 make_blobs() 函數(shù)詳解的文章就介紹到這了,更多相關(guān)Python sklearn make_blobs() 函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python中使用sklearn進(jìn)行特征降維的方法
- Python sklearn CountVectorizer使用詳解
- Python?sklearn預(yù)測(cè)評(píng)估指標(biāo)混淆矩陣計(jì)算示例詳解
- Python+Sklearn實(shí)現(xiàn)異常檢測(cè)
- Python sklearn中的K-Means聚類(lèi)使用方法淺析
- python?sklearn與pandas實(shí)現(xiàn)缺失值數(shù)據(jù)預(yù)處理流程詳解
- Python sklearn分類(lèi)決策樹(shù)方法詳解
- Python sklearn對(duì)文本數(shù)據(jù)進(jìn)行特征化提取
相關(guān)文章
Python函數(shù)中的函數(shù)(閉包)用法實(shí)例
這篇文章主要介紹了Python函數(shù)中的函數(shù)(閉包)用法,結(jié)合實(shí)例形式分析了Python閉包的定義與使用技巧,需要的朋友可以參考下2016-03-03python找出列表中大于某個(gè)閾值的數(shù)據(jù)段示例
今天小編就為大家分享一篇python找出列表中大于某個(gè)閾值的數(shù)據(jù)段示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11python類(lèi)中的self和變量用法及說(shuō)明
這篇文章主要介紹了python類(lèi)中的self和變量用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11Python實(shí)現(xiàn)蒙特卡洛算法小實(shí)驗(yàn)過(guò)程詳解
這篇文章主要介紹了Python實(shí)現(xiàn)基于蒙特卡洛算法過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07Flask框架中request、請(qǐng)求鉤子、上下文用法分析
這篇文章主要介紹了Flask框架中request、請(qǐng)求鉤子、上下文用法,結(jié)合實(shí)例形式分析了flask框架中request、請(qǐng)求鉤子及上下文的功能、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-07-07