'numpy.ndarray' object has no attribute 'linalg'
时间: 2023-09-19 12:09:23 浏览: 281
This error occurs when you try to use a method or attribute that is not defined for a numpy array. In this case, it seems that you are trying to use the 'linalg' attribute on a numpy array, but this attribute is not defined for numpy arrays.
The 'linalg' attribute is defined for numpy matrices, not for numpy arrays. If you want to use linear algebra functions on a numpy array, you can convert it into a matrix using the 'numpy.matrix' function, like this:
```
import numpy as np
arr = np.array([[1, 2], [3, 4]])
mat = np.matrix(arr)
det = np.linalg.det(mat)
```
In this example, we first create a numpy array 'arr'. We then convert it into a numpy matrix 'mat' using the 'numpy.matrix' function. Finally, we use the 'numpy.linalg.det' function to calculate the determinant of the matrix 'mat'.
Alternatively, you can use the 'numpy.linalg' module directly on the numpy array, like this:
```
import numpy as np
arr = np.array([[1, 2], [3, 4]])
det = np.linalg.det(arr)
```
In this example, we use the 'numpy.linalg.det' function directly on the numpy array 'arr'.
阅读全文