sklearn.datasets 中主要包含了4类数据集。
scikit-learn 内置的一些小型标准数据集,不需要从某个外部网站下载任何文件,用datasets.load_xx()加载。比如:鸾尾花、波士顿房价等数据集。
Toy datasets 通过 sklearn.datasets.load_
加载对应的数据集。
这些数据集通常需要通过sklearn.datasets.fetch_
函数从网络上下载,它们是近年来真实收集的数据,适用于更复杂的机器学习任务。例如,新闻组(20 Newsgroups)数据集,这是一个用于文本分类的大型数据集。
sklearn.datasets
还提供了一系列函数来生成人工数据集,如make_classification
、make_regression
等。这些函数可以根据用户指定的参数生成用于分类、回归等任务的数据集。
sklearn.datasets
还提供了一些加载其它数据集的方法,例如:
sklearn.datasets.fetch_openml()
函数,可以从OpenML下载各种数据集。建议除了玩具数据集和生成数据集以外,都在网上下载后用pandas导入。
例如,导入iris文件:
import pandas as pd import seaborn as sns # 基于matplotlib和pandas的画图库 import matplotlib.pyplot as plt data = pd.read_csv("/path/to/iris.csv", encoding='gbk') # 我把数据集列名改成了中文 所以用gbk解码 sns.relplot(x='petal_width', y='sepal_length', hue="species", data=data) # seaborn库这里不做过多介绍 plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一(替换sans-serif字体) # plt.rcParams['axes.unicode_minus'] = False # 步骤二(解决坐标轴负数的负号显示问题) plt.show()
值得注意的是,sklearn.datasets
中的数据集主要是为了方便教学和入门学习而提供的。在实际应用中,可能需要使用更大规模、更复杂的数据集来训练模型。此外,随着时间的推移,sklearn
库可能会更新和添加新的数据集,因此建议查阅最新的官方文档以获取最准确的信息。
both loaders and fetchers functions return a Bunch
object holding at least two items: an array of shape n_samples
* n_features
with key data
(except for 20newsgroups) and a numpy array of length n_samples
, containing the target values, with key target
. The datasets also contain a full description in their DESCR
attribute and some contain feature_names
and target_names
.
例如:
from sklearn.datasets import load_iris iris = load_iris() print(iris.keys()) # 查看键(属性) dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names', 'filename', 'data_module']) print(iris.data[:5]) # 获取特征值 print(iris.target[:5]) # 获取目标值 print(iris.DESCR) # 获取数据集描述 print(iris.data.shape,iris.target.shape) # 查看数据的形状 (150, 4) (150,) print(iris.feature_names) # 查看有哪些特征 这里共4种:['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'] print(iris.target_names) # target name:['setosa' 'versicolor' 'virginica']
It’s also possible for almost all of these function to constrain the output to be a tuple containing only the data and the target, by setting the return_X_y
parameter to True
.
例如:
from sklearn.datasets import load_iris data, target = load_iris(return_X_y=True)
The dataset generation functions return a tuple (X, y)
consisting of a n_samples
* n_features
numpy array X
and an array of length n_samples
containing the targets y
.