# [Solved] LinAlgError: Last 2 dimensions of the array must be square

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error LinAlgError: Last 2 dimensions of the array must be square in python. So Here I am Explain to you all the possible solutions here.

## How LinAlgError: Last 2 dimensions of the array must be square Error Occurs?

Today I get the following error LinAlgError: Last 2 dimensions of the array must be square in python.

## How To Solve LinAlgError: Last 2 dimensions of the array must be square Error ?

1. How To Solve LinAlgError: Last 2 dimensions of the array must be square Error ?

To Solve LinAlgError: Last 2 dimensions of the array must be square Error A square matrix is a matrix with the same number of rows and columns. The matrix you are doing is a 3 by 2. Add a column of zeroes to fix this problem.

2. LinAlgError: Last 2 dimensions of the array must be square

To Solve LinAlgError: Last 2 dimensions of the array must be square Error A square matrix is a matrix with the same number of rows and columns. The matrix you are doing is a 3 by 2. Add a column of zeroes to fix this problem.

## Solution 1

In case you still haven’t found an answer, or in case someone in the future has this question.

To solve Ax=b:

numpy.linalg.solve uses LAPACK gesv. As mentioned in the documentation of LAPACK, gesv requires A to be square:

LA_GESV computes the solution to a real or complex linear system of equations AX = B, where A is a square matrix and X and B are rectangular matrices or vectors. Gaussian elimination with row interchanges is used to factor A as A = PL*U , where P is a permutation matrix, L is unit lower triangular, and U is upper triangular. The factored form of A is then used to solve the above system.

If A matrix is not square, it means that you either have more variables than your equations or the other way around. In these situations, you can have the cases of no solution or infinite number of solutions. What determines the solution space is the rank of the matrix compared to the number of columns. Therefore, you first have to check the rank of the matrix.

That being said, you can use another method to solve your system of linear equations. I suggest having a look at factorization methods like LU or QR or even SVD. In LAPACK you can use `getrs`, in Python you can different things:

• first do the factorization like QR and then feed the resulting matrices to a method like `scipy.linalg.solve_triangular`
• solve the least-squares using `numpy.linalg.lstsq`

## Solution 2

A square matrix is a matrix with the same number of rows and columns. The matrix you are doing is a 3 by 2. Add a column of zeroes to fix this problem.

## Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.