【问题标题】:"index 1 is out of bounds for axis 0 with size 1" in pythonpython中的“索引1超出了轴0的范围,大小为1”
【发布时间】:2023-03-26 11:48:01
【问题描述】:

我似乎遇到了索引问题?我不知道如何解释这个错误......:/我认为这与我如何初始化你有关。

我有这个 3x3 G 矩阵,它是使用变量 u(一个向量,x - y)创建的。我现在刚刚制作了一个零矩阵,因为我不太确定如何对其进行编码,涉及到很多部分和规范哈哈。 x_j = (x_1 (j), x_2 (j), x_3 (j)) 和 y_j = (y_1 (j), y_2 (j), y_3 (j))。 x 和 y 是 nx3 个向量。 alpha_j 是一个 3x3 矩阵。 A 矩阵是大小为 3nx3n 的块对角矩阵。我在使用 W 矩阵时遇到问题(大小为 3nx3n,其中第 (i,j) 个块是由 alpha_i*G_[ij]*alpha_j 给出的 3x3 矩阵)。

def G(u):

    u1 = u[0]
    u2 = u[1]
    u3 = u[2]
    g = np.array([[0,0,0],[0,0,0],[0,0,0]],complex)  

    return g

def W(x, y, k, alpha, A):

    # initialization
    n = x.shape[0] # the number of x vextors 
    result = np.zeros([3*n,3*n],complex)
    u = np.matlib.zeros((n, 3)) # u = x - y 
    print(u)
    num_in_blocks = n

    # variables
    a_i = alpha_j(alpha, A)
    a_j = alpha_j(alpha, A)

    for i in range(0, 2):
        x1 = x[i] # each row of x
        y1 = y[i] # each row of y
        for j in range(0, n-1):
            u[i][j] = x1[j] - y1[j] # each row of x minus each row of y
        if i != j:
            block_result = a_i * G((u[i][j]), k) * a_j
            for k in range(num_in_blocks):
                for l in range(num_in_blocks):
                    result[3*i + k, 3*j + l] = block_result[i, j] 

    return result

def alpha_j(a, A):
    alph = np.array([[0,0,0],[0,0,0],[0,0,0]],complex)
    n = A.shape[0]
    rho = np.random.rand(n,1)
    for i in range(0, n-1):
        for j in range(0, n-1):
            alph[i,j] = (rho[i] * a * A[i,j])
    return alph

#------------------------------------------------------------------

# random case

def x(n):
    return np.random.randint(100, size=(n, 3))

def y(n):
    return np.random.randint(100, size=(n, 3))

# SYSTEM PARAMETERS

theta = 0 # can range from [0, 2pi)

chi = 10 + 1j

lam = 0.5 # microns (values between .4-.7)

k = (2 * np.pi)/lam # 1/microns

V_0 = (0.05)**3 # microns^3

K = k * np.array([[0], [np.sin(theta)], [np.cos(theta)]])

alpha = (V_0 * 3 * chi)/(chi + 3)

A = np.matlib.identity(3) 

#------------------------------------------------------------------

# TEST FUNCTIONS

w = W(x(3), y(3), k, alpha, A)
print(w)

我不断收到错误“标量变量的索引无效”。在 u1 = u[0] 行。

【问题讨论】:

  • 您不应该将这种类型的链式索引与numpy 数组一起使用。此外,调试此问题的一种简单方法是在流程的每个步骤中弄清楚您的形状是什么,因为它们显然不是您所期望的
  • @user3483203 链式索引是什么意思?这是我知道怎么做的唯一方法:/谢谢你的提示,我会试试的
  • 你为什么使用np.matlib
  • 检查问题数组的shape,并在该点进行索引。如果轴的大小为 1,则只能使用 [0] 作为索引。请记住索引在 [0,n) 范围内(就像 range(n)np.arange(n))。
  • @hpauli idk 我修复了那个 lmao。我会试试谢谢

标签: python numpy indexing


【解决方案1】:

np.matlibnp.matrixnp.ndarray 的子类。它应该给人一种 MATLAB 的感觉,并且(几乎)总是产生一个二维数组。不鼓励在新代码中使用它。

In [42]: U = np.matrix(np.arange(9).reshape(3,3))                                    
In [43]: U                                                                           
Out[43]: 
matrix([[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]])

使用 [0] 进行索引选择第一行,但返回一个二维矩阵。

In [44]: U[0]                                                                        
Out[44]: matrix([[0, 1, 2]])
In [45]: U[0].shape                                                                  
Out[45]: (1, 3)

添加另一个 [1] 仍然索引第一个维度(现在大小为 1):

In [46]: U[0][1]                                                                     
---------------------------------------------------------------------------
IndexError: index 1 is out of bounds for axis 0 with size 1

通常我们使用复合索引来索引 numpy 数组:

In [47]: U[0,1]                                                                      
Out[47]: 1

如果我们改为使用ndarray

In [48]: U = np.arange(9).reshape(3,3)                                               
In [49]: U                                                                           
Out[49]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
In [50]: U[0]                                                                        
Out[50]: array([0, 1, 2])      # 1d
In [51]: U[0][1]               # works,                                                       
Out[51]: 1
In [52]: U[0,1]                # still preferable                                                      
Out[52]: 1

【讨论】:

    猜你喜欢
    • 2018-08-28
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 2015-10-13
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多