sklearn 笔记:make_blobs 生成聚类数据
生活随笔
收集整理的這篇文章主要介紹了
sklearn 笔记:make_blobs 生成聚类数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
from sklearn.datasets import make_blobs
1 基本用法
data, label = make_blobs(n_features=2, n_samples=100, centers=3, random_state=3, cluster_std=[0.8, 2, 5])2 參數說明
| n_features | 每一個樣本有多少個特征值 |
| n_samples | 樣本的個數 |
| centers | 聚類中心的個數 也可以是一個列表,表示每個中心 對應的坐標(維度和n_features一樣) |
| random_state | 隨機種子 |
| cluster_std | 每個類別的方差 |
3 使用舉例
3.1 centers 為數字
from sklearn.datasets import make_blobs data, label = make_blobs(n_features=2, n_samples=100, centers=2, random_state=2019, cluster_std=[0.6,0.7] )label ''' array([1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1,0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0,1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1,0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0,0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1]) ''' # 100維data ''' array([[ 2.42739457, 3.06175875],[ 7.05586911, -2.34535549],[ 7.49164297, -2.09940869],[ 8.95763559, -3.40157028],[ 2.17353708, 3.06694536], ......[ 2.29981982, 3.8569118 ]]) ''' #100*2 維 import matplotlib.pyplot as plt colors=['green','blue'] for i,color in enumerate(colors):color_tmp=np.where(label==i)[0]plt.scatter(data[color_tmp,0],data[color_tmp,1],c=color,label=i) plt.legend()3.2?center? 為坐標
from sklearn.datasets import make_blobs data, label = make_blobs(n_features=2, n_samples=100, centers=[[-100,1],[0,5]], random_state=2019, cluster_std=[0.6,0.7] import matplotlib.pyplot as plt colors=['green','blue'] for i,color in enumerate(colors):color_tmp=np.where(label==i)[0]plt.scatter(data[color_tmp,0],data[color_tmp,1],c=color,label=i) plt.legend()?
總結
以上是生活随笔為你收集整理的sklearn 笔记:make_blobs 生成聚类数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 笔记 size-const
- 下一篇: statsmodels 笔记 STL