首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Runtime.getRuntime().exec()在Java中运行scipy.optimize.curve_fit

使用Runtime.getRuntime().exec()在Java中运行scipy.optimize.curve_fit
EN

Stack Overflow用户
提问于 2019-05-22 04:00:32
回答 1查看 151关注 0票数 0

我使用进程p= Runtime.getRuntime().exec在Java语言中运行了一个曲线拟合Python脚本,在我对scipy.optimize.curve_fit的一次调用之后,Java没有得到输出。我正在拟合一个分段误差函数,如下所示。

我创建了一个“虚拟”版本的函数,以检查曲线拟合是否在此基础上工作,它做到了。但是,当我将曲线拟合应用于实际数据时,Java没有得到该语句下面的任何输出。

我在终端中运行了Python代码,它工作得很好。

Java代码:

代码语言:javascript
运行
复制
String[] command = new String[2];
command[0] = "python";
command[1] = "python/curvefit.py";


try {
    Process p = Runtime.getRuntime().exec(command);
    BufferedReader stdInput = new BufferedReader(new 
    InputStreamReader(p.getInputStream()));
    while ((s1 = stdInput.readLine()) != null) {
        System.out.println(s1);
    }
}catch (IOException e) {
    e.printStackTrace();
}

Python代码

代码语言:javascript
运行
复制
## modifiable error function
def easyerf(x,a,b,c, d):
    return a*scipy.special.erf((b * x) - c) + d

## piecewise error function
def pwerf(x, a1, a2, b, c1, c2, h, e):
    returnarray = []
    for i in x:
        if i < e:
            returnarray.append(easyerf(i, a1, b, c1, h - a1))
        else:
            returnarray.append(easyerf(i, a2, b, c2, h + a2))
    return returnarray


array = np.linspace(-5, 10, 15)
array2 = np.linspace(10, 25, 15)
array3 = [easyerf(i, 4, 1, 1, 1) for i in array]
array4 = [easyerf(i, -4, 1, 15, 1) for i in array2]
array5 = np.concatenate((array,array2)) # x values
array6 = np.concatenate((array3,array4)) # y values
pguess7 = [10, -4, 17, 1, 15, 1, 10]

par3, con3 = scipy.optimize.curve_fit(pwerf, array5, array6, p0 = pguess7)

print("got here")

indexes = [1, 2, 3, 4, 5, 6, 7, 8,... ## long array
intensities = [0.050938000000000004, 0.049611, 0.054938, 0.047958,... ## long array

## fit piecewise error function and find ends/length
par4, con4 = scipy.optimize.curve_fit(pwerf, indexes, intensities, [10, 1, 1, 5, 17, 25,120])
print("now got here")

Java只打印第一个"got here“,而不打印第二个曲线拟合后应该紧跟的"now got here”。再说一次,它在终端上工作得很好。我需要从第二次曲线拟合中获得输出。发生什么事了呢?

EN

回答 1

Stack Overflow用户

发布于 2019-05-29 04:02:31

更新:弄清楚了。Java运行的是较旧版本的Python,并且要求curve_fit的参数是数值数组,而不是列表。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56245577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档