栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

心得体会day52(日撸 Java 三百行)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

心得体会day52(日撸 Java 三百行)

文章链接:日撸 Java 三百行(总述)_minfanphd的博客-CSDN博客

day52 kNN 分类器 (续) 52.1 重新实现 computeNearests, 仅需要扫描一遍训练集, 即可获得 k 个邻居 

分析:

扫描一次训练集,当时我第一想法是先扫描训练集,计算测试点到训练集的距离并用距离数组进行存储,在对距离数组进行插入排序获取前k个数据,但是这样并不满足时间复杂度的要求,后面对其进行改进,在计算距离的同时对数据进行排序,这样就满足要求了(因为这个方法返回的邻居,这里借助了一个数组来存储训练集的索引并与距离数组一一对应)。经过测试,符合要求。

当时想着不用两个数组来做,用 List> distance = new ArrayList<>(),键值对的形式,但是发现这个在进行交换数据比较麻烦,故放弃了。

如下代码:

public int[] computeNearestsNew(int paraCurrent) {
        int[] resultNearests = new int[numNeighbors];

        int[] trainingIndex = new int[trainingSet.length];
        double[] trainingDistance = new double[trainingSet.length];
       
        int j;
        double tempData;

        //在计算距离的同时进行插入排序
        for (int i = 0; i < trainingSet.length; i++) {
            trainingDistance[i]  = distance(paraCurrent, trainingSet[i]);
            tempData = trainingDistance[i];
            for (j = i-1; j >= 0 && trainingDistance[j] > tempData; j--) {
                trainingDistance[j+1] = trainingDistance[j];
                trainingIndex[j+1] = trainingIndex[j];
            }
            trainingDistance[j+1] = tempData;
            trainingIndex[j+1] = i;
        }

        //获取前面k个邻居
        for (int i = 0; i < numNeighbors; i++) {
            resultNearests[i] = trainingSet[trainingIndex[i]];
        }
        System.out.println("The nearest of " + paraCurrent + " are: " + Arrays.toString(resultNearests));
        return resultNearests;
    }

运行结果:

The nearest of 46 are: [19, 48, 4, 27, 10, 0, 17]
The nearest of 50 are: [52, 76, 58, 75, 77, 51, 56]
The nearest of 128 are: [132, 104, 103, 111, 112, 147, 140]
The nearest of 1 are: [45, 12, 34, 37, 25, 30, 35]
The nearest of 54 are: [58, 75, 76, 74, 51, 91, 72]
The nearest of 44 are: [19, 43, 48, 26, 24, 27, 23]
The nearest of 21 are: [19, 17, 4, 48, 0, 27, 43]
The nearest of 53 are: [80, 69, 92, 90, 59, 82, 99]
The nearest of 40 are: [17, 0, 4, 7, 49, 39, 27]
The nearest of 16 are: [10, 48, 33, 19, 36, 14, 18]
The nearest of 89 are: [69, 80, 92, 99, 59, 82, 90]
The nearest of 5 are: [18, 10, 48, 19, 33, 27, 31]
The nearest of 116 are: [103, 147, 111, 112, 132, 104, 124]
The nearest of 88 are: [96, 95, 99, 82, 66, 55, 92]
The nearest of 81 are: [80, 69, 79, 92, 82, 64, 62]
The nearest of 144 are: [140, 143, 124, 104, 100, 112, 115]
The nearest of 101 are: [142, 113, 121, 83, 127, 114, 123]
The nearest of 122 are: [105, 135, 125, 117, 102, 131, 129]
The nearest of 9 are: [34, 37, 30, 12, 25, 49, 29]
The nearest of 126 are: [123, 127, 146, 83, 133, 72, 78]
The nearest of 136 are: [148, 115, 100, 140, 124, 104, 143]
The nearest of 119 are: [72, 83, 68, 146, 113, 123, 133]
The nearest of 32 are: [33, 19, 48, 10, 4, 27, 0]
The nearest of 67 are: [92, 82, 99, 69, 95, 96, 71]
The nearest of 6 are: [47, 2, 42, 29, 3, 30, 7]
The nearest of 106 are: [84, 59, 90, 66, 121, 55, 113]
The nearest of 141 are: [145, 139, 112, 147, 115, 140, 77]
The nearest of 85 are: [56, 51, 91, 78, 66, 127, 97]
The nearest of 61 are: [96, 78, 95, 99, 97, 71, 66]
The nearest of 28 are: [27, 0, 39, 17, 7, 49, 4]
The nearest of 118 are: [105, 135, 117, 102, 125, 131, 109]
The nearest of 86 are: [52, 58, 75, 76, 77, 51, 56]
The nearest of 110 are: [147, 115, 77, 145, 139, 127, 112]
The nearest of 149 are: [127, 142, 83, 121, 123, 133, 113]
The nearest of 138 are: [127, 123, 78, 91, 83, 56, 142]
The nearest of 63 are: [91, 73, 78, 97, 127, 55, 133]
The nearest of 70 are: [127, 56, 91, 78, 121, 66, 142]
The nearest of 130 are: [102, 125, 129, 135, 105, 108, 143]
The nearest of 107 are: [125, 105, 102, 129, 135, 143, 108]
The nearest of 120 are: [143, 140, 124, 112, 139, 102, 104]
The nearest of 65 are: [75, 58, 51, 74, 76, 52, 97]
The nearest of 94 are: [99, 96, 90, 92, 55, 95, 82]
The nearest of 13 are: [38, 42, 8, 47, 2, 3, 12]
The nearest of 137 are: [103, 147, 111, 112, 124, 132, 104]
The nearest of 11 are: [29, 7, 26, 24, 30, 49, 39]
The accuracy of the classifier is: 0.9555555555555556
 52.2增加 setDistanceMeasure() , setNumNeighors() 方法.
    public void setDistanceMeasure(int distanceMeasure) {
        this.distanceMeasure = distanceMeasure;
    }    

    public void setNumNeighbors(int numNeighbors) {
        this.numNeighbors = numNeighbors;
    }
总结:

今天结合昨天的内容,对昨天的内容又进行了一遍梳理,更熟悉这个内容了,对computeNearests ()方法进行重写,提高了其运行效率。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/778393.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号