在Julia中是否有类似于scipy.optimize.least_squares的功能?

huangapple go评论61阅读模式
英文:

Is there an analog for scipy.optimize.least_squares in Julia?

问题

我正在尝试将我在Python中的一些代码改写成Julia,以检查是否会更快,并在某个地方遇到了问题。

在Python中,我有以下代码来计算一些点的一种中心:

from scipy.optimize import least_squares
import numpy as np

def lat_lng_dist(lat, lng, _lat, _lng):
    # 计算点(lat, lng)与点之间的距离
    return # 包含距离的数组

def lat_lng_metric_MAE(point, _lat, _lng, _radius):
    lat, lng = point
    modules = np.abs(lat_lng_dist(lat, lng, _lat, _lng) - _radius)
    return np.mean(modules)

def predict_center(init_point, data):
    return least_squares(lat_lng_metric_MAE, x0=init_point, loss='cauchy', \
                         args=(data['lat'].values, data['lng'].values, data['radius'].values, ))

Julia中是否有一个可替代的方法?
或者可以指导我如何使用类似LsqFit等包来获得相同的结果,我已经花了半天时间,找到了一些类似的方法,但不知道如何采用它们。

英文:

I'm trying to rewrite some Python code i have in Julia to check if it will be faster and got stuck at some point.

In Python i have this code to calculate a kind of center for some points:

from scipy.optimize import least_squares
import numpy as np

def lat_lng_dist(lat, lng, _lat, _lng):
    # calc distance between point (lat, lng) and points
    return # array with distances

def lat_lng_metric_MAE(point, _lat, _lng, _radius):
    lat, lng = point
    modules = np.abs(lat_lng_dist(lat, lng, _lat, _lng) - _radius)
    return np.mean(modules)

def predict_center(init_point, data):
    return least_squares(lat_lng_metric_MAE, x0=init_point, loss='cauchy', \
                         args=(data['lat'].values, data['lng'].values, data['radius'].values, ))

Is there a drop in replacement for Julia?
Or maybe point me on how to get same result with packages like LsqFit and others, spent half a day on this, find some similar, but can't understand how to adopt them.

答案1

得分: 1

抱歉,代码部分无需翻译。以下是翻译好的部分:

"Well, I didn't find a direct replacement and this part is quite small compared to the whole program, so I used PyCall for this, hoping scipy is more or less fast, and now the code looks like this:

using PyCall
optimize = pyimport("scipy.optimize")

"""
import numpy as np

def lat_lng_dist(lat, lng, _lat, _lng):
    d_s_lat = np.square(np.sin(np.deg2rad((lat - _lat) / 2)))
    d_s_lng = np.square(np.sin(np.deg2rad((lng - _lng) / 2)))
    return 2 * np.float64(6371008.8) * np.arcsin(
        np.sqrt(d_s_lat + np.cos(np.deg2rad(lat)) * np.cos(np.deg2rad(_lat)) * d_s_lng))

def lat_lng_metric_MAE(point, _lat, _lng, _radius):
    lat, lng = point
    modules = np.abs(lat_lng_dist(lat, lng, _lat, _lng) - _radius)
    return np.mean(modules )
"""

function predict_center(init_point, data)
    return optimize.least_squares(py"lat_lng_metric_MAE", x0=init_point, loss="cauchy", args=(data.lat, data.lng, data.radius))
end

I don't know how this performs, but in overall I got program running ~50 times faster and I'm happy with it.
But still would highly appreciate if someone could help to translate it to native Julia code."

英文:

Well, I didn't find a direct replacement and this part is quite small compared to the whole program, so I used PyCall for this, hoping scipy is more or less fast, and now the code looks like this:

using PyCall
optimize = pyimport("scipy.optimize")

py"""
import numpy as np

def lat_lng_dist(lat, lng, _lat, _lng):
    d_s_lat = np.square(np.sin(np.deg2rad((lat - _lat) / 2)))
    d_s_lng = np.square(np.sin(np.deg2rad((lng - _lng) / 2)))
    return 2 * np.float64(6371008.8) * np.arcsin(
        np.sqrt(d_s_lat + np.cos(np.deg2rad(lat)) * np.cos(np.deg2rad(_lat)) * d_s_lng))

def lat_lng_metric_MAE(point, _lat, _lng, _radius):
    lat, lng = point
    modules = np.abs(lat_lng_dist(lat, lng, _lat, _lng) - _radius)
    return np.mean(modules )
"""

function predict_center(init_point, data)
    return optimize.least_squares(py"lat_lng_metric_MAE", x0=init_point, loss="cauchy", args=(data.lat, data.lng, data.radius))
end

I don't know how this performs, but in overall I got program running ~50 times faster and I'm happy with it.
But still would highly appreciate if someone could help to translate it to native Julia code.

huangapple
  • 本文由 发表于 2023年6月16日 04:33:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定