简体   繁体   English

TypeError:'dia_matrix'对象没有属性'__getitem__'-Python

[英]TypeError: 'dia_matrix' object has no attribute '__getitem__' - Python

I'm currently trying to dot product a row from a diagonal matrix formed by scipy.sparse.diags function in "Poisson Stiffness" below, but i am having trouble selecting the row and am getting the following error: 我目前正在尝试从下面的“泊松刚度”中scipy.sparse.diags函数形成的对角矩阵中点积一行,但是我在选择该行时遇到了麻烦,并且遇到了以下错误:

TypeError: 'dia_matrix' object has no attribute '__getitem__'

The following is my code 以下是我的代码

def Poisson_Stiffness(x0):
    """Finds the Poisson equation stiffness matrix with any non uniform mesh x0"""

    x0 = np.array(x0)
    N = len(x0) - 1 # The amount of elements; x0, x1, ..., xN

    h = x0[1:] - x0[:-1]

    a = np.zeros(N+1)
    a[0] = 1 #BOUNDARY CONDITIONS
    a[1:-1] = 1/h[1:] + 1/h[:-1]
    a[-1] = 1/h[-1]
    a[N] = 1 #BOUNDARY CONDITIONS

    b = -1/h
    b[0] = 0 #BOUNDARY CONDITIONS

    c = -1/h
    c[N-1] = 0 #BOUNDARY CONDITIONS: DIRICHLET

    data = [a.tolist(), b.tolist(), c.tolist()]
    Positions = [0, 1, -1]
    Stiffness_Matrix = diags(data, Positions, (N+1,N+1))

    return Stiffness_Matrix


def Error_Indicators(Uh,U_mesh,Z,Z_mesh,f):
    """Take in U, Interpolate to same mesh as Z then solve for eta"""

    u_inter = interp1d(U_mesh,Uh) #Interpolation of old mesh
    U2 = u_inter(Z_mesh) #New function u for the new mesh to use in 

    Bz = Poisson_Stiffness(Z_mesh)

    eta = np.empty(len(Z_mesh))
    for i in range(len(Z_mesh)):
        eta[i] = (f[i] - np.dot(Bz[i,:],U2[:]))*z[i]

    return eta

The error is specifically coming from the following line of code: 该错误专门来自以下代码行:

eta[i] = (f[i] - np.dot(Bz[i,:],U2[:]))*z[i]

It is the Bz matrix that is causing the error, being created using scipy.sparse.diags and will not allow me to call the row. 使用scipy.sparse.diags创建的是导致错误的Bz矩阵,不允许我调用该行。

Bz = Poisson_Stiffness(np.linspace(0,1,40))
print Bz[0,0]

This code will also produce the same error. 此代码也将产生相同的错误。

Any help is very much appreciated Thanks 任何帮助都非常感谢

sparse.dia_matrix apparently does not support indexing. sparse.dia_matrix显然不支持索引。 The way around that would be to convert it to another format. 解决方法是将其转换为另一种格式。 For calculation purposes tocsr() would be appropriate. 出于计算目的, tocsr()是合适的。

The documentation for dia_matrix is limited, though I think the code is visible Python. dia_matrix的文档是有限的,尽管我认为代码是可见的Python。 I'd have to double check, but I think it is a matrix construction tool, not a fully developed working format. 我必须仔细检查,但我认为它是矩阵构造工具,而不是完全开发的工作格式。

In [97]: M=sparse.dia_matrix(np.ones((3,4)),[0,-1,2])

In [98]: M[1,:]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-98-a74bcb661a8d> in <module>()
----> 1 M[1,:]

TypeError: 'dia_matrix' object is not subscriptable

In [99]: M.tocsr()[1,:]
Out[99]: 
<1x4 sparse matrix of type '<class 'numpy.float64'>'
    with 4 stored elements in Compressed Sparse Row format>

A dia_matrix stores its information in a .data and .offsets attributes, which are simple modifications of the input parameters. dia_matrix将其信息存储在.data.offsets属性中,这是对输入参数的简单修改。 They aren't ammenable for 2d indexing. 它们不适用于2d索引。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError&#39;classobj&#39;对象在python中没有属性&#39;__getitem__&#39; - TypeError 'classobj' object has no attribute '__getitem__' in python 类型错误:&#39;float&#39; 对象在 Python 中没有属性 &#39;__getitem__&#39; - TypeError: 'float' object has no attribute '__getitem__' in Python Python TypeError:“ int”对象没有属性“ __getitem__” - Python TypeError : 'int' object has no attribute '__getitem__' Python TypeError:“ NoneType”对象在图像处理中没有属性“ __getitem__” - Python TypeError: 'NoneType' object has no attribute '__getitem__' in image processing PYTHON:TypeError:&#39;NoneType&#39;对象没有TESTCASE的属性&#39;__getitem__&#39; - PYTHON: TypeError: 'NoneType' object has no attribute '__getitem__' for TESTCASE Python_TypeError:“ NoneType”对象没有属性“ __getitem__” - Python_TypeError: 'NoneType' object has no attribute '__getitem__' TypeError:“函数”对象没有属性“ __getitem __” / Python - TypeError: 'function' object has no attribute '__getitem__'/Python Python:TypeError:“函数”对象没有属性“ __getitem__” - Python: TypeError: 'function' object has no attribute '__getitem__' 类型错误:&#39;generator&#39; 对象在 python 中没有属性 &#39;__getitem__&#39; - TypeError: 'generator' object has no attribute '__getitem__' in python AI Python-TypeError:“ int”对象没有属性“ __getitem__” - AI Python - TypeError: 'int' object has no attribute '__getitem__'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM