为什么mean方法不能正常工作?

kiz8lqtg  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(243)

我有以下课程 imp 内部阶级 ScoringSummaryImplementation ,当我测试 mean() 方法本身工作正常,但当我测试它时,正如我在主方法中所示,答案不正确,我需要知道原因:

public class imp implements Normalizer {
     public static List<BigDecimal> dataToBeNormalized = new ArrayList<>();

    @Override
    public ScoringSummary zscore(Path csvPath, Path destPath, String colToStandardize) {

        ScoringSummary scoringObejct = new ScoringSummaryImplementation();
        if (!Files.exists(csvPath)) {
            throw new IllegalArgumentException("source file not found");
        }

        try {
            readDataToBeNormalized(csvPath, destPath, colToStandardize);
        } catch (IOException ex) {
            Logger.getLogger(imp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CsvValidationException ex) {
            Logger.getLogger(imp.class.getName()).log(Level.SEVERE, null, ex);
        }

        //Z-score
        for (int i = 0; i < dataToBeNormalized.size(); i++) {
            BigDecimal sub = (dataToBeNormalized.get(i).subtract(scoringObejct.mean()));
            try {
                BigDecimal divide = sub.divide(scoringObejct.standardDeviation(), 2, RoundingMode.HALF_EVEN);
                dataToBeNormalized.set(i, divide);
            } catch (ArithmeticException e) {

            }

        }

        return scoringObejct;
    }

    private void readDataToBeNormalized(Path csvPath, Path destPath, String colToStandardize) throws FileNotFoundException, IOException, CsvValidationException {
        int indexOfTheCol = -1;

        CSVReader reader = new CSVReader(new FileReader(csvPath.toString()));
        String[] header = reader.readNext();
        for (int i = 0; i < header.length; i++) {
            if (header[i].equalsIgnoreCase(colToStandardize)) {
                indexOfTheCol = i;
            }
        }
        if (indexOfTheCol == -1) {
            throw new IllegalArgumentException("column " + colToStandardize + " not found");
        }
        String[] values;
        while ((values = reader.readNext()) != null) {
            dataToBeNormalized.add(new BigDecimal(values[indexOfTheCol]));

        }

    }

    //Inner class to implement ScoringSummary
    public static class ScoringSummaryImplementation implements ScoringSummary {

        @Override
        public BigDecimal mean() {
             BigDecimal sum = new BigDecimal("0");
            BigDecimal sizeOfTheList = new BigDecimal(dataToBeNormalized.size());
            for (int i = 0; i < dataToBeNormalized.size(); i++) {
                sum = sum.add(dataToBeNormalized.get(i));
            }

         sum.divide(sizeOfTheList, 2, RoundingMode.HALF_EVEN);

           return sum.divide(sizeOfTheList, 2, RoundingMode.HALF_EVEN);
        }

    }

}

//主要测试:

public class Test {
    public static void main(String [] args){
        Path p = Paths.get("C:\\Users\\DELL\\Documents\\GitHub\\J2EE\\demo\\src\\main\\java\\com\\mycompany\\demo\\d.csv");
        Path p2 = Paths.get("C:\\Users\\DELL\\Documents\\GitHub\\J2EE\\demo\\src\\main\\java\\com\\mycompany\\demo\\des.csv");

    Normalizer n1 = new imp ();

    ScoringSummary n2 = n1.zscore(p, p2, "age");

        BigDecimal mean = n2.mean();
        System.out.println(mean);
    }

}

以下是输出:

0.82

以下是年龄栏:

|年龄|

|5434| |42423| |54534| |3333|

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题