【机器学习】【样本数据生成器】聚类算法中使用make_blobs聚类数据生成器(sklearn.datasets.make_blobs)

官网manual详见:sklearn.datasets.make_blobs

klearn.datasets.make_blobs( n_samples=100,             #样本总数
                            n_features=2,              #每个样本的特征值总数
                            centers=3,                 #有几簇聚类数据
                            cluster_std=1.0,           #每簇的标准差,可以设置成[1.0, 2.0, 4.0]依次依次表示每簇数据的标准差
                            center_box=(-10.0, 10.0),  
                            shuffle=True, 
                            random_state=None)

一般使用sklearn的make_blobs模块生成聚类算法使用的测试数据样本集,聚类数据样本集。

样本集:

1)n_samples:样本集中的样本总数

2)n_features:如果每个样本有2个特征数,则可以通过设置入参n_features=2即可

3)centers:如果需要有2簇(或者3簇)聚类数据,可以通过设置入参centers=2即可

4)返回的data就是样本集,data是一个n_features列的数组,data的总元素=n_samples。即一个坐标轴代表样本的一个特征

    data, data_flag = make_blobs(……)

5)返回的第二个data_flag就是每个样本的标记

    data, data_flag = make_blobs(……)

1.代码

# -*- coding: utf-8 -*-
"""
@author: 蔚蓝的天空TOM
Talk is cheap, show me the code
Aim:使用sklearn的make_blobs模块生成聚类算法使用的测试数据,聚类数据样本集
"""

from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt

if __name__ == '__main__':
    #待生成的样本的总数N
    N = 400
    #每个样本的特征数m
    feature_cnt = 2
    #要生成的样本中心(类别)数,或者是确定的中心点
    centers = 3
    #每个类别的方差,例如我们希望生成2类数据,其中一类比另一类具有更大的方差,可以将
    stds = [1.0, 2.0, 4.0]
    #生成的样本数据集
    data = []
    #样本数据集的标签
    data_flag = []
    #生成3簇聚类数据集,每个样本有2个特征数
    data, data_flag = make_blobs(n_samples=N, n_features=feature_cnt, centers=centers, cluster_std=stds)

    #显示方法1:打印数据
    print('feature1:',data[:, 0])
    print('feature2:',data[:,1])
    print('sample flag:',data_flag)
    #显示方法2:显示图形
    plt.scatter(data[:, 0], data[:, 1], c=data_flag)
    plt.show()

2.生成的聚类数据样本集

2.1图像显示

【机器学习】【样本数据生成器】聚类算法中使用make_blobs聚类数据生成器(sklearn.datasets.make_blobs)_第1张图片

2.2数据集打印

runfile('C:/Users/Administrator/tom_sklearn_make_blobs.py', wdir='C:/Users/Administrator')
feature1: [  2.54001684   5.22477454  -6.10862356  -9.44626916  -7.19535902
   5.2679688  -11.42425981  -9.67999405  -5.75053619   3.41957156
   5.09385822   3.63539588   2.39926446   3.99817201   4.19291335
   2.73072773   5.76337415   2.4222701    3.15417303   0.94907246
   1.36182381   4.96241642   4.86671958   5.20985281   1.64624643
 -14.72680244   7.99360547   2.65330678   3.15022979   4.56360555
   4.63259477   2.59664438   5.56695351   1.18395658   1.96238448
   1.09608662  -7.66847365   3.41224687   0.49211757   1.18152355
  -9.17519306   3.87926943  -0.13577271   0.93578799   3.55081517
   5.23720729  -0.64761192   4.69662965   7.0559361   -3.96957324
 -15.22902701   4.48168543   5.24452705  -7.14471869   5.04623679
   3.07387132  -8.27907084   9.67937407  -6.45947198  -1.03013999
  -2.95222822   6.08051981   7.47502014   3.87496611   4.26346936
   5.28032249   5.36402547 -12.50263226  -9.98922844   3.44383279
   5.53567406   2.77681492   2.47596677   3.71157681   6.93141516
   4.81154579   4.39246095   3.46001954   7.16033998  -9.36287977
   4.42585417 -12.01046108  -5.48842508  -8.09393056  -7.63301015
   1.3017978    6.32493182   4.91798834   2.83690423 -12.45202027
   6.55735457   0.83800565 -13.92198443   3.55798733   3.47635849
   1.11887909  -7.3717065    5.36060022   4.84613756  -5.44547875
   4.22004423   6.17248305  -5.61493363   3.44629171  -0.30119395
   2.54442362  -4.52647232   5.4049048    3.83076375   3.38269177
  -8.07584942   6.97801761   5.01249312 -12.82025552   3.19128868
   0.01915967  -7.60260281   4.47812045 -11.05616      4.32210167
  -8.69116672 -14.47541942   5.55093824  -6.81995053  -3.46424793
 -15.0478003    7.01657954   1.98619736   4.18689311 -10.59195846
   4.34154671   2.34773687  -3.82972255   2.49290146  -6.79971674
   5.99340176   5.00999004   1.58085981  -3.81399986 -17.91429556
   3.16026452   4.03150833   3.90216926   3.77203953   1.45972767
   5.01045185   3.68028032   1.6941889   -9.52076656  -8.69411543
 -14.21479873   2.89890282  -4.71518133   1.66558516  -5.36897562
 -15.1910513  -11.10300811  -5.42707725   3.117764     2.97779908
 -17.27023219   7.43560257 -13.06688758   0.55781448   3.62325289
   3.89961284   7.18912608   5.1969713    3.16858891  -7.828569
  -0.64736538   2.85120859  -7.7216443    2.37924707 -10.32153546
   4.78406751  -5.93610128   6.45881575   3.02560725 -13.76864526
   3.95054161  -9.46958548   1.93352057  -4.27419178   3.15573802
   5.11376807   2.88904977   3.29729305 -14.17801404   3.10793356
 -14.30615647   1.42852967 -13.16736891  -9.1207519    5.17114257
  -6.00406616   4.11425007   4.62327581   3.89915447  -2.70104915
   4.20338442  -6.07979745  -0.81877116   3.12007292   4.15980286
   4.35376008   4.61922807   4.17522306 -10.47480982 -14.15660931
   3.32435013   3.76674811   1.67381789   2.76483863  -4.89666633
 -11.5697076    4.49286139   4.69041631   3.60032826   2.98966154
 -11.47953484   4.09246416  -3.65444366 -11.81245388   4.15326014
   5.14560023   3.28488777   5.41592429 -12.87356406   4.36345422
 -12.69752338 -11.03798681  -5.18587288 -13.69598773  -4.51256723
   4.52610831   4.8246689    3.10219456   4.37730678   3.12271511
   1.97437983   4.3381746    3.70079951   3.04486924 -11.81158114
   3.3110105    4.18949342   4.26633801   2.30299281   4.30993944
   2.15163664   2.41774354   3.12275675   2.6008485    2.87939312
   3.30214153 -10.73547774   4.07512349   3.49447238   2.14269081
   3.83058592  -7.98708734 -10.96873049   4.81132885   4.57319828
   2.6472614    3.10536484   6.65846973   5.1701295    4.29148281
   3.9504729    8.64968329   5.1115541    3.3396767    1.15475134
   3.63055553   1.29130707   5.48626597   2.25498677   3.82380313
  -5.28364346   4.04933045   3.70676864   4.46852358   3.10599793
   4.49933284  -4.88631602   4.46179572   2.33630831   4.48273994
  -3.3881548    2.63858106   2.06087063   2.45884774   5.69174821
 -10.47167177  -8.91294921   3.88988285   5.36876469 -10.97848427
   2.08420097   3.63561427   1.65981692   4.72266574   7.50226355
  -3.11479009   4.67510804  -9.38099514  -7.51121123   5.06025869
 -12.30393289 -18.3023349    5.43396406   4.29042285   4.9474745
   2.82259014   3.81591397   2.57205612   1.83831286   8.69160799
 -13.38330803   5.36516825  -2.66978701  -8.48009189   4.31809912
   2.66554538   2.97743468  -7.4361818    4.12355177 -14.67419105
   4.3903146   -8.43656594 -12.82606416   4.88737576 -11.74928273
  -7.34046462  -0.29160917 -11.27810822   3.10491547   4.28404647
  -5.62831231   4.48705667  -8.94656524   5.97360668  -6.11086158
  -6.33392368  -8.6906396    1.30013255   5.24645345  -9.86873502
   1.75938389   6.00053569   2.47007016   2.55130305  -7.43933537
   3.57772639   3.33744497 -12.31901139  -5.08053595 -10.3618278
   2.73479637  -5.70588654   2.8326919    2.30470297  -9.5639898
   2.90663074  -0.20308604   3.92394428   4.79539194   5.46654587
   1.97093273   5.12826771   3.70097223   4.06415874 -13.74714065
   5.65744848  -3.11550898  -7.90204097   5.11293166   2.24037331
   3.1409847    5.79456048   4.23209357   4.83098824 -11.52084913
   6.41546178 -10.43674852   3.47067399   6.30777835 -10.20796564
  -1.99534127  -1.3469743    4.11144355   5.93526313  -8.05510561
   5.62432519   2.82347606 -11.2159931    3.55754881   3.40021177]
feature2: [  5.00243197  -9.73014489  -5.40541613  -3.35791757   5.65380623
  -9.2279083    0.49661355  -4.06477365  -3.28652232   6.52045498
  -6.69197888   9.10967481  -7.94082697   7.24244686  -7.99962295
   9.04605494   8.15475066   5.85390605  -7.56229274   7.77637249
  -8.45067951  -9.42754394   7.99815742  -6.15420462   9.83919929
  -8.07112339   8.58693156  -9.99714922  -8.95085152  -8.74209638
  -8.9298814   -8.68646791   4.40774235  -7.21992736   5.71983094
  -8.20117025  -3.75691967  -8.1787106    6.6797308    7.44558349
  -4.30765946  -9.1693361    1.16835121   5.03792399  -9.21486755
  -9.45776835  -3.48606248  -9.19175617   9.7571073   -0.43668415
  -5.6479297   -8.09154068  -7.57378712   6.61440419   8.72150175
  -8.11018272  -2.71201037   4.88411041   1.01675309  -2.76660598
  -7.92114188  10.26739083   9.44663616   7.77877745  -8.68765553
   7.63491524   4.72258202  -9.79000379   0.71140727  -8.89727697
   7.57780466  -8.28398667  -6.52169789  -8.29901036   9.94526363
   6.7319939    9.80853566   6.69384318   7.64737989 -10.97038919
  -7.18149455  -2.50499485   1.23736879  -1.31754489  -1.60424527
   4.11385222   9.40301348   6.05228843  -9.01594053  -6.44196505
   8.42630703   0.45365265   6.17361738  -9.42330449   9.6978061
   3.68463095  -3.47306848   7.54521756   7.38078938   0.20122659
   7.06225305  -9.31980008   2.55688683   9.32872263  -2.86434113
   4.61762854   0.64840482   3.96127174  -9.07793247  -8.83063505
  -4.40247177   7.07430428  10.39906212  -6.3823179   -8.26561134
   0.21312997  -6.23599564  -8.20253898  -3.75294066  -8.62103652
  -4.34700347  -6.09724612   7.54858558   1.70571693 -10.71244054
  -5.97309595   6.88662211  -9.75947147   8.79373495  -4.64101679
  -8.68945069   4.74543829  -3.94189757  -7.85485564  -6.41668825
   5.63228126   9.66858429  -9.45155698  -3.01280834  -2.10610182
  -8.09432238   5.75491056  -8.71814378   8.95783882  -7.74584278
   4.88719519  -7.09493421   5.65651947  -0.8970432   -3.54410773
   0.6560659    6.61533499 -10.8173666   -7.50697336  -4.76240525
  -3.42442474  -0.17562749  -6.05822736  -9.73582487  -9.01686146
  -1.0397827    4.45189671 -18.95888463   5.12461982  -8.47746153
  -8.3884141    5.73625339 -10.18731836  -8.2478336   -8.33394419
  -1.89747988  -6.76328738  -6.13709301 -10.69425754  -5.62726148
  -7.4063552    6.07073079   2.76473286   6.90764778  -5.33478026
  -8.38636671  -6.00185818  -8.30891429  -3.8941615   -8.49088207
  10.66921349  -9.22749587   6.36510837  -6.31632459  -8.29237183
  -3.81702286  -8.19973202   4.33675367  -3.35782641  -9.06036541
 -12.61998831   6.65747214  -8.14969518   6.65078882  -0.22861711
  -8.01812791  -6.06767172  -5.95684927  -5.73826242  -8.62698766
  -8.52677874   6.90924197  -6.63905656   1.76169707  -8.73385859
   9.20835528  -9.60297382   5.53727004  -9.12782318   1.90680221
  -0.33132447   5.98490923   4.8812901   -8.17161697   3.74575392
   3.36851727   4.76191501  -5.97428702  -5.81951938  -8.31599146
  -7.11760157   5.76644899  -8.26407075  -9.09451949   9.42551019
 -12.54652219  -8.55926793  -5.36416495   1.42763995   2.20333104
  -9.25049341   6.04614945  -8.37925738  -9.57283549  -8.66306651
   4.32671369   8.94989248  -8.20537513   6.94561615  -7.28448373
  -6.99322079  -8.6795246    4.4410359    6.15033469  -8.77462036
  -8.78317067  -8.94671893  -8.79493768  -7.49770271   4.67834826
   5.97811348   1.71936361   8.90996576  -8.0355813   -7.57808544
  -9.22056336   0.81741878  -3.91991163  -9.54676634  10.15312805
   7.54791174   8.94657337   9.87739394   7.07882496  -7.80909356
  -8.44885286   8.20071119  -8.77828242  10.3416206    5.62535309
   8.89446964   4.89603598   5.83386674   5.8520997   -6.49204292
   3.5472786    4.55357509  -8.59765645   9.09402304  -8.40894718
  -6.8502887   -2.64761524   4.66313166   6.8361387   -9.16548908
  -3.69170458  -7.02618834  -8.15014038  -7.19471715   9.2498692
  -5.47303775  -6.2108176   -7.09460557   8.43397013  -9.32168281
   8.35209887  -9.26923654   6.29142714   7.70660187   6.95123777
  -3.12596176   5.91808055  -4.03997309  -5.07213929   6.97272209
   3.39620456  -4.18869129  -8.59899771  -8.44384782   6.0702184
  -8.02237096 -10.85656904 -10.43506779  -8.51167239   6.38276427
  -4.67264191   6.61562984  -2.45287561   4.62384095  -9.44309267
  -8.76317212  -9.01220675  -5.82309484   8.75795922  -4.56443651
   4.35872006  -3.31473598   4.64056854  -7.70485814  -1.95726655
   3.08048018   2.75833051  -0.36562047   7.68897469   6.80910891
  -2.18444267  -8.45606969  -2.06931268   6.23533328  -4.37617123
  -1.63254046   4.12349264   4.71078562   6.11360966   7.30142906
  -8.29380007  -9.30242638  -8.40089995  -7.32743395  -0.84906755
   6.11015196   7.06962917  -3.48329162  -2.61403204  -6.8578181
  -8.42139804   2.09751446   9.18930574  -8.04955866   0.24112748
  -8.89060441   3.83170781  -7.72403247   8.05576068  -7.9900562
   2.68715593 -10.33380369  -7.5614925    6.29316617 -11.16532679
   5.28873433  -3.01468229  -1.73606497  10.1905166   -7.26527301
  -8.08832542  10.65528544   9.66575807   9.11541077  -3.70539384
   5.43510942  -2.83315089  -8.04404777   7.21621929  -2.51911535
  -6.9223089   12.80395738  -8.92635443 -10.03846768  -2.35078014
  -9.52337838  -8.55439382  -3.10552363  -7.71549202   7.42837917]
sample flag: [1 0 2 2 2 0 2 2 2 1 0 1 0 1 0 1 1 1 0 1 0 0 1 0 1 2 1 0 0 0 0 0 1 0 1 0 2
 0 1 1 2 0 2 1 0 0 2 0 1 2 2 0 0 2 1 0 2 1 2 2 2 1 1 1 0 1 1 2 2 0 1 0 0 0
 1 1 1 1 1 2 0 2 2 2 2 1 1 1 0 2 1 2 2 0 1 1 2 1 1 2 1 0 2 1 2 1 2 1 0 0 2
 1 1 2 0 2 2 0 2 0 2 2 1 2 2 2 1 0 1 2 0 1 2 0 2 1 1 0 2 2 0 1 0 1 0 1 0 1
 2 2 2 1 2 0 2 2 2 2 0 0 2 1 2 1 0 0 1 0 0 2 2 0 2 0 2 0 2 1 1 2 0 2 0 2 0
 1 0 1 2 0 2 0 2 2 0 2 1 0 1 2 0 2 2 0 0 0 1 0 2 2 1 0 1 0 2 2 1 1 0 1 2 1
 2 2 0 0 1 0 2 1 2 2 2 2 2 0 1 0 0 0 1 1 0 1 2 0 0 1 1 0 0 0 0 0 1 1 2 1 0
 0 0 2 2 0 1 1 1 1 1 0 0 1 0 1 1 1 1 1 1 0 2 1 0 1 0 0 2 1 1 0 2 0 0 0 1 2
 2 0 1 2 1 0 1 1 1 2 1 2 2 1 2 2 0 0 1 0 0 0 0 1 2 1 2 2 0 0 0 2 1 2 1 2 2
 0 2 2 1 2 1 1 2 0 2 1 2 2 2 1 1 2 0 0 0 0 2 1 1 2 2 2 0 2 1 0 2 0 1 0 1 0
 1 0 0 1 2 1 2 2 1 0 0 1 1 1 2 1 2 0 1 2 2 1 0 0 2 0 0 2 0 1]

(end)

你可能感兴趣的:(人工智能,机器学习,python,跟我一起学机器学习,Machine,Learning)