01-23-2017 10:05 PM
I have a dataset with around 100000 records. I want to know only those three records that have shortest distance with a particular record. If I run the distance procedure then the program generates the output of matrix of 100000 variables giving distance of a variable with all other variables. It is actually not required and unecessarily it eats up my system time and sometimes evenhangs my system. If there is any other procedure that gives me the variables with just the shortest distance, please share.
Thanks in advance.....
01-24-2017 12:09 AM
01-24-2017 12:42 AM
Not sure but one way to get that is using k means clustering. Make a reference value and use that in proc fastclus in instat option to get the distances with regard to reference value. Then sort the output data set to flag the top 3 closest values.
01-24-2017 05:43 AM
1 5 6 7
2 1 2 3
3 2 1 6
4 5 1 6
In this sample data set, you say 5, 6, 7 are the records that are closer to Record 1. You show a small example as how you got 5, 6, and 7?
What variable(s) (not shown here) are used for this decision?
Provide an example and illustrate how you find them. Finding algorithm to do fast is simpler once your your example is clear.
01-24-2017 03:32 PM
It was a logical question. You can't find the nearest neighbour without calculating all values first. Perhaps the KNN process in SPSS precalculates the distance and then obtains the nearest neighbour when you request it. This means it isn't calculating the distance matrix every time.
You need to provide more details regarding what is the issue in your current process. Ideally, you can provide sample input, your code and a message with which step is inefficient. Then we'll be able to suggest alternatives for your.
PROC DISTANCE is relatively fast, but if you don't need it run each time and can cache the results somehow that's a better process.
01-24-2017 08:25 AM
I assume you want the Euclidean distance.
If you know the "particular record", then this is an easy problem that you can complete in the DATA step.
1. Put the particular record first in the data set (or hard-code it into an array).
2. Use the EUCLID function to compute the Euclidean distance between the particular record and the others.
3. Sort the data by distance and use the first 3 records.
For example, the following DATA step computes the Euclidean distances between the numerical measurements for the first observation ("Alfred") and the other observations. The result shows that Mary, William, and Janet are the students whose numerical measurements are closest to Alfred, as measured by the Euclidean distance function:
data Dist(drop=i); array refPt  _temporary_; /* automatically RETAINed */ array diff  _temporary_; set sashelp.class; array Pt [*] Height Weight Age; /* initialize refPt, or use array refPt (val1 val2 val3); */ if _N_ = 1 then do i = 1 to dim(Pt); refPt[i] = Pt[i]; end; /* compute difference between Pt and refPt */ do i = 1 to dim(pt); diff[i] = refPt[i] - Pt[i]; end; dist = euclid(of diff[*]); if _N_>1; run; proc sort data=Dist; by dist; run; proc print data=Dist(obs=3); run;
You can generalize this problem. Instead of finding the points nearest to a single reference point, you can find the points in one group that are nearest to points in another group. See the article "Distances between observations in two groups."
01-24-2017 10:36 PM
_n_ is an automatic variable that counts the number of boundary steps. It is typically used as a pseudo row counter.
01-25-2017 12:07 AM
01-25-2017 05:47 AM
Please clarify two issue:
1. How many variables in this problem?
2. Do you have ONE reference point, and you want the three closest from among 1M observations? Or do you have 1M points and for each of those you want to find the three nearest neighbors? The first case requires computing 1M distances, The second case requires computing 1E12 distances.
01-25-2017 08:21 PM
The shape of my dataset is as follows:
I want KNN (K=3) for each observation from A1 to A1000000. Actually I had 200M observations, first I made clusters using fastclus and thought of using distance procedure for calculating distance and sort and take the top 3 records. Some clusters have less records but some still have high number of records. I was able to use distance procedure till 100,000 records but it is not responding above this limit. When I tried KNN Node in SPSS it responded to higher number of records too, so I thought of customising program for distance instead of using Distance proc.
Please advise me something for this situation.