英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论