1
$\begingroup$

While I was studying Scikit-learn's kNN algorithm, I realized that if I use sklearn.model_selection.train_test_split, the provided data gets automatically split into the train data and the test data set, according to the proportions provided as parameters.

Then based on the train data, the algorithm looks at the k-nearest neighbor points closest to the test data points to determine whether the test data points belong to a certain criteria or not.

I was wondering whether there was a way to predict the criteria NOT for the test data sets, which were already a part of the provided data set, but brand new data that were not provided during the whole process.

Is there a way to do that using sci-kit learn?

$\endgroup$

2 Answers 2

1
$\begingroup$

KNN is not fitted to "the k-nearest neighbor points closest to the test data points". You specify the fit option, like:

neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(X, y)

Usually this will be xtrain, ytrain, while you test the model performance using "new" (unseen) data and compare the true targets to the prediction.

neigh.predict(xtest)

or

neigh.predict_proba(xtest)

See docs: https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html

$\endgroup$
1
  • $\begingroup$ Thanks @Peter. Your answer clarified things quite a lot. I think I know what to do now..! $\endgroup$
    – user9487
    Commented May 1, 2021 at 15:28
1
$\begingroup$

After your initial validation of the model using train-test split, if you are satisfied with the performance, You can create a final model by training on the entire dataset. That way you put to use all available labeled for running inferences on brand new data.

You would simply perform a:

model = KNeighborsClassifier()

model.fit(X, y)

Where X, y represent your entire training data.

$\endgroup$
1
  • $\begingroup$ Thank you @Jayyaram Iyer. I tried to do that--but I think the sklearn.model_selection.train_test_split didn't allow me to set 0 as the parameter value in train, test = train_test_split(df, test_size=0). Any suggestions on how to fix that..? $\endgroup$
    – user9487
    Commented May 1, 2021 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.