【问题标题】:Python returns error "Arrays used as indices must be of integer (or boolean) type"Python 返回错误“用作索引的数组必须是整数(或布尔)类型”
【发布时间】:2020-05-16 18:47:06
【问题描述】:

我正在尝试拟合自己的 2d 高斯。主要目标是提取高斯参数。这是我的代码。

import matplotlib.pyplot as plt
import numpy as np
import random
from scipy.optimize import curve_fit,fmin

def Gauss2(x,y, amplitude, xo, yo, sigma_x, sigma_y, theta, bgr):
      xo = float(xo)
      yo = float(yo)    
      a = (np.cos(theta)**2)/(2*sigma_x**2) + (np.sin(theta)**2)/(2*sigma_y**2)
      b = -(np.sin(2*theta))/(4*sigma_x**2) + (np.sin(2*theta))/(4*sigma_y**2)
      c = (np.sin(theta)**2)/(2*sigma_x**2) + (np.cos(theta)**2)/(2*sigma_y**2)
      g = bgr + amplitude*np.exp( - (a*((x-xo)**2) + 2*b*(x-xo)*(y-yo) 
                        + c*((y-yo)**2)))
      return g

xsz = 40
ysz = 40

x0=xsz/2
y0=ysz/2

MInt1=200
bgr=10
noise=5

sigma_x = 5.
sigma_y = 4.

### create random Gaussian 2d
x = np.linspace(0, xsz-1, xsz)
y = np.linspace(0, xsz-1, xsz)
xx1, yy1 = np.meshgrid(x, y)
ex1=np.exp(-(((xx1-x0)**2)/(2*sigma_x**2) + ((yy1-y0)**2)/(2*sigma_y**2)))
z1 = MInt1* random.uniform(0, 1)*noise* ex1

theta=0
#aa=Gauss2(xx1,yy1, MInt1,x0,y0,sigma_x,sigma_y,theta,bgr)

poptX, pcovX = curve_fit(Gauss2, z1[x,y], p0=[x,y, MInt1,x0,y0,sigma_x,sigma_y,theta,bgr])

img = z1.reshape((xsz,xsz))
plt.imshow(img); plt.colorbar();plt.show()

我收到这条消息:

IndexError: 用作索引的数组必须是整数(或布尔)类型

【问题讨论】:

  • 请发布完整的错误回溯!
  • 错误在哪一行?
  • 我怀疑 z1[x,y] 表达式。 xylinspace 生成,因此是浮点数(即使它们可以四舍五入为整数)。索引操作不多。
  • @Raju:感谢您想在这里编辑问题。我批准了您的修改,但请不要使用全部大写 - 这被广泛理解为大喊大叫。

标签: python numpy numpy-ndarray


【解决方案1】:
x = np.linspace(0, xsz-1, xsz, dtype='int')
y = np.linspace(0, xsz-1, xsz, dtype='int')

这样

z1[x, y] 

会起作用(前提是z1 大小合适)

【讨论】:

    【解决方案2】:

    您会遇到一些问题。第一:

    x = np.linspace(0, xsz-1, xsz, dtype=np.int)
    y = np.linspace(0, xsz-1, xsz, dtype=np.int)
    

    xy 转换为整数类型,以便您可以索引到z1 并摆脱您当前看到的错误。

    此外,您拨打curve_fit 也会遇到一些问题。调用应该类似于:

    poptX, pcovX = curve_fit(f, xdata, ydata, p0=[p1, ..., pn])
    

    如果我正在解释您要完成的任务,这似乎是在二维高斯内拟合一维曲线,您必须执行以下操作:

     poptX, pcovX = curve_fit(Gauss2, (x, y), z1[x, y], p0=[MInt1,x0,y0,sigma_x,sigma_y,theta,bgr])
    

    这需要您对Gauss2 的定义进行非常轻微的修改,因为 scipy 期望数据作为第一个位置参数传入:

    def Gauss2(X, amplitude, xo, yo, sigma_x, sigma_y, theta, bgr):
          x, y = X # Pack x, y params into X
          xo = float(xo)
          yo = float(yo)    
          a = (np.cos(theta)**2)/(2*sigma_x**2) + (np.sin(theta)**2)/(2*sigma_y**2)
          b = -(np.sin(2*theta))/(4*sigma_x**2) + (np.sin(2*theta))/(4*sigma_y**2)
          c = (np.sin(theta)**2)/(2*sigma_x**2) + (np.cos(theta)**2)/(2*sigma_y**2)
          g = bgr + amplitude*np.exp( - (a*((x-xo)**2) + 2*b*(x-xo)*(y-yo) 
                            + c*((y-yo)**2)))
          return g
    

    【讨论】:

    • 大家好,非常感谢您的讨论。解决了“IndexError:用作索引的数组必须是整数(或布尔)类型”的问题。
    • @OtabekBur 我的回答可能也解释了您在curve_fit 中看到的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    相关资源
    最近更新 更多