其他分享
首页 > 其他分享> > day57

day57

作者:互联网


            // Step 2.4 Find the new center
            double[] tempCenterDistance = new double[tempNewCenters.length];
            double[] tempNearest = new double[tempNewCenters.length];
            for (int i = 0; i < tempNewCenters.length; i++) {
                for (int j = 0; j < numClusters; j++) {
                    tempCenterDistance[j] = newDistance(tempNewCenters[i], tempCenters[j], tempNewCenters.length);
                    if (tempCenterDistance[j] > tempNearest[j]) {
                        tempCenterDistance[j] = tempNearest[j];
                        tempNewCenters[i] = tempCenters[j];
                    }//of if
                }//of for j
            }//of for i

以上为定位准确位置,以下为距离计算

/**
     *********************
     * The distance between two instances.
     *
     * @param paraI
     *            The index of the first instance.
     * @param paraArray
     *            The array representing a point in the space.
     * @return The distance.
     *********************
     */
    public double newDistance(double[] paraI, double[] paraArray, int paraLength) {
        int resultDistance = 0;
        double tempDifference;
        switch (distanceMeasure) {
            case MANHATTAN:
                for (int i = 0; i < paraLength; i++) {
                    for (int j = 0; i < dataset.numAttributes() - 1; i++) {
                        tempDifference = paraI[i] - paraArray[j];
                        //将距离绝对值加上
                        if (tempDifference < 0) {
                            resultDistance -= tempDifference;
                        } else {
                            resultDistance += tempDifference;
                        }//of if
                    }//of for j
                }//of for i

                break;

            case EUCLIDEAN:
                for (int i = 0; i < paraLength; i++) {
                    for (int j = 0; j < dataset.numAttributes() - 1; j++) {
                    tempDifference = paraI[i] - paraArray[j];
                    resultDistance += tempDifference * tempDifference;
                    //根据维度求欧式距离
                    }//of for j
                }//of for i
                break;
            default:
                System.out.println("Unsupported distance measure: " + distanceMeasure);
        }//of switch

        return resultDistance;
    }//of newDistance

标签:int,double,++,tempNewCenters,tempDifference,resultDistance,day57
来源: https://blog.csdn.net/mitchellemin/article/details/118864042