欢迎这位怪蜀黍来到《Python机器学习(二十七)Sklearn 数据集基本信息 - 大码王 - 博客园》

关闭页面特效

前面章节中,我们加载了SciKit-Learn自带的数据集digits,可以通过以下语句查看数据集中包含哪些主要内容:

digits.keys()

输出

dict_keys(['data', 'target', 'target_names', 'images', 'DESCR'])
  • data 样本数据
  • target 目标值
  • target_names 目标名称
  • images 图像格式(二维)的样本数据
  • DESCR 描述信息

查看数据集的描述:

print(digits.DESCR)

输出

复制代码
.. _digits_dataset:

Optical recognition of handwritten digits dataset
--------------------------------------------------

**Data Set Characteristics:**

    :Number of Instances: 5620
    :Number of Attributes: 64
    :Attribute Information: 8x8 image of integer pixels in the range 0..16.
    :Missing Attribute Values: None
    :Creator: E. Alpaydin (alpaydin '@' boun.edu.tr)
    :Date: July; 1998

This is a copy of the test set of the UCI ML hand-written digits datasets
https://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten+Digits

The data set contains images of hand-written digits: 10 classes where
each class refers to a digit.

Preprocessing programs made available by NIST were used to extract
normalized bitmaps of handwritten digits from a preprinted form. From a
total of 43 people, 30 contributed to the training set and different 13
to the test set. 32x32 bitmaps are divided into nonoverlapping blocks of
4x4 and the number of on pixels are counted in each block. This generates
an input matrix of 8x8 where each element is an integer in the range
0..16. This reduces dimensionality and gives invariance to small
distortions.

For info on NIST preprocessing routines, see M. D. Garris, J. L. Blue, G.
T. Candela, D. L. Dimmick, J. Geist, P. J. Grother, S. A. Janet, and C.
L. Wilson, NIST Form-Based Handprint Recognition System, NISTIR 5469,
1994.

.. topic:: References

  - C. Kaynak (1995) Methods of Combining Multiple Classifiers and Their
    Applications to Handwritten Digit Recognition, MSc Thesis, Institute of
    Graduate Studies in Science and Engineering, Bogazici University.
  - E. Alpaydin, C. Kaynak (1998) Cascading Classifiers, Kybernetika.
  - Ken Tang and Ponnuthurai N. Suganthan and Xi Yao and A. Kai Qin.
    Linear dimensionalityreduction using relevance weighted LDA. School of
    Electrical and Electronic Engineering Nanyang Technological University.
    2005.
  - Claudio Gentile. A New Approximate Maximal Margin Classification
    Algorithm. NIPS. 2000.
复制代码

这是一个手写数字的数据集。

类似地,你也可以查看其它内容:

复制代码
# 打印数据内容
print(digits.data)

# 打印目标值
print(digits.target)

# 打印目标名称(标签)
print(digits.target_names)
...
复制代码

注意:如果使用read_csv()导入数据集,数据集已经分割好,导入的数据集中可能没有描述字段,但是你可以使用head()tail()来检查数据。在这种情况下,最好仔细查看数据描述文件夹!

接下来,我们进一步了解数据集中的数据。

可以看到,数据集中的数据都是numpy数组的格式,可以查看这些数组的数据类型,形状,长度等信息。

复制代码
import numpy as np

# 打印data数组的形状
print(digits.data.shape) # 输出:(1797, 64)
# 打印data数组的类型
print(digits.data.dtype) # 输出:float64

# 打印target数组的形状
print(digits.target.shape) # 输出:(1797,)
# 打印target数组的类型
print(digits.target.dtype) # 输出:int32
# 打印target数组中包含的唯一值数量
print(len(np.unique(digits.target))) # 输出:10

# 打印target_names数组的形状
print(digits.target_names.shape) # 输出:(10,)
# 打印target_names数组的类型
print(digits.target_names.dtype) # 输出:int32

# 打印images数组的形状
print(digits.images.shape) # 输出:(1797, 8, 8)
# 打印images数组的类型
print(digits.images.dtype) # 输出:float64
复制代码

可以看出,digits.data中,有1797个样本,每个样本有64个特征值(实际上是像素灰度值)。

digits.target中,包含了上面样本数据对应的目标值(样本标签),同样有1797个目标值,但10个唯一值,即0-9。换句话说,所有1797个目标值都由0到9之间的数字组成,这意味着模型要识别的是从0到9的数字。

digits.target_names包含了样本标签的名称: 0~9。

最后,可以看到digits.images数组包含3个维度: 有1797个实例,大小为8×8像素。digits.images数据与digits.data内容应该相同,只是格式不同。可以通过以下方式验证两者内容是否相同:

print(np.all(digits.images.reshape((1797, 64)) == digits.data)) # 输出:true

digits.images改变形状为(1797, 64),与digits.data比较,两者相等。numpy方法all()可以检测所有数组元素的值是否为True。

 

 posted on   大码王  阅读(759)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具

成都

复制代码

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示