sklearn 笔记:make_blobs 生成聚类数据

本文介绍了sklearn.datasets模块中的make_blobs函数,用于生成二维的多类别样本数据。通过调整参数n_features、n_samples、centers、cluster_std等,可以定制不同数量特征和样本的聚类数据。示例中展示了如何生成两个类别的数据,并使用matplotlib进行可视化展示,帮助理解数据分布。
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()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UQI-LIUWJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值