Python?sklearn?中的?make_blobs()?函數(shù)示例詳解
一、介紹
make_blobs() 是 sklearn.datasets中的一個(gè)函數(shù)。
主要是產(chǎn)生聚類數(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,表示類別數(shù)(標(biāo)簽的種類數(shù)),默認(rèn)值3;cluster_std = 1.0,表示每個(gè)類別的方差,例如我們希望生成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í)建議給定數(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聚類使用方法淺析
- python?sklearn與pandas實(shí)現(xiàn)缺失值數(shù)據(jù)預(yù)處理流程詳解
- Python sklearn分類決策樹方法詳解
- Python sklearn對(duì)文本數(shù)據(jù)進(jìn)行特征化提取
相關(guān)文章
Python 隨機(jī)生成測(cè)試數(shù)據(jù)的模塊:faker基本使用方法詳解
這篇文章主要介紹了Python 隨機(jī)生成測(cè)試數(shù)據(jù)的模塊:faker基本使用方法,結(jié)合實(shí)例形式詳細(xì)分析了Python 隨機(jī)生成測(cè)試數(shù)據(jù)的模塊faker基本功能、原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04
Python3.6實(shí)現(xiàn)帶有簡(jiǎn)單界面的有道翻譯小程序
本文通過實(shí)例代碼給大家介紹了基于Python3.6實(shí)現(xiàn)帶有簡(jiǎn)單界面的有道翻譯小程序,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-04-04
Python函數(shù)基礎(chǔ)(定義函數(shù)、函數(shù)參數(shù)、匿名函數(shù))
這篇文章介紹了Python函數(shù)的基礎(chǔ)用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
opencv-python 開發(fā)環(huán)境的安裝、配置教程詳解
這篇文章主要介紹了opencv-python 開發(fā)環(huán)境的安裝、配置,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
Python框架Flask的基本數(shù)據(jù)庫操作方法分析
這篇文章主要介紹了Python框架Flask的基本數(shù)據(jù)庫操作方法,結(jié)合實(shí)例形式分析了Flask框架數(shù)據(jù)庫操作常用函數(shù)功能、用法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2018-07-07
Python使用Slider組件實(shí)現(xiàn)調(diào)整曲線參數(shù)功能示例
這篇文章主要介紹了Python使用Slider組件實(shí)現(xiàn)調(diào)整曲線參數(shù)功能,結(jié)合實(shí)例形式分析了Python使用matplotlib與Slider組件進(jìn)行圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2019-09-09
TensorFlow實(shí)現(xiàn)AutoEncoder自編碼器
這篇文章主要為大家詳細(xì)介紹了TensorFlow實(shí)現(xiàn)AutoEncoder自編碼器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03

