Многие контролируемые Scikit-Learn классификаторы имеют возможность predict
с новыми данными.
Например, посмотрите документацию для K ближайших соседей:
knn.predict(new_data) # will predict classes for new data
ОБНОВЛЕНИЕ
Когда вы прогнозируетеклассы, основанные на новых данных, вам нужно только указать новый X
. Вот более длинная версия кода, чтобы описать лучше:
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
# make some example data
X, y = make_blobs(n_samples = 100, n_features = 2,
centers = 2, random_state = 123)
# fit supervised KNN classifier
knn = KNeighborsClassifier()
knn.fit(X, y)
# create 50 new data points
# with the same number of features as the training set
new_data = np.random.randn(50, 2)
# predict new labels
new_labels = knn.predict(new_data)
# plot training clusters
plt.plot(X[y== 1, 0],
X[y==1,1],
"C1o", label = "training cluster 1")
plt.plot(X[y== 0, 0],
X[y==0,1],
"C0o", label = "training custer 2")
# plot predictions on new data
plt.plot(new_data[new_labels== 1, 0],
new_data[new_labels==1,1],
"ro", label = "new data assigned to cluster 1")
plt.plot(new_data[new_labels== 0, 0],
new_data[new_labels==0,1],
"bo", label = "new data assigned to cluster 2")
plt.legend()