使用sklearn的Python MNIST数据集,选择特定数字

时间:2019-11-16 15:48:40

标签: python scikit-learn mnist

我正在使用Sklearn在MNIST数据集上训练很少的模型,如何只使用MNIST数据集中的两位数字4和9(两个类)来训练线性模型?

  • 如何挑选我的X_test,X_train, y_test,y_train

1 个答案:

答案 0 :(得分:2)

因此,您只想使用数字4和9的图像。

您需要像X[np.logical_or(y == 4, y == 9)]那样建立索引:

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_digits

digits = load_digits()

X = digits.data
y = digits.target

#Select only the digit 4 and 9 images
X = X[np.logical_or(y == 4, y == 9)]
y = y[np.logical_or(y == 4, y == 9)]

# verify selection
np.unique(y)
#array([4, 9])

# Now split them
X_train, X_test, y_train, y_test = train_test_split(
    X, y, train_size=200, test_size=100)

仅使用数字4:

X = digits.data
y = digits.target

#Select only the digit 4 and 9 images
X = X[y == 4]
y = y[y == 4]