from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784', version=1, as_frame=False)
mnist.keys()
X, y = mnist["data"], mnist["target"]
y = y.astype(np.uint8)
X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]
y_train_5 = (y_train == 5)
y_test_5 = (y_test == 5)
from sklearn.neighbors import KNeighborsClassifier
neigh = KNeighborsClassifier(n_neighbors=4, weights = 'uniform')
neigh.fit(X_train, y_train_5)
from sklearn.model_selection import cross_val_predict
y_test_pred = cross_val_predict(neigh, X_test, y_test_5, cv=3)
from sklearn.metrics import precision_score
precision_score(y_test_5, y_test_pred)