gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:23:40 25 4
gpt4 key购买 nike

我正在使用 Process p = Runtime.getRuntime().exec 在 Java 中运行曲线拟合 Python 脚本,并且在我调用 scipy.optimize.curve_fit 之一后,Java 没有获得输出。我正在拟合分段误差函数,如下所示。

我创建了该函数的“虚拟”版本来检查曲线拟合是否可以正常工作,而且确实如此。但是当我将曲线拟合应用于实际数据时,Java 没有获得该语句下方的任何输出。

我在终端中运行了 Python 代码,它运行良好。

Java代码:

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代码

## 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 仅打印第一个“到达这里”,但不打印第二条曲线拟合后应遵循的“现在到达这里”。同样,它在终端中运行得非常好。我需要获得第二条曲线拟合的输出。发生什么事了?

最佳答案

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

关于java - scipy.optimize.curve_fit 使用 Runtime.getRuntime().exec() 在 Java 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56245577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com