标量变量错误的索引是什么意思?蟒蛇

时间:2021-06-22 16:57:05
import numpy as np

with open('matrix.txt', 'r') as f:
    x = []
    for line in f:
        x.append(map(int, line.split()))
f.close()

a = array(x)

l, v = eig(a)

exponent = array(exp(l))

L = identity(len(l))

for i in xrange(len(l)):
    L[i][i] = exponent[0][i]

print L

My code opens up a text file containing a matrix:
1 2
3 4
and places it in list "x" as integers. The list "x" is then converted into an array "a". The eigenvalues of "a" are placed in "l" and the eigenvectors are placed in "v". I then want to take the exp(a) and place it in another array "exponent". Then I create an identity matrix of whatever length "l" is and call the matrix "L". My for loop is suppose to take the values of "exponent" and replace the 1's across the diagonal of the identity matrix but I get an error saying "invalid index to scalar variable". What is wrong with my code?

我的代码打开一个包含矩阵的文本文件:1 2 3 4并将其作为整数放在列表“x”中。然后将列表“x”转换为数组“a”。 “a”的特征值放在“l”中,特征向量放在“v”中。然后我想把exp(a)放在另一个数组“exponent”中。然后我创建一个长度为“l”的单位矩阵,并调用矩阵“L”。我的for循环假设采用“exponent”的值并在单位矩阵的对角线上替换1,但是我得到一个错误,说“标量变量的索引无效”。我的代码出了什么问题?

2 个解决方案

#1


18  

exponent is a 1D array. This means that exponent[0] is a scalar, and exponent[0][i] is trying to access it as if it were an array.

指数是一维数组。这意味着exponent [0]是一个标量,而exponent [0] [i]正试图访问它,就像它是一个数组一样。

Did you mean to say:

你的意思是说:

L = identity(len(l))
for i in xrange(len(l)):
    L[i][i] = exponent[i]

or even

L = diag(exponent)

?

#2


8  

IndexError: invalid index to scalar variable happens when you try to index a numpy scalar such as numpy.int64 or numpy.float64. It is very similar to TypeError: 'int' object has no attribute '__getitem__' when you try to index an int.

IndexError:当您尝试索引numpy标量(如numpy.int64或numpy.float64)时,会发生标量变量的无效索引。它与TypeError非常相似:当您尝试索引int时,'int'对象没有属性'__getitem__'。

>>> a = np.int64(5)
>>> type(a)
<type 'numpy.int64'>
>>> a[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: invalid index to scalar variable.
>>> a = 5
>>> type(a)
<type 'int'>
>>> a[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object has no attribute '__getitem__'

#1


18  

exponent is a 1D array. This means that exponent[0] is a scalar, and exponent[0][i] is trying to access it as if it were an array.

指数是一维数组。这意味着exponent [0]是一个标量,而exponent [0] [i]正试图访问它,就像它是一个数组一样。

Did you mean to say:

你的意思是说:

L = identity(len(l))
for i in xrange(len(l)):
    L[i][i] = exponent[i]

or even

L = diag(exponent)

?

#2


8  

IndexError: invalid index to scalar variable happens when you try to index a numpy scalar such as numpy.int64 or numpy.float64. It is very similar to TypeError: 'int' object has no attribute '__getitem__' when you try to index an int.

IndexError:当您尝试索引numpy标量(如numpy.int64或numpy.float64)时,会发生标量变量的无效索引。它与TypeError非常相似:当您尝试索引int时,'int'对象没有属性'__getitem__'。

>>> a = np.int64(5)
>>> type(a)
<type 'numpy.int64'>
>>> a[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: invalid index to scalar variable.
>>> a = 5
>>> type(a)
<type 'int'>
>>> a[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object has no attribute '__getitem__'