0

我正在尝试运行 OLS 回归,代码工作正常,但将变量连接成一个例外。我试图调查可能是什么问题,但我无法找到原因

@Component
public class AppRunner implements CommandLineRunner{

    private static final String DATA_FILE =  "/path/to/datafile.csv";

    @Override
    public void run(String... args) throws Exception {
        buildModel(DATA_FILE, 2304);
    }

    public static OLSMultipleLinearRegression buildModel( String filenames, int SIZE ) throws IOException {
        double[] Y = new double[SIZE];
        double[][] X = new double[SIZE][];

        BufferedReader bufferedReader = new BufferedReader(new FileReader(filenames));
        try {
            String record;
            int index = 0;
            while ((record = bufferedReader.readLine()) != null) {
                String[] tokens = StringUtils.split(record, ",");
                Y[index] = Double.parseDouble(tokens[0]);
                double[] features = new double[tokens.length - 1];

                for (int i = 0; i < features.length; i++) {
                    features[i] = Double.parseDouble(tokens[i + 1]);
                }
                X[index] = features;
                index++;
            }
        } finally {
            bufferedReader.close();
        }
        OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
        regression.newSampleData(Y, X);
        regression.setNoIntercept(false);
        System.out.println(regression);
        return regression;
    }

}

它抛出以下错误

Caused by: java.lang.NumberFormatException: For input string: "226.12000000,160.99000000,50.69000000"
    at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054) ~[na:na]
    at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110) ~[na:na]
    at java.base/java.lang.Double.parseDouble(Double.java:543) ~[na:na]
    at io.christdoes.wealth.generator.cmdrunner.AppRunner.buildModel(AppRunner.java:42) ~[classes/:na]
    at io.christdoes.wealth.generator.cmdrunner.AppRunner.run(AppRunner.java:25) ~[classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    ... 5 common frames omitted

从我可以阅读的内容来看,它似乎是连接这些值而不是一个接一个地读取。

4

0 回答 0