day53

该博客介绍了两种机器学习中的方法:一种是加权投票分类,通过距离排序邻居并赋予不同的权重来预测类别;另一种是留一法(Leave-One-Out)交叉验证,用于分割数据集进行训练和测试。这两种技术常用于提升分类模型的性能和评估其稳定性。
摘要由CSDN通过智能技术生成

1.进行权重排序

    /**
     ********************************
     * Voting using the instances.
     *
     * @param paraNeighbors The sorted indices of the neighbors.
     * @return The predicted label.
     ********************************
     */
    public int weightedVoting(int[] paraNeighbors) {

        int[] tempVotes = new int[dataset.numClasses()];
        for (int i = 0; i < paraNeighbors.length; i++) {
            //投票,由于i=0的点即对应距离最小点,因此可直接根据距离顺序决定投票权重
            tempVotes[(int) dataset.instance(paraNeighbors[i]).classValue()] += 2/(i+1);
        }//of for i

        int tempMaximalVotingIndex = 0;
        int tempMaximalVoting = 0;
        //统票
        for (int i = 0; i < dataset.numClasses(); i++) {
            if (tempVotes[i] > tempMaximalVoting) {
                tempMaximalVoting = tempVotes[i];
                tempMaximalVotingIndex = i;
            }//of if
        }//of for i
        return tempMaximalVotingIndex;
    }//of weightedVoting

2.leave-one-out

/**
     ********************************
     * Split the data into training and testing parts.
     *
     * @param tempIndex
     ********************************
     */
    public void oneSplitTrainingTesting(int tempIndex) {
        int tempSize = dataset.numInstances();
        int tempTrainingSize = (int) (tempSize - 1);

        trainingSet = new int[tempTrainingSize];
        testingSet = new int[tempSize - tempTrainingSize];

        int tempTrainIdx = 0;
        for (int i = 0; i < tempSize; i++) {
            if (i != tempIndex)
                trainingSet[tempTrainIdx++] =(int) dataset.instance(i).classValue();
        }//of for i

            testingSet[0] = (int)dataset.instance(tempIndex).classValue();

    }//of oneSplitTrainingTesting

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值