• Register
1 vote
4.6k views

Problem:

I have a text file that contains a matrix of [2][2] and I am trying to open it in my python program and store the values as integers in a variable. Further, I tried to add some more operations with those values. For better understanding you may check my sample code snippet below:

import numpy as np

with open('test.txt', 'r') as f:
    n = []
    for line in f:
        n.append(map(int, line.split()))
f.close()
a = array(n)
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

I am willing to take the values of exponent and replace the 1's across the diagonal of the identity matrix using the for-loop but this not happening in my case and I am getting an error

IndexError: invalid index to scalar variable.

What is the solid reason behind it, and how can I fix the error? Please help to get rid of this. I appreciate your concern.

Thanks.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

2 votes

Solution:

You have messed up on the line 

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

Here, exponent [0] is a scalar. In your program exponent is a One-Dimensional array. Once you write exponent [0] [i] it behaves like a two-dimensional array although it is not. You may want to write this way:

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

I hope you got the point and this might help you. Good Day!

13 9 6
94,260 points

Related questions

0 votes
1 answer 54 views
54 views
Problem: What is the solid reason behind it, and how can I fix the error? Please help to get rid of this. I appreciate your concern: Indexerror: invalid index to scalar variable.. Thanks.
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 72 views
72 views
Problem: This code generates error: IndexError: invalid index to scalar variable. at the line: results.append(RMSPE(np.expm1(y_train[testcv]), [y[1] for y in y_test])) How to fix it?
asked Jan 11 Mashhoodch 13k points
0 votes
1 answer 14 views
14 views
Problem: Does anyone grasp the problem? >Indexerror: index 2 is out of bounds for axis 0 with size 2
asked May 8 Chi Omega 21.3k points
0 votes
1 answer 4 views
4 views
Problem: Try to discover this >Integer scalar arrays can be converted to a scalar index
asked 6 days ago Sawyer Parviz 21.1k points
0 votes
1 answer 9 views
9 views
Problem: I want to write a function that randomly picks elements from a training set, based on the bin probabilities provided. I divide the set indices to 11 bins, then create custom probabilities for them. bin_probs = [0.5, 0.3, 0.15, 0.04, 0.0025, 0.0025, 0.001, 0.001, ... It is 1-D, it is integer, and it is scalar. What am I missing? Note : I tried to pass indices with astype(int). Same error.
asked May 2 anika11 32.2k points
0 votes
0 answers 5 views
5 views
Problem: I have numpy data x = [[1. 2.2 3.4] [3. 4. 5. ]] and y = [[2.6660993 3.6791213 3.7325573]] Just want to concatenate these, result should be this. [[1. 2.2 3.4] [3. 4. 5. ] [2.6660993 3.6791213 3.7325573] ] However, np.concatenate(x,y) shows the error. only integer scalar arrays can be converted to a scalar index
asked May 1 Yeamin 22k points
2 votes
2 answers 2.1K views
2.1K views
Problem: I wrote the following code in python using numpy. But it gives me some error message. The code I wrote is: import numpy as np arrayone = np.array(['Human', 'Birds', 'Groots', 'Oats']) arraytwo = np.array(['Beach', 'Toast']) arraythree ... concatenate TypeError: only integer scalar arrays can be converted to a scalar index Can anyone sort out this error and solve this. Thanks in advance.
asked May 6, 2020 samhaz 5k points
0 votes
1 answer 35 views
35 views
Problem: I know there is a ton of these threads but all of them are for very simple cases like 3x3 matrices and things of that sort and the solutions do not even begin to apply to my situation. So I'm trying to graph G versus l1 (that's not an eleven, but an ... 250 data points. I had another user (shout out to Hugh Bothwell!) help me with an error in my code, but now another error has surfaced.
asked Jan 6 Mashhoodch 13k points
1 vote
2 answers 754 views
754 views
Problem: Hello guys, Well, I am working with a NumPy array after learning the array a little bit. I am trying to print a particular element from each row from a two-dimensional array. While I am trying to run the program it returns an error Indexerror: too many indices for ... ;s wrong with this and how may I get rid of this? I am looking forward to hearing back from you guys. Thanks a lot.
asked Apr 21, 2020 Gavin 15.3k points