当前位置:首页 > 《数据仓库与数据挖掘技术》-基于weka实现KNN
计算机与数据科学学院 《数据仓库与数据挖掘》实验报告
public Point(long id,double x, double y) { this.x = x; this.y = y; this.id = id; } public Point(long id,double x, double y, String type) { } }
this.x = x; this.y = y; this.type = type; this.id = id; public int getX() { // TODO Auto-generated method stub return 0; } public int getY() { // TODO Auto-generated method stub return 0; } public long getId() { // TODO Auto-generated method stub return 0; } public String getType() { // TODO Auto-generated method stub return null; } //get、set方法省略 计算机与数据科学学院 《数据仓库与数据挖掘》实验报告
public class Distance {
// 已知点id
private long id;
// 未知点id
private long nid;
// 二者之间的距离
private double disatance;
public Distance(long id, long nid, {
this.id = id;
this.nid = nid;
this.disatance = disatance;
}
//get、set方法省略
double) disatance 计算机与数据科学学院 《数据仓库与数据挖掘》实验报告
public int getDisatance() { // TODO Auto-generated method stub return 0; } }
public class CompareClass implements Comparator
public int compare(Distance d1, Distance d2) {
return d1.getDisatance()>d2.getDisatance()?20 : -1;
} } // 一、输入所有已知点
List
Point x = new Point(5, 1.2, 1.2);
// 三、计算所有已知点到未知点的欧式距离,并根据距离对所有已知点排序
CompareClass compare = new CompareClass(); Set
for ( Point point : dataList ) { distanceSet.add(new Distance(point.getId(), x.getId(), oudistance(point, x))); }
计算机与数据科学学院 《数据仓库与数据挖掘》实验报告
// 四、选取最近的k个点 double k = 5; /**
* 五、计算k个点所在分类出现的频率 */
// 1、计算每个分类所包含的点的个数 List
getNumberOfType(distanceList, dataList, k); // 2、计算频率
Map
x.setType(maxP(p)); System.out.println(\未知点的类型为:\+x.getType()); }
// 欧式距离计算
public static double oudistance (Point point1, Point point2) {
double temp = Math.pow(point1.getX() - point2.getX(), 2)
+ Math.pow(point1.getY() - point2.getY(), 2); return Math.sqrt(temp); }
// 找出最大频率
public static String maxP(Map
for (Map.Entry
if (entry.getValue() > value) { key = entry.getKey();
共分享92篇相关文档