数据集处理

train_test_split Link to heading

train_test_split 是 scikit-learn 库中的一个函数,用于将数据集分割为训练集和测试集。

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
  • X 是特征矩阵,通常是一个二维数组。
  • y 是目标变量,通常是一个一维数组。
  • test_size 是测试集占总数据集的比例。在上面的例子中,测试集占总数据集的20%。
  • random_state 是一个随机数种子,用于确保每次分割结果的一致性。 train_test_split 函数会返回四个值:
  • X_train:训练集的特征。
  • X_test:测试集的特征。
  • y_train:训练集的目标变量。
  • y_test:测试集的目标变量。

fetch_openml Link to heading

fetch_openml 用于下载Openml中的开放数据集。自动下载数据集到C:\Users\你的用户名\scikit_learn_data 中,可以通过修改SCIKIT_LEARN_DATA环境变量来修改下载路径文件夹。

from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784')
X = mnist.data
y = mnist.target