sklearn提供的自带的数据集

sklearn 的数据集有好多个种

  • 自带的小数据集(packaged dataset):sklearn.datasets.load_<name>
  • 可在线下载的数据集(Downloaded Dataset):sklearn.datasets.fetch_<name>
  • 计算机生成的数据集(Generated Dataset):sklearn.datasets.make_<name>
  • svmlight/libsvm格式的数据集:sklearn.datasets.load_svmlight_file(...)
  • 从买了data.org在线下载获取的数据集:sklearn.datasets.fetch_mldata(...)

①自带的数据集

其中的自带的小的数据集为:sklearn.datasets.load_<name>

这些数据集都可以在官网上查到,以鸢尾花为例,可以在官网上找到demo,http://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html

 from sklearn.datasets import load_iris
#加载数据集
iris=load_iris()
iris.keys()  #dict_keys(['target', 'DESCR', 'data', 'target_names', 'feature_names'])
#数据的条数和维数
n_samples,n_features=iris.data.shape
print("Number of sample:",n_samples) #Number of sample: 150
print("Number of feature",n_features)  #Number of feature 4
#第一个样例
print(iris.data[0])      #[ 5.1 3.5 1.4 0.2]
print(iris.data.shape)    #(150, 4)
print(iris.target.shape)  #(150,)
print(iris.target)
"""   [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2] """
import numpy as np
print(iris.target_names)  #['setosa' 'versicolor' 'virginica']
np.bincount(iris.target)  #[50 50 50] import matplotlib.pyplot as plt
#以第3个索引为划分依据,x_index的值可以为0,1,2,3
x_index=3
color=['blue','red','green']
for label,color in zip(range(len(iris.target_names)),color):
plt.hist(iris.data[iris.target==label,x_index],label=iris.target_names[label],color=color) plt.xlabel(iris.feature_names[x_index])
plt.legend(loc="Upper right")
plt.show() #画散点图,第一维的数据作为x轴和第二维的数据作为y轴
x_index=0
y_index=1
colors=['blue','red','green']
for label,color in zip(range(len(iris.target_names)),colors):
plt.scatter(iris.data[iris.target==label,x_index],
iris.data[iris.target==label,y_index],
label=iris.target_names[label],
c=color)
plt.xlabel(iris.feature_names[x_index])
plt.ylabel(iris.feature_names[y_index])
plt.legend(loc='upper left')
plt.show()

手写数字数据集load_digits():用于多分类任务的数据集

 from sklearn.datasets import load_digits
digits=load_digits()
print(digits.data.shape)
import matplotlib.pyplot as plt
plt.gray()
plt.matshow(digits.images[0])
plt.show() from sklearn.datasets import load_digits
digits=load_digits()
digits.keys()
n_samples,n_features=digits.data.shape
print((n_samples,n_features)) print(digits.data.shape)
print(digits.images.shape) import numpy as np
print(np.all(digits.images.reshape((1797,64))==digits.data)) fig=plt.figure(figsize=(6,6))
fig.subplots_adjust(left=0,right=1,bottom=0,top=1,hspace=0.05,wspace=0.05)
#绘制数字:每张图像8*8像素点
for i in range(64):
ax=fig.add_subplot(8,8,i+1,xticks=[],yticks=[])
ax.imshow(digits.images[i],cmap=plt.cm.binary,interpolation='nearest')
#用目标值标记图像
ax.text(0,7,str(digits.target[i]))
plt.show()

乳腺癌数据集load-barest-cancer():简单经典的用于二分类任务的数据集

糖尿病数据集:load-diabetes():经典的用于回归任务的数据集,值得注意的是,这10个特征中的每个特征都已经被处理成0均值,方差归一化的特征值

波士顿房价数据集:load-boston():经典的用于回归任务的数据集

体能训练数据集:load-linnerud():经典的用于多变量回归任务的数据集,其内部包含两个小数据集:Excise是对3个训练变量的20次观测(体重,腰围,脉搏),physiological是对3个生理学变量的20次观测(引体向上,仰卧起坐,立定跳远)

svmlight/libsvm的每一行样本的存放格式:

<label><feature-id>:<feature-value> <feature-id>:<feature-value> ....

这种格式比较适合用来存放稀疏数据,在sklearn中,用scipy sparse CSR矩阵来存放X,用numpy数组来存放Y

 from sklearn.datasets import load_svmlight_file
x_train,y_train=load_svmlight_file("/path/to/train_dataset.txt","")#如果要加在多个数据的时候,可以用逗号隔开

②生成数据集

生成数据集:可以用来分类任务,可以用来回归任务,可以用来聚类任务,用于流形学习的,用于因子分解任务的

用于分类任务和聚类任务的:这些函数产生样本特征向量矩阵以及对应的类别标签集合

make_blobs:多类单标签数据集,为每个类分配一个或多个正太分布的点集

make_classification:多类单标签数据集,为每个类分配一个或多个正太分布的点集,提供了为数据添加噪声的方式,包括维度相关性,无效特征以及冗余特征等

make_gaussian-quantiles:将一个单高斯分布的点集划分为两个数量均等的点集,作为两类

make_hastie-10-2:产生一个相似的二元分类数据集,有10个维度

make_circle和make_moom产生二维二元分类数据集来测试某些算法的性能,可以为数据集添加噪声,可以为二元分类器产生一些球形判决界面的数据

 #生成多类单标签数据集
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_blobs
center=[[1,1],[-1,-1],[1,-1]]
cluster_std=0.3
X,labels=make_blobs(n_samples=200,centers=center,n_features=2,
cluster_std=cluster_std,random_state=0)
print('X.shape',X.shape)
print("labels",set(labels)) unique_lables=set(labels)
colors=plt.cm.Spectral(np.linspace(0,1,len(unique_lables)))
for k,col in zip(unique_lables,colors):
x_k=X[labels==k]
plt.plot(x_k[:,0],x_k[:,1],'o',markerfacecolor=col,markeredgecolor="k",
markersize=14)
plt.title('data by make_blob()')
plt.show() #生成用于分类的数据集
from sklearn.datasets.samples_generator import make_classification
X,labels=make_classification(n_samples=200,n_features=2,n_redundant=0,n_informative=2,
random_state=1,n_clusters_per_class=2)
rng=np.random.RandomState(2)
X+=2*rng.uniform(size=X.shape) unique_lables=set(labels)
colors=plt.cm.Spectral(np.linspace(0,1,len(unique_lables)))
for k,col in zip(unique_lables,colors):
x_k=X[labels==k]
plt.plot(x_k[:,0],x_k[:,1],'o',markerfacecolor=col,markeredgecolor="k",
markersize=14)
plt.title('data by make_classification()')
plt.show() #生成球形判决界面的数据
from sklearn.datasets.samples_generator import make_circles
X,labels=make_circles(n_samples=200,noise=0.2,factor=0.2,random_state=1)
print("X.shape:",X.shape)
print("labels:",set(labels)) unique_lables=set(labels)
colors=plt.cm.Spectral(np.linspace(0,1,len(unique_lables)))
for k,col in zip(unique_lables,colors):
x_k=X[labels==k]
plt.plot(x_k[:,0],x_k[:,1],'o',markerfacecolor=col,markeredgecolor="k",
markersize=14)
plt.title('data by make_moons()')
plt.show()

Python——sklearn提供的自带的数据集的更多相关文章

  1. sklearn提供的自带的数据集

    sklearn 的数据集有好多个种 自带的小数据集(packaged dataset):sklearn.datasets.load_<name> 可在线下载的数据集(Downloaded ...

  2. sklearn提供的自带数据集

    sklearn 的数据集有好多个种 自带的小数据集(packaged dataset):sklearn.datasets.load_<name> 可在线下载的数据集(Downloaded ...

  3. 『Sklearn』框架自带数据集接口

    自带数据集类型如下: # 自带小型数据集# sklearn.datasets.load_<name># 在线下载数据集# sklearn.datasets.fetch_<name&g ...

  4. Python: sklearn库——数据预处理

    Python: sklearn库 —— 数据预处理 数据集转换之预处理数据:      将输入的数据转化成机器学习算法可以使用的数据.包含特征提取和标准化.      原因:数据集的标准化(服从均值为 ...

  5. 用python+sklearn(机器学习)实现天气预报数据 模型和使用

    用python+sklearn机器学习实现天气预报 模型和使用 项目地址 系列教程 0.前言 1.建立模型 a.准备 引入所需要的头文件 选择模型 选择评估方法 获取数据集 b.建立模型 c.获取模型 ...

  6. 用python+sklearn(机器学习)实现天气预报数据 数据

    用python+sklearn机器学习实现天气预报 数据 项目地址 系列教程 勘误表 0.前言 1.爬虫 a.确认要被爬取的网页网址 b.爬虫部分 c.网页内容匹配取出部分 d.写入csv文件格式化 ...

  7. 用python+sklearn(机器学习)实现天气预报 准备

    用python+sklearn机器学习实现天气预报 准备 项目地址 系列教程 0.流程介绍 1. 环境搭建 a.python b.涉及到的机器学习相关库 sklearn panda seaborn j ...

  8. python+sklearn+kaggle机器学习

    python+sklearn+kaggle机器学习 系列教程 0.kaggle 1. 初级线性回归模型机器学习过程 a. 提取数据 b.数据预处理 c.训练模型 d.根据数据预测 e.验证 今天是10 ...

  9. C#调用Oracle带输出数据集的存储过程

    1.创建一个带输出数据集的Oracle存储过程 create or replace procedure PRO_test(in_top in number,cur_out out sys_refcur ...

随机推荐

  1. selenium之动作链

    概念:一组连续的行为动作 爬取网站:https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable 背景:把左边的方块横竖往下便宜 ...

  2. 微信小程序编译提示tabar.iconPath 文件不存在

    tabBar.list[0].iconPath 文件不存在 明明是按路径放了本地图片的,却依然显示路径不存在 需要把路径的图片转移到编译后的weapp文件中相同路径下的img文件中 本地正常路径 粘贴 ...

  3. 第一章、接口规范之Restful规范

    阅读目录 2.1 数据的安全保障 2.2 接口特征表现 2.3 多数据版本共存 2.4 数据即是资源 2.5 资源操作由请求方式决定 3.1 正常响应 3.2 重定向响应 3.3 客户端异常 3.4 ...

  4. RobHess的SIFT代码解析步骤四

    平台:win10 x64 +VS 2015专业版 +opencv-2.4.11 + gtk_-bundle_2.24.10_win32 主要参考:1.代码:RobHess的SIFT源码 2.书:王永明 ...

  5. Delphi 类的特性

  6. yum list报一些error的组件

    1 删除那些无效的参数配置,就不再报错了

  7. CP and Tucker Tensor Decomposition

    1.. 2..

  8. (转)为什么收到三个重复的ACK意味着发生拥塞?

    三次重复的ACK,可能是丢包引起的,丢包可能是网络拥塞造成的,也可能是信号失真造成的. 三次重复的ACK,也有可能是乱序引起的,而乱序和网络拥塞没有直接关系. 如果就写这两行,感觉什么都没写,接下来的 ...

  9. Redis入门(一)——安装

    1.下载地址,选择对应版本 https://github.com/dmajkic/redis/downloads 2.下载解压后,进入到文件夹,地址栏cmd,redis-server.exe redi ...

  10. CSS基础学习 18.CSS多列

    四种常见的浏览器内核: