2

I have a 4 x 3 system to solve using numpy linalg.solve , but numpy keeps throwing LinAlgError: 1-dimensional array given. Array must be at least two-dimensional.

Tutorials and questions available are all for square matrices, and I'm not sure how to proceed.

This is my code and matrix:

import numpy as np

A = np.array([[[[1/15, 1/15, 2/19],
            1/15, 2/15, 4,19],
            2/15, 2/15, 4/19],
            2/15, 2/15, 4/19])

B = np.array([0.1144807411, 0.1262803853, 0.1234210927, 0.130977131])

C = np.linalg.solve(A, B)

print(C)

Where did it go wrong?

3 Answers 3

2

Like hpaulj already said, your A is wrong. But even if you will write it in the correct syntax, you can use np.linalg.solve only for the case your matrix A is a square matrix (means shape (n,n)) and has full rank. Because your matrix A has shape (4,3) np.linalg.solve will raise an LinAlgError, so you need to use np.linalg.lstsq instead:

import numpy as np

A = np.array([[1/15, 1/15, 2/19], [1/15, 2/15, 4/19], [2/15, 2/15, 4/19], [2/15, 2/15, 4/19]])
B = np.array([0.1144807411, 0.1262803853, 0.1234210927, 0.130977131])
C = np.linalg.lstsq(A, B, rcond=None)[0]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much. I think passing rcond=None does not work in Numpy v1.14. This worked when rcond is not passed or set to -1, as Charles encountered here.
1

Look at your A. Don't just assume you wrote it right.

In [387]: A = np.array([[[[1/15, 1/15, 2/19],
     ...:             1/15, 2/15, 4,19],
     ...:             2/15, 2/15, 4/19],
     ...:             2/15, 2/15, 4/19])
     ...: 
     ...:             
In [388]: A
Out[388]: 
array([list([[[0.06666666666666667, 0.06666666666666667, 0.10526315789473684], 0.06666666666666667, 0.13333333333333333, 4, 19], 0.13333333333333333, 0.13333333333333333, 0.21052631578947367]),
       0.13333333333333333, 0.13333333333333333, 0.21052631578947367],
      dtype=object)
In [389]: A.shape
Out[389]: (4,)
In [390]: A.dtype
Out[390]: dtype('O')

In [391]: A[0]
Out[391]: 
[[[0.06666666666666667, 0.06666666666666667, 0.10526315789473684],
  0.06666666666666667,
  0.13333333333333333,
  4,
  19],
 0.13333333333333333,
 0.13333333333333333,
 0.21052631578947367]
In [392]: A[1]
Out[392]: 0.13333333333333333

1 Comment

thank you for pointing this out - I've been careless enough not to learn how to check my data
0

As @hpaulj points out, your matrix 'A' is not 4x3. If you initialize a new all zero 4x3 array with numpy like this:

z = np.zeros((4, 3)

You will see:

[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

Note the placement of the brackets. Now compare to your array 'A'.

You also have a critical typo in your array. 4, 19 should be 4/19.

Here it is fixed:

import sys
import numpy as np


print(np.__version__)
print(sys.version)

A = np.array([[1/15, 1/15, 2/19],
              [1/15, 2/15, 4/19],
              [2/15, 2/15, 4/19],
              [2/15, 2/15, 4/19]])

print(f'Matrix dimensions: ', {A.shape})
print(f'Matrix size: ', {A.size})

Which results in:

1.15.0
3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
Matrix dimensions:  {(4, 3)}
Matrix size:  {12}

Process finished with exit code 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.