Java 寻找最近邻K-NN
嘿,伙计们,我的代码创建了10个点,为每个点指定了一个介于1和500之间的距离,然后为每个点找到最近的两个邻居。到目前为止,我有两个班,一个是负责数学和分配邻居的PointD班,另一个是负责运行程序和计算距离的主班: 点类:Java 寻找最近邻K-NN,java,arrays,algorithm,Java,Arrays,Algorithm,嘿,伙计们,我的代码创建了10个点,为每个点指定了一个介于1和500之间的距离,然后为每个点找到最近的两个邻居。到目前为止,我有两个班,一个是负责数学和分配邻居的PointD班,另一个是负责运行程序和计算距离的主班: 点类: package points; /** * * @Ashley Caldwell */ public class PointD { private final int x; private final String name; //Cr
package points;
/**
*
* @Ashley Caldwell
*/
public class PointD {
private final int x;
private final String name;
//Creates an int named x for the math and a string called name for the point
public PointD(int x, String name) {
this.x = x;
this.name = name;
}//Used to create the Point
public int distanceTo(PointD other) {
return Math.abs(other.x - this.x);
}//Math used to work out the distance to the next point
/* extensions for your 2 neighbors */
private PointD neighbor1 = null;
private PointD neighbor2 = null;
public String toString() {
return name +" "+ "at " + x + ", "
+ "with neighbors: " + (neighbor1 != null? neighbor1.name +
" Distance(" + distanceTo(neighbor1) + "), " : "")
+ (neighbor2 != null? neighbor2.name +
" Distance(" + distanceTo(neighbor2) + ")" : "");
}// sets the values to string for both the points
public void setNeighbors(PointD p1, PointD p2) {
neighbor1 = p1;
neighbor2 = p2;
//Assigns the neighbours to the point
}
}
主要类别:
package points;
/**
*
* @Ashley Caldwell
*/
import java.util.Random;
public class Main {
// better into a seperate MathLibrary
public static int distance(PointD p1, PointD p2) {
return p1.distanceTo(p2);
}
public static void main(String[] args) {
Random rng = new Random();
//used to create distances for points
int numberOfPoints = 10+1;
// generate points
PointD[] points = new PointD[numberOfPoints];
for(int i = 0; i < numberOfPoints; ++i) {
points[i] = new PointD(rng.nextInt(500) + 1, "Points " + i);
//Loop assigns value between 1 and 500 to each point
}
for(int i = 0; i < numberOfPoints; ++i) {
PointD currentPoint = points[i];
PointD closestPoint = null;
PointD secondClosestPoint = null;
for(int j = 0; j < numberOfPoints; ++j) {
if(i == j) continue;
if(closestPoint == null || currentPoint.distanceTo(points[j]) < currentPoint.distanceTo(closestPoint)) {
secondClosestPoint = closestPoint;
closestPoint = points[j];
} else if(secondClosestPoint == null || currentPoint.distanceTo(points[j]) < currentPoint.distanceTo(secondClosestPoint)) {
secondClosestPoint = points[j];
}//Loop used to calculate the distance between each point by using current distance of point
}
currentPoint.setNeighbors(closestPoint, secondClosestPoint);
}
for(int i = 0; i < numberOfPoints; ++i)
{
System.out.println(points[i]);
}
}// Prints out the points and the strings
}
我遇到的主要问题是,有些点使用同一点超过两次,因此有些点没有连接,我需要尝试对其进行排序,以便它只找到两个最接近的点,而这两个点尚未使用,我遇到的另一个问题是,我希望我的点0始终设置为距离0,但不知道如何将此元素的值设置为零,因为它引用另一个类,随机设置距离不是必须的,如果我更容易更改为设置值,那么我会这样做,但我只是想知道是否有人可以提供任何建议?我根本不理解以下内容,请重新表述:“某些点使用相同点超过两次,因此某些点没有连接”抱歉,我没有说得更清楚,有些点两次找到同一点,因此,例如,在输出点8中,点2、4和10最接近,这意味着有些点与任何其他点都不相邻,我试图创建一个点网络,所有点都相互连接,但每个点只有两个连接,就像一个树形图,希望这有助于从输出中明显看出,每个点正好有两个邻居,因此没有任何点不连接。例如,假设您在各自的距离1、2、3和10处有4个点A、B、C、D。你的算法将A连接到B,C;B至A,C;C至A、B;从D到C,B,你想怎么做A,B,在这种情况下,C和D是连接的?从点0开始,我希望连接到两个点,然后每个点都有两个自己的点,每次每个点只有两个点,因为我试图找到两个最近的邻居,但是在我的代码中,每个点可以有任意多的邻居,这取决于距离所以我想限制它,使点不能被使用超过两次
Points 0 at 80, with neighbors: Points 6 Distance(39), Points 3 Distance(40)
Points 1 at 212, with neighbors: Points 7 Distance(29), Points 9 Distance(64)
Points 2 at 308, with neighbors: Points 5 Distance(12), Points 8 Distance(19)
Points 3 at 40, with neighbors: Points 6 Distance(1), Points 0 Distance(40)
Points 4 at 489, with neighbors: Points 10 Distance(35), Points 8 Distance(162)
Points 5 at 296, with neighbors: Points 2 Distance(12), Points 9 Distance(20)
Points 6 at 41, with neighbors: Points 3 Distance(1), Points 0 Distance(39)
Points 7 at 241, with neighbors: Points 1 Distance(29), Points 9 Distance(35)
Points 8 at 327, with neighbors: Points 2 Distance(19), Points 5 Distance(31)
Points 9 at 276, with neighbors: Points 5 Distance(20), Points 2 Distance(32)
Points 10 at 454, with neighbors: Points 4 Distance(35), Points 8 Distance(127)