Numpy.linalg.LinAlgError: Last 2 dimensions of the array must be square error occurs when you **try to perform a linear algebra operation on an array that doesn’t have the correct shape. **

To **fix** the **error, ensure that the input matrix is square.**

The main reason is that the last two dimensions of the array must be square, meaning that the number of rows must be equal to the number of columns.

In linear algebra, a square matrix is a matrix with the same number of rows and columns. Many operations, such as finding the inverse or computing eigenvalues, are defined specifically for square matrices.

For example, if you have a **3×3 array**, the last two dimensions are square, and you can perform linear algebra operations on it, but if you have a **3×4 array**, the last two dimensions are not square, and you will receive this error message.

**Flow diagram**

**Reproduce the error**

```
import numpy as np
# Creating a non-square matrix
a = np.array([[1, 2, 3], [4, 5, 6]])
# Attempting to perform a linear algebra operation
b = np.linalg.inv(a)
print(b)
```

**Output**

The error is raised because the **inv()** function, which calculates the inverse of a matrix, requires the input to be a square matrix, but a has shape (2, 3), so it is not square.

**How to fix it?**

```
import numpy as np
# Creating a square matrix
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Attempting to perform a linear algebra operation
inver = np.linalg.inv(arr)
print(inver)
```

**Output**

In this code example, arr has shape (3, 3), so it is a square matrix, and you can perform linear algebra operations on it without encountering the error.