实验中使用的数据依然是UCI上的Iris,实验中分别有样本数据和测试使用的数据,分别如下:
样本数据是分别算则iris中三类数据各30个:
文章图片
文章图片
5.1,3.5,1.4,0.2,Iris-setosa 4.9,3.0,1.4,0.2,Iris-setosa 4.7,3.2,1.3,0.2,Iris-setosa 4.6,3.1,1.5,0.2,Iris-setosa 5.0,3.6,1.4,0.2,Iris-setosa 5.4,3.9,1.7,0.4,Iris-setosa 4.6,3.4,1.4,0.3,Iris-setosa 5.0,3.4,1.5,0.2,Iris-setosa 4.4,2.9,1.4,0.2,Iris-setosa 4.9,3.1,1.5,0.1,Iris-setosa 5.4,3.7,1.5,0.2,Iris-setosa 4.8,3.4,1.6,0.2,Iris-setosa 4.8,3.0,1.4,0.1,Iris-setosa 4.3,3.0,1.1,0.1,Iris-setosa 5.8,4.0,1.2,0.2,Iris-setosa 5.7,4.4,1.5,0.4,Iris-setosa 5.4,3.9,1.3,0.4,Iris-setosa 5.1,3.5,1.4,0.3,Iris-setosa 5.7,3.8,1.7,0.3,Iris-setosa 5.1,3.8,1.5,0.3,Iris-setosa 5.4,3.4,1.7,0.2,Iris-setosa 5.1,3.7,1.5,0.4,Iris-setosa 4.6,3.6,1.0,0.2,Iris-setosa 5.1,3.3,1.7,0.5,Iris-setosa 4.8,3.4,1.9,0.2,Iris-setosa 5.0,3.0,1.6,0.2,Iris-setosa 5.0,3.4,1.6,0.4,Iris-setosa 5.2,3.5,1.5,0.2,Iris-setosa 5.2,3.4,1.4,0.2,Iris-setosa 4.7,3.2,1.6,0.2,Iris-setosa 7.0,3.2,4.7,1.4,Iris-versicolor 6.4,3.2,4.5,1.5,Iris-versicolor 6.9,3.1,4.9,1.5,Iris-versicolor 5.5,2.3,4.0,1.3,Iris-versicolor 6.5,2.8,4.6,1.5,Iris-versicolor 5.7,2.8,4.5,1.3,Iris-versicolor 6.3,3.3,4.7,1.6,Iris-versicolor 4.9,2.4,3.3,1.0,Iris-versicolor 6.6,2.9,4.6,1.3,Iris-versicolor 5.2,2.7,3.9,1.4,Iris-versicolor 5.0,2.0,3.5,1.0,Iris-versicolor 5.9,3.0,4.2,1.5,Iris-versicolor 6.0,2.2,4.0,1.0,Iris-versicolor 6.1,2.9,4.7,1.4,Iris-versicolor 5.6,2.9,3.6,1.3,Iris-versicolor 6.7,3.1,4.4,1.4,Iris-versicolor 5.6,3.0,4.5,1.5,Iris-versicolor 5.8,2.7,4.1,1.0,Iris-versicolor 6.2,2.2,4.5,1.5,Iris-versicolor 5.6,2.5,3.9,1.1,Iris-versicolor 5.9,3.2,4.8,1.8,Iris-versicolor 6.1,2.8,4.0,1.3,Iris-versicolor 6.3,2.5,4.9,1.5,Iris-versicolor 6.1,2.8,4.7,1.2,Iris-versicolor 6.4,2.9,4.3,1.3,Iris-versicolor 6.6,3.0,4.4,1.4,Iris-versicolor 6.8,2.8,4.8,1.4,Iris-versicolor 6.7,3.0,5.0,1.7,Iris-versicolor 6.0,2.9,4.5,1.5,Iris-versicolor 5.7,2.6,3.5,1.0,Iris-versicolor 6.3,3.3,6.0,2.5,Iris-virginica 5.8,2.7,5.1,1.9,Iris-virginica 7.1,3.0,5.9,2.1,Iris-virginica 6.3,2.9,5.6,1.8,Iris-virginica 6.5,3.0,5.8,2.2,Iris-virginica 7.6,3.0,6.6,2.1,Iris-virginica 4.9,2.5,4.5,1.7,Iris-virginica 7.3,2.9,6.3,1.8,Iris-virginica 6.7,2.5,5.8,1.8,Iris-virginica 7.2,3.6,6.1,2.5,Iris-virginica 6.5,3.2,5.1,2.0,Iris-virginica 6.4,2.7,5.3,1.9,Iris-virginica 6.8,3.0,5.5,2.1,Iris-virginica 5.7,2.5,5.0,2.0,Iris-virginica 5.8,2.8,5.1,2.4,Iris-virginica 6.4,3.2,5.3,2.3,Iris-virginica 6.5,3.0,5.5,1.8,Iris-virginica 7.7,3.8,6.7,2.2,Iris-virginica 7.7,2.6,6.9,2.3,Iris-virginica 6.0,2.2,5.0,1.5,Iris-virginica 6.9,3.2,5.7,2.3,Iris-virginica 5.6,2.8,4.9,2.0,Iris-virginica 7.7,2.8,6.7,2.0,Iris-virginica 6.3,2.7,4.9,1.8,Iris-virginica 6.7,3.3,5.7,2.1,Iris-virginica 7.2,3.2,6.0,1.8,Iris-virginica 6.2,2.8,4.8,1.8,Iris-virginica 6.1,3.0,4.9,1.8,Iris-virginica 6.4,2.8,5.6,2.1,Iris-virginica 7.2,3.0,5.8,1.6,Iris-virginica
View Code 测试数据为剩余的60条数据:
文章图片
文章图片
4.8,3.1,1.6,0.2,Iris-setosa 5.4,3.4,1.5,0.4,Iris-setosa 5.2,4.1,1.5,0.1,Iris-setosa 5.5,4.2,1.4,0.2,Iris-setosa 4.9,3.1,1.5,0.1,Iris-setosa 5.0,3.2,1.2,0.2,Iris-setosa 5.5,3.5,1.3,0.2,Iris-setosa 4.9,3.1,1.5,0.1,Iris-setosa 4.4,3.0,1.3,0.2,Iris-setosa 5.1,3.4,1.5,0.2,Iris-setosa 5.0,3.5,1.3,0.3,Iris-setosa 4.5,2.3,1.3,0.3,Iris-setosa 4.4,3.2,1.3,0.2,Iris-setosa 5.0,3.5,1.6,0.6,Iris-setosa 5.1,3.8,1.9,0.4,Iris-setosa 4.8,3.0,1.4,0.3,Iris-setosa 5.1,3.8,1.6,0.2,Iris-setosa 4.6,3.2,1.4,0.2,Iris-setosa 5.3,3.7,1.5,0.2,Iris-setosa 5.0,3.3,1.4,0.2,Iris-setosa 5.5,2.4,3.8,1.1,Iris-versicolor 5.5,2.4,3.7,1.0,Iris-versicolor 5.8,2.7,3.9,1.2,Iris-versicolor 6.0,2.7,5.1,1.6,Iris-versicolor 5.4,3.0,4.5,1.5,Iris-versicolor 6.0,3.4,4.5,1.6,Iris-versicolor 6.7,3.1,4.7,1.5,Iris-versicolor 6.3,2.3,4.4,1.3,Iris-versicolor 5.6,3.0,4.1,1.3,Iris-versicolor 5.5,2.5,4.0,1.3,Iris-versicolor 5.5,2.6,4.4,1.2,Iris-versicolor 6.1,3.0,4.6,1.4,Iris-versicolor 5.8,2.6,4.0,1.2,Iris-versicolor 5.0,2.3,3.3,1.0,Iris-versicolor 5.6,2.7,4.2,1.3,Iris-versicolor 5.7,3.0,4.2,1.2,Iris-versicolor 5.7,2.9,4.2,1.3,Iris-versicolor 6.2,2.9,4.3,1.3,Iris-versicolor 5.1,2.5,3.0,1.1,Iris-versicolor 5.7,2.8,4.1,1.3,Iris-versicolor 7.4,2.8,6.1,1.9,Iris-virginica 7.9,3.8,6.4,2.0,Iris-virginica 6.4,2.8,5.6,2.2,Iris-virginica 6.3,2.8,5.1,1.5,Iris-virginica 6.1,2.6,5.6,1.4,Iris-virginica 7.7,3.0,6.1,2.3,Iris-virginica 6.3,3.4,5.6,2.4,Iris-virginica 6.4,3.1,5.5,1.8,Iris-virginica 6.0,3.0,4.8,1.8,Iris-virginica 6.9,3.1,5.4,2.1,Iris-virginica 6.7,3.1,5.6,2.4,Iris-virginica 6.9,3.1,5.1,2.3,Iris-virginica 5.8,2.7,5.1,1.9,Iris-virginica 6.8,3.2,5.9,2.3,Iris-virginica 6.7,3.3,5.7,2.5,Iris-virginica 6.7,3.0,5.2,2.3,Iris-virginica 6.3,2.5,5.0,1.9,Iris-virginica 6.5,3.0,5.2,2.0,Iris-virginica 6.2,3.4,5.4,2.3,Iris-virginica 5.9,3.0,5.1,1.8,Iris-virginica
View Code 算法代码如下:
1 package neugle.knn; 2 3 import java.io.BufferedReader; 4 import java.io.FileReader; 5 import java.util.ArrayList; 6 import java.util.HashMap; 7 import java.util.LinkedHashMap; 8 import java.util.List; 9 import java.util.Map.Entry; 10 import java.util.Set; 11 12 public class KNN { 13private ListirisList = new ArrayList (); 14 15class Iris { 16public double Sep_len; 17public double Sep_wid; 18public double Pet_len; 19public double Pet_wid; 20public String Iris_type; 21} 22 23// 读取数据 24public List ReadFile(String filePath) { 25FileReader fr = null; 26BufferedReader br = null; 27List irisList = new ArrayList (); 28try { 29fr = new FileReader(filePath); 30br = new BufferedReader(fr); 31String line = null; 32while ((line = br.readLine()) != null) { 33Iris iris = new Iris(); 34String[] agrs = line.split(","); 35iris.Sep_len = Double.parseDouble(agrs[0]); 36iris.Sep_wid = Double.parseDouble(agrs[1]); 37iris.Pet_len = Double.parseDouble(agrs[2]); 38iris.Pet_wid = Double.parseDouble(agrs[3]); 39iris.Iris_type = agrs[4]; 40irisList.add(iris); 41} 42} catch (Exception e) { 43e.printStackTrace(); 44} finally { 45try { 46br.close(); 47} catch (Exception e) { 48e.printStackTrace(); 49} 50} 51return irisList; 52} 53 54// 计算测试数据和样本点中每个点的距离 55public LinkedHashMap GetDistance(Iris iris) { 56LinkedHashMap irisMap = new LinkedHashMap (); 57for (int i = 0; i < this.irisList.size(); i++) { 58double d = this.DistanceCalculate(iris, this.irisList.get(i)); 59irisMap.put(i, d); 60} 61return irisMap; 62} 63 64private double DistanceCalculate(Iris iris1, Iris iris2) { 65double sum = Math.sqrt(Math.pow((iris1.Sep_len - iris2.Sep_len), 2) 66+ Math.pow((iris1.Sep_wid - iris2.Sep_wid), 2) 67+ Math.pow((iris1.Pet_len - iris2.Pet_len), 2) 68+ Math.pow((iris1.Pet_wid - iris2.Pet_wid), 2)); 69return sum; 70} 71 72// 找出前k个数据 73public List FindKData(int k, LinkedHashMap irisMap) { 74List iList = new ArrayList (); 75List rList = new ArrayList (); 76Set > set = irisMap.entrySet(); 77for (int i = 0; i < k; i++) { 78int key = 0; 79double value = https://www.it610.com/article/0; 80boolean flag = true; 81for (Entry e : set) { 82if (flag == true) { 83key = e.getKey(); 84value = https://www.it610.com/article/e.getValue(); 85flag = false; 86continue; 87} 88if (e.getValue() < value) { 89key = e.getKey(); 90value = e.getValue(); 91} 92} 93iList.add(key); 94irisMap.remove(key); 95} 96 97for (int i = 0; i < iList.size(); i++) { 98rList.add(this.irisList.get(iList.get(i))); 99} 100return rList; 101} 102 103// 找出该测试数据应属于哪一类 104public String FindClass(List iList) { 105HashMap map = new HashMap(); 106for (int i = 0; i < iList.size(); i++) { 107String s = iList.get(i).Iris_type; 108if (map.containsKey(s)) { 109map.put(s, map.get(s) + 1); 110} else { 111map.put(s, 1); 112} 113} 114 115String key = null; 116int value = https://www.it610.com/article/0; 117for (Entry e : map.entrySet()) { 118if (e.getValue()> value) { 119value = https://www.it610.com/article/e.getValue(); 120key = e.getKey(); 121} 122} 123return key; 124} 125 126// 操控方法 127public void Calc(String filePath1, String filePath2, int k) { 128this.irisList = this.ReadFile(filePath1); 129List fList = this.ReadFile(filePath2); 130System.out.println("测试数据展示:"); 131System.out.println("-----------------------"); 132for (int i = 0; i < fList.size(); i++) { 133Iris iris = fList.get(i); 134System.out.println(iris.Pet_len + " " + iris.Pet_wid + " " 135+ iris.Sep_len + " " + iris.Sep_wid + " " + iris.Iris_type); 136} 137System.out.println("-----------------------"); 138System.out.println("测试结果为:"); 139System.out.println("-----------------------"); 140for (int i = 0; i < fList.size(); i++) { 141Iris iris = fList.get(i); 142LinkedHashMap dMap = this.GetDistance(iris); 143List iList = this.FindKData(k, dMap); 144String type = this.FindClass(iList); 145System.out.println(iris.Pet_len + " " + iris.Pet_wid + " " 146+ iris.Sep_len + " " + iris.Sep_wid + " " + type); 147} 148System.out.println("-----------------------"); 149} 150 151public static void main(String[] args) { 152KNN knn = new KNN(); 153String filePath1 = "D:\\data\\KNN\\iris.data"; // 样本数据位置 154String filePath2 = "D:\\data\\KNN\\firis.data"; // 测试数据位置 155int k = 3; 156knn.Calc(filePath1, filePath2, k); 157} 158 }
实验结果如下:
文章图片
文章图片
1.6 0.2 4.8 3.1 Iris-setosa 1.5 0.4 5.4 3.4 Iris-setosa 1.5 0.1 5.2 4.1 Iris-setosa 1.4 0.2 5.5 4.2 Iris-setosa 1.5 0.1 4.9 3.1 Iris-setosa 1.2 0.2 5.0 3.2 Iris-setosa 1.3 0.2 5.5 3.5 Iris-setosa 1.5 0.1 4.9 3.1 Iris-setosa 1.3 0.2 4.4 3.0 Iris-setosa 1.5 0.2 5.1 3.4 Iris-setosa 1.3 0.3 5.0 3.5 Iris-setosa 1.3 0.3 4.5 2.3 Iris-setosa 1.3 0.2 4.4 3.2 Iris-setosa 1.6 0.6 5.0 3.5 Iris-setosa 1.9 0.4 5.1 3.8 Iris-setosa 1.4 0.3 4.8 3.0 Iris-setosa 1.6 0.2 5.1 3.8 Iris-setosa 1.4 0.2 4.6 3.2 Iris-setosa 1.5 0.2 5.3 3.7 Iris-setosa 1.4 0.2 5.0 3.3 Iris-setosa 3.8 1.1 5.5 2.4 Iris-versicolor 3.7 1.0 5.5 2.4 Iris-versicolor 3.9 1.2 5.8 2.7 Iris-versicolor 5.1 1.6 6.0 2.7 Iris-virginica 4.5 1.5 5.4 3.0 Iris-versicolor 4.5 1.6 6.0 3.4 Iris-versicolor 4.7 1.5 6.7 3.1 Iris-versicolor 4.4 1.3 6.3 2.3 Iris-versicolor 4.1 1.3 5.6 3.0 Iris-versicolor 4.0 1.3 5.5 2.5 Iris-versicolor 4.4 1.2 5.5 2.6 Iris-versicolor 4.6 1.4 6.1 3.0 Iris-versicolor 4.0 1.2 5.8 2.6 Iris-versicolor 3.3 1.0 5.0 2.3 Iris-versicolor 4.2 1.3 5.6 2.7 Iris-versicolor 4.2 1.2 5.7 3.0 Iris-versicolor 4.2 1.3 5.7 2.9 Iris-versicolor 4.3 1.3 6.2 2.9 Iris-versicolor 3.0 1.1 5.1 2.5 Iris-versicolor 4.1 1.3 5.7 2.8 Iris-versicolor 6.1 1.9 7.4 2.8 Iris-virginica 6.4 2.0 7.9 3.8 Iris-virginica 5.6 2.2 6.4 2.8 Iris-virginica 5.1 1.5 6.3 2.8 Iris-virginica 5.6 1.4 6.1 2.6 Iris-virginica 6.1 2.3 7.7 3.0 Iris-virginica 5.6 2.4 6.3 3.4 Iris-virginica 5.5 1.8 6.4 3.1 Iris-virginica 4.8 1.8 6.0 3.0 Iris-virginica 5.4 2.1 6.9 3.1 Iris-virginica 5.6 2.4 6.7 3.1 Iris-virginica 5.1 2.3 6.9 3.1 Iris-virginica 5.1 1.9 5.8 2.7 Iris-virginica 5.9 2.3 6.8 3.2 Iris-virginica 5.7 2.5 6.7 3.3 Iris-virginica 5.2 2.3 6.7 3.0 Iris-virginica 5.0 1.9 6.3 2.5 Iris-virginica 5.2 2.0 6.5 3.0 Iris-virginica 5.4 2.3 6.2 3.4 Iris-virginica 5.1 1.8 5.9 3.0 Iris-virginica
View Code 【数据挖掘分类算法--KNN】转载于:https://www.cnblogs.com/niuxiaoha/p/4651018.html
推荐阅读
- 人工智能|收藏 | 计算机顶会论文投稿指南
- Java|docker化你的java应用(上)
- python|「薅羊毛」青龙定时面板——京东活动
- k8s|实战(本地存储-2022.3.1(更新版))
- #|Java 根据模板文件生成新的PPT
- java|云原生解决什么问题()
- Docker|Docker【2】 | 大白话带你快速安装Docker,不懂你捶我
- Springcloud笔记|SpringCloud 基础学习笔记
- 程序员|作为Java开发程序员,MySQL千万数据量深分页优化