分享python机器学习中应用所产生的聚类数据集方法
作者:卓晴 发布时间:2021-06-05 13:28:39
01直接生成
这类方法是利用基本程序软件包numpy的随机数产生方法来生成各类用于聚类算法数据集合,也是自行制作轮子的生成方法。
一、基础类型
1、月牙形数据集合
from headm import *import numpy as nppltgif = PlotGIF()def moon2Data(datanum): x1 = linspace(-3, 3, datanum) noise = np.random.randn(datanum) * 0.15 y1 = -square(x1) / 3 + 4.5 + nois x2 = linspace(0, 6, datanum) noise = np.random.randn(datanum) * 0.15 y2 = square(x2 - 3) / 3 + 0.5 + noise plt.clf() plt.axis([-3.5, 6.5, -.5, 5.5]) plt.scatter(x1, y1, s=10) plt.scatter(x2, y2, s=10) plt.draw() plt.pause(.1) pltgif.append(plt)for _ in range(20): moon2Data(300)pltgif.save(r'd:\temp\GIF1.GIF')
2、方形数据集
from headm import *import numpy as nppltgif = PlotGIF()def moon2Data(datanum): x = np.random.rand(datanum, 2) condition1 = x[:, 1] <= x[:, 0] condition2 = x[:, 1] <= (1-x[:, 0]) index1 = np.where(condition1 & condition2) x1 = x[index1] x = np.delete(x, index1, axis=0) index2 = np.where(x[:, 0] <= 0.5) x2 = x[index2] x3 = np.delete(x, index2, axis=0) plt.clf() plt.scatter(x1[:, 0], x1[:, 1], s=10) plt.scatter(x2[:, 0], x2[:, 1], s=10) plt.scatter(x3[:, 0], x3[:, 1], s=10) plt.draw() plt.pause(.1) pltgif.append(plt)for _ in range(20): moon2Data(1000)pltgif.save(r'd:\temp\GIF1.GIF')
3、螺旋形数据集合
from headm import *import numpy as nppltgif = PlotGIF()def randData(datanum): t = 1.5 * pi * (1+3*random.rand(1, datanum)) x = t * cos(t) y = t * sin(t) X = concatenate((x,y)) X += 0.7 * random.randn(2, datanum) X = X.T norm = plt.Normalize(y.min(), y.max()) plt.clf() plt.scatter(X[:, 0], X[:, 1], s=10, c=norm(X[:,0]), cmap='viridis') plt.axis([-20, 21, -20, 16]) plt.draw() plt.pause(.1) pltgif.append(plt)for _ in range(20): randData(1000)pltgif.save(r'd:\temp\GIF1.GIF')
下面的知识螺旋线,没有随机移动的点。
将随机幅值从原来的0.7增大到1.5,对应的数据集合为:
02样本生成器
利用sklearn.datasets自带的样本生成器来生成相应的数据集合。
一、基础数据集
1、点簇形数据集合
from headm import *from sklearn.datasets import make_blobspltgif = PlotGIF()def randData(datanum): x1,y1 = make_blobs(n_samples=datanum, n_features=2, centers=3, random_state=random.randint(0, 1000)) plt.clf() plt.scatter(x1[:,0], x1[:, 1], c=y1, s=10) plt.draw() plt.pause(.1) pltgif.append(plt)for _ in range(20): randData(300)pltgif.save(r'd:\temp\gif1.gif')
绘制三簇点集合,也可以使用如下的语句:
plt.scatter(x1[y1==0][:,0], x1[y1==0][:,1], s=10)plt.scatter(x1[y1==1][:,0], x1[y1==1][:,1], s=10)plt.scatter(x1[y1==2][:,0], x1[y1==2][:,1], s=10)
2、线簇形数据集合
生成代码,只要在前面的x1后面使用旋转矩阵。
transformation = [[0.60834549, -0.63667341], [-0.40887718, 0.85253229]]x1 = dot(x1, transformation)
其中转换矩阵的特征值与特征向量为:
特征值:[0.20581711.25506068]
特征向量:[[-0.845237740.7015526][-0.53439045-0.71261768]]
3、环形数据集合
from headm import *from sklearn.datasets import make_circlespltgif = PlotGIF()def randData(datanum): x1,y1 = make_circles(n_samples=datanum, noise=0.07, random_state=random.randint(0, 1000), factor=0.6) plt.clf() plt.scatter(x1[y1==0][:,0], x1[y1==0][:,1], s=10) plt.scatter(x1[y1==1][:,0], x1[y1==1][:,1], s=10) plt.axis([-1.2, 1.2, -1.2, 1.2]) plt.draw() plt.pause(.1) pltgif.append(plt)for _ in range(20): randData(1000)pltgif.save(r'd:\temp\gif1.gif')
4、月牙数据集合
from headm import *from sklearn.datasets import make_moonspltgif = PlotGIF()def randData(datanum): x1,y1 = make_moons(n_samples=datanum, noise=0.07, random_state=random.randint(0, 1000)) plt.clf() plt.scatter(x1[y1==0][:,0], x1[y1==0][:,1], s=10) plt.scatter(x1[y1==1][:,0], x1[y1==1][:,1], s=10) plt.axis([-1.5, 2.5, -1, 1.5]) plt.draw() plt.pause(.1) pltgif.append(plt)for _ in range(20): randData(1000)pltgif.save(r'd:\temp\gif1.gif')
测试结论
sklearn里面还有好多函数来自定制数据,除此之外还可以使用numpy生成,然后通过高级索引进行划分,最好结合着matplotlib中的cmap来做颜色映射,这样可以做出好玩又好看的数据集,希望大家以后多多支持脚本之家!
来源:https://blog.csdn.net/zhuoqingjoking97298/article/details/119114663
猜你喜欢
- Symfony2是一个基于PHP语言的Web开发框架,有着开发速度快、性能高等特点。本文以一个程序示例的实现过程详细叙述了Symfony2框
- 一天不小心把ROOT的权限改到最小了(只能登录,什么都做不了),这可急死我了.重装的话太麻烦,而且里面有很多的用户,一个个重新弄不知道到什么
- 方法一 :这个是我在站长工具的查询页面使用的防止频繁查询,刷新页面的代码!下面函数的功能是3秒内查询页面即刷新了页面,超过2次就提示!sea
- 前言Python语言处理字符串、数组类的问题时有一定概率需要使用切片方法,比如:Leetcode_5。学习官方解法时发现切片的索引可以超出字
- Google中秋的logo出来了,酷似一美男站在月亮上,结果被网友弄出一撒尿版来。中国网民好智慧啊~原logo: 撒尿版logo:
- 加号+, 是字符串优先.并且从左向右计算. 就是运算前后两个值,只要有一个是字符串,就会将其中一个非字符串的试图转换成字符串.
- jupyter notebookjupyter不被识别为内部或外部命令,可操作程序或批处理文件'上次 jupyter noteboo
- 本文实例讲述了python使用Flask框架获取用户IP地址的方法。分享给大家供大家参考。具体如下:下面的代码包含了html页面和pytho
- 下面这些命令可以在命令行下用isql执行,isql -E -Q "命令",isql.exe
- 一、介绍正则表达式各语言都有自己的规范,但是基本都差不多,都是由元字符的组合来进行匹配;由于Nmap内嵌的服务与版本探测是使用的Perl正则
- 以Python 3.x版本为主一、比较运算符比较运算符主要是True和Fals两个值编号比较运算符说明1==等于2!=<>(Py
- 本文实例讲述了python使用正则表达式提取网页URL的方法。分享给大家供大家参考。具体实现方法如下:import reimport url
- 1、手上目前拥有数据集是一大坨,没有train,test,val的划分如图所示2、目录结构:|---data |
- 按照CSS规范,浮动元素(floats)会被移出文档流,不会影响到块状盒子的布局而只会影响内联盒子(通常是文本)的排列。因此当其高度超出包含
- 1、目的:在Python中实现只读取扩展名为xlsx的文件解决方法:使用os模块。解决思路:1、确定目录2、循环遍历每一个文件3、筛选符合条
- 在写 Python 代码的时候,一个很好的编码实践就是使得你的代码简洁,易懂。组织代码,设置变量,以及给函数有意义的名字,都是几个不错的方法
- 我就废话不多说了,大家还是直接看代码吧~#! python3# -*- coding: utf-8 -*-import os, codecs
- 1、grid 布局说明:参数说明: sticky:在插件正常尺寸下,分配单元中多余的空间(如果没有声明属性,默认插
- 1. 截取GB2312中文字符串 <?php //截取中文字符串 function mysubstr($str, $star
- Hello, 大家好,又是我~ 大家有看过font set和一些要注意的基本问题以及通用字体族两篇文章后,应该对字体的基本有了一些了解。现