
☞ ░ 前往老猿Python博客 ░ https://blog.youkuaiyun.com/LaoYuanPython
一、概述
Scikit-learn(sklearn)内置了多种经典数据集,适用于机器学习算法的快速验证、教学和实验。这些数据集通常是小规模的、结构化的,并已预处理好格式(如 NumPy 数组或 Pandas DataFrame)。
二、数据集列表
2.1、内置数据集
Scikit-learn 内置了多种经典数据集,涵盖分类、回归、聚类等任务,适用于算法测试和教学演示。
这些数据集主要分为两类:
- 小型练习数据集(Toy Datasets):内置在库中,无需联网即可使用,加载函数名为load开头
- 大型真实数据集(Real-World Datasets): 需要从网络下载,需要能访问外部网站才可以使用,加载函数名为fetch开头
另外还有一类合成数据集:合成数据集(Synthetic Dataset) 是通过人工生成的、非真实采集的数据集合,通常用于机器学习模型开发、测试和隐私保护等场景。其核心特点是 “人造但逼真”,既保留真实数据的统计特性,又避免真实数据的局限性,其加载函数为make开头。
以下是主要内置数据集及其特性的详细分类说明:
-
分类数据集
-
回归数据集
注:自 scikit-learn 1.2 起,load_boston() 因伦理问题被移除,改用 fetch_california_housing()。因为这个数据源自 1978 年波士顿地区的房产记录,反映了当时 种族隔离政策 ,房价数据集包含一个名为 B 的特征,该特征直接量化社区的黑人人口比例,并将高黑人比例与房价负相关性强绑定,强化了种族与房产价值的歧视性关联。 -
聚类数据集
-
图像数据集
-
其他实用数据集
2.2、自定义生成数据
Scikit-learn 提供数据生成工具,包括:
- 生成分类数据:make_classification
- 生成回归数据:make_regression
- 生成多标签数据:make_multilabel_classification
具体函数功能及使用方法在这里不展开介绍。
一个分类数据生成的案例代码:
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
from sklearn.preprocessing import StandardScaler
# 生成数据集
X, Y = make_classification(n_samples=1000, n_features=10, n_classes=2, random_state=42)
# 划分训练测试集
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X_train) # 拟合训练集并转换
X_test_scaled = scaler.transform(X_test) # 用相同参数转换测试集
# 训练模型
model = RandomForestClassifier()
model.fit(X_scaled, Y_train)
print("准确率:", model.score(X_test_scaled, Y_test))
三、数据集对象属性
所有加载后的数据集都是 Bunch 对象,包含如下属性:
- data:特征矩阵(NumPy数组或稀疏矩阵)
- target:标签数组
- feature_names:特征名称列表
- target_names:类别名称列表
- DESCR:数据集的完整描述
以手写数字识别的数据集加载并输出相关属性的案例代码:
from sklearn.datasets import load_digits
sep = '\n>>>>>>>>>>-------------------------------------输出数据分隔线----------------------------------------\n'
dg = load_digits()
print(f"{sep}data.shape={dg.data.shape}{sep}target={dg.target}{sep}feature_names={dg.feature_names}{sep}target_names={dg.target_names}{sep}DESCR={dg.DESCR}")
输出数据如下:
>>>>>>>>>>-------------------------------------输出数据分隔线----------------------------------------
data.shape=(1797, 64)
>>>>>>>>>>-------------------------------------输出数据分隔线----------------------------------------
target=[0 1 2 ... 8 9 8]
>>>>>>>>>>-------------------------------------输出数据分隔线----------------------------------------
feature_names=['pixel_0_0', 'pixel_0_1', 'pixel_0_2', 'pixel_0_3', 'pixel_0_4', 'pixel_0_5', 'pixel_0_6', 'pixel_0_7', 'pixel_1_0', 'pixel_1_1', 'pixel_1_2', 'pixel_1_3', 'pixel_1_4', 'pixel_1_5', 'pixel_1_6', 'pixel_1_7', 'pixel_2_0', 'pixel_2_1', 'pixel_2_2', 'pixel_2_3', 'pixel_2_4', 'pixel_2_5', 'pixel_2_6', 'pixel_2_7', 'pixel_3_0', 'pixel_3_1', 'pixel_3_2', 'pixel_3_3', 'pixel_3_4', 'pixel_3_5', 'pixel_3_6', 'pixel_3_7', 'pixel_4_0', 'pixel_4_1', 'pixel_4_2', 'pixel_4_3', 'pixel_4_4', 'pixel_4_5', 'pixel_4_6', 'pixel_4_7', 'pixel_5_0', 'pixel_5_1', 'pixel_5_2', 'pixel_5_3', 'pixel_5_4', 'pixel_5_5', 'pixel_5_6', 'pixel_5_7', 'pixel_6_0', 'pixel_6_1', 'pixel_6_2', 'pixel_6_3', 'pixel_6_4', 'pixel_6_5', 'pixel_6_6', 'pixel_6_7', 'pixel_7_0', 'pixel_7_1', 'pixel_7_2', 'pixel_7_3', 'pixel_7_4', 'pixel_7_5', 'pixel_7_6', 'pixel_7_7']
>>>>>>>>>>-------------------------------------输出数据分隔线----------------------------------------
target_names=[0 1 2 3 4 5 6 7 8 9]
>>>>>>>>>>-------------------------------------输出数据分隔线----------------------------------------
DESCR=.. _digits_dataset:
Optical recognition of handwritten digits dataset
--------------------------------------------------
**Data Set Characteristics:**
:Number of Instances: 1797
: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.
.. dropdown:: 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.
四、小结
数据是人工智能工作的燃料,Scikit-learn内置了多种经典数据集,适用于机器学习算法的快速验证、教学和实验。本文详细介绍了每种数据集的功能、加载方法和归属类别,有助于大家快速掌握Scikit-learn的数据集情况,并在进行机器学习时熟练使用。
更多人工智能知识学习过程中可能遇到的疑难问题及解决办法请关注专栏《零基础机器学习入门》及付费专栏《机器学习疑难问题集》后续的文章。
写博不易,敬请支持:
如果阅读本文于您有所获,敬请点赞、评论、收藏,谢谢大家的支持!
关于老猿的付费专栏
- 付费专栏《https://blog.youkuaiyun.com/laoyuanpython/category_9607725.html 使用PyQt开发图形界面Python应用》专门介绍基于Python的PyQt图形界面开发基础教程,对应文章目录为《 https://blog.youkuaiyun.com/LaoYuanPython/article/details/107580932 使用PyQt开发图形界面Python应用专栏目录》;
- 付费专栏《https://blog.youkuaiyun.com/laoyuanpython/category_10232926.html moviepy音视频开发专栏 )详细介绍moviepy音视频剪辑合成处理的类相关方法及使用相关方法进行相关剪辑合成场景的处理,对应文章目录为《https://blog.youkuaiyun.com/LaoYuanPython/article/details/107574583 moviepy音视频开发专栏文章目录》;
- 付费专栏《https://blog.youkuaiyun.com/laoyuanpython/category_10581071.html OpenCV-Python初学者疑难问题集》为《https://blog.youkuaiyun.com/laoyuanpython/category_9979286.html OpenCV-Python图形图像处理 》的伴生专栏,是笔者对OpenCV-Python图形图像处理学习中遇到的一些问题个人感悟的整合,相关资料基本上都是老猿反复研究的成果,有助于OpenCV-Python初学者比较深入地理解OpenCV,对应文章目录为《https://blog.youkuaiyun.com/LaoYuanPython/article/details/109713407 OpenCV-Python初学者疑难问题集专栏目录 》
- 付费专栏《https://blog.youkuaiyun.com/laoyuanpython/category_10762553.html Python爬虫入门 》站在一个互联网前端开发小白的角度介绍爬虫开发应知应会内容,包括爬虫入门的基础知识,以及爬取优快云文章信息、博主信息、给文章点赞、评论等实战内容。
前两个专栏都适合有一定Python基础但无相关知识的小白读者学习,第三个专栏请大家结合《https://blog.youkuaiyun.com/laoyuanpython/category_9979286.html OpenCV-Python图形图像处理 》的学习使用。
对于缺乏Python基础的同仁,可以通过老猿的免费专栏《https://blog.youkuaiyun.com/laoyuanpython/category_9831699.html 专栏:Python基础教程目录)从零开始学习Python。
如果有兴趣也愿意支持老猿的读者,欢迎购买付费专栏。