【问题标题】:How to solve Non-Linear model in Java with Apache Commons or other?如何使用 Apache Commons 或其他解决 Java 中的非线性模型?
【发布时间】:2023-04-01 17:49:01
【问题描述】:

我有一个时间和温度 (x,y) 的样本,我需要知道在 Z 摄氏度(例如:35 C)时温度等于多少时间。

我收集的样本很少,为了开始微积分,我使用了 3 个样本。关注:

Temperature | Time (s)
    25      |  0 
    27      |  10 
    33      |  17
    40      |  ?

我知道我需要更多样本才能得到准确的结果,但一开始我就用这个。

问题是,我如何为此实现代码?我了解 Apache Commons,例如:

org.apache.commons.math3.optim.nonlinear.scalar 但我不知道如何使用这个库。 我需要一个微积分代码示例。 谢谢!

【问题讨论】:

  • 看来需要根据已知数据构建Pattern,最后使用Pattern获取。这是机器学习最简单的问题。你可以试试 WEKA。

标签: java apache-commons nonlinear-optimization non-linear-regression


【解决方案1】:

除非我误解了您想要实现的目标,否则您似乎将一个简单的问题复杂化了。

解决问题的最简单方法是将其视为Linear Regression 问题并使用SimpleRegression Apache Commons 类执行以下操作:

import org.apache.commons.math3.stat.regression.SimpleRegression;

public class MySimpleRegression {
    public static void main(String[] args) {

        // create a Simple Regression object 
        SimpleRegression simpleRegression = new SimpleRegression();

        // create your data object with various instances of x, y - make the 
        // variable you want to predict the 'y' in your data
        // i.e. if you wanna predict the time at a given temperature, 
        // 'x' would be temperature and 'y' time

        double[][] data = { { 25, 0 }, {27, 10 }, {33, 17 }, {40, 20 }};

        // pass this data to your simple regression object
        simpleRegression.addData(data);

        // and then you can predict the time at a given temperature value
        System.out.println("Predicted Time: "  + simpleRegression.predict(35));

        // You can also get the slope and intercept from your data
        System.out.println("slope = " + simpleRegression.getSlope());
        System.out.println("intercept = " + simpleRegression.getIntercept());
    }
}

这似乎是解决您的问题的更直接的方法。希望这对您或其他人有所帮助。

P.S.:没试过运行这个。不过应该可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 2011-10-04
    • 2015-04-06
    • 2012-08-30
    • 1970-01-01
    • 2013-05-21
    • 2018-02-14
    相关资源
    最近更新 更多