小编use*_*241的帖子

如何使用scipy.optimize.minimize进行最大似然回归

如何使用scipy.optimize.minimize?进行最大似然回归?我特别想在minimize这里使用这个函数,因为我有一个复杂的模型,需要添加一些约束.我目前正在尝试使用以下内容的简单示例:

from scipy.optimize import minimize

def lik(parameters):
    m = parameters[0]
    b = parameters[1]
    sigma = parameters[2]
    for i in np.arange(0, len(x)):
        y_exp = m * x + b
    L = sum(np.log(sigma) + 0.5 * np.log(2 * np.pi) + (y - y_exp) ** 2 / (2 * sigma ** 2))
    return L

x = [1,2,3,4,5]
y = [2,3,4,5,6]
lik_model = minimize(lik, np.array([1,1,1]), method='L-BFGS-B', options={'disp': True})
Run Code Online (Sandbox Code Playgroud)

当我运行它时,收敛失败.有谁知道我的代码有什么问题?

我运行它的消息是'ABNORMAL_TERMINATION_IN_LNSRCH'.我使用的是我optim在R中使用的算法.

python regression scipy

7
推荐指数
1
解决办法
8567
查看次数

在 matplotlib 中映射六边形网格

我想画一个带有六角形网格的图形。最终结果应该看起来像蜂窝。但是,我无法使用 matplotlib.collections.RegularPolyCollection 正确调整六边形的大小。任何人都可以看到我做错了什么,或者提供另一种解决方案。我想这以前已经做过了,所以我不需要重新发明轮子。

import matplotlib.pyplot as plt
from matplotlib import collections, transforms
from matplotlib.colors import colorConverter
import numpy as np

# Make some offsets, doing 4 polygons for simplicity here
xyo = [(0,0), (1,0), (0,1), (1,1)]
# length of hexagon side
hexside = 1
# area of circle circumscribing the hexagon
circ_area = np.pi * hexside ** 2

fig, ax = plt.subplots(1,1)
col = collections.RegularPolyCollection(6, np.radians(90), sizes = (circ_area,),
    offsets=xyo,transOffset=ax.transData)
ax.add_collection(col, autolim=True)
colors = [colorConverter.to_rgba(c) for c in ('r','g','b','c')]
col.set_color(colors) …
Run Code Online (Sandbox Code Playgroud)

python grid matplotlib

5
推荐指数
1
解决办法
4929
查看次数

Use Python lmfit with a variable number of parameters in function

I am trying to deconvolve complex gas chromatogram signals into individual gaussian signals. Here is an example, where the dotted line represents the signal I am trying to deconvolve.

在此输入图像描述 I was able to write the code to do this using scipy.optimize.curve_fit; however, once applied to real data the results were unreliable. I believe being able to set bounds to my parameters will improve my results, so I am attempting to use lmfit, which allows this. I am having a problem …

python modeling signal-processing lmfit deconvolution

5
推荐指数
2
解决办法
1808
查看次数

掩盖Fortran阵列的更好方法是什么?

我想掩盖一个Fortran数组.这是我目前正在做的方式......

where (my_array <=15.0)
    mask_array = 1
elsewhere
    mask_array = 0
end where
Run Code Online (Sandbox Code Playgroud)

那么我得到我的蒙面数组:

masked = my_array * mask_array
Run Code Online (Sandbox Code Playgroud)

有没有更简洁的方法来做到这一点?

arrays fortran masking fortran90

3
推荐指数
3
解决办法
1868
查看次数