I have two datasets A and B. What I would like to do is for each observation in A, I would like to find 5 observations from B that are closest and match to A.

How should I start?

Thank you for your help!

  • $\begingroup$ Add more information. What is the data format of observation? Are they simple floating point numbers or strings or more complex data types? The approach will vary based on data. Try adding some (anonymized) sample data if you can. $\endgroup$
    – hssay
    Jan 8, 2018 at 5:36
  • $\begingroup$ Hi, All variables in the datasets are simple floating and categorical. No complex data types. Thanks for your help! $\endgroup$ Jan 8, 2018 at 6:24

1 Answer 1


Look at "unsupervised nearest neighbor" algorithm. This algorithm needs records to be first expressed as vectors so that "distance" between two point so that it makes sense to talk about distance between two points. For each point in data A, you can look for K nearest neighbors from data B, after expressing all observations in a common vector space.

You will have to handle categorical columns correctly (say using one-hot encoding), as there's no concept of (direct) distance between categorical data.

Python scikit-learn library has a good implementation of this algorithm. Reading the API documentation is a good place to start.

  • $\begingroup$ do you know any other unsurpervised way to do this? $\endgroup$
    – Maths12
    May 12, 2022 at 16:15

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