arr_a = np.arange(8).reshape(2, 4) arr_b= np.random.randint(1, 10, size = 4),通过python将arr_a和arr_b转换成矩阵,计算矩阵的矢量积和数量积。
时间: 2023-05-31 21:05:45 浏览: 103
import numpy as np
arr_a = np.arange(8).reshape(2, 4)
arr_b = np.random.randint(1, 10, size=4)
# 转换为矩阵
mat_a = np.matrix(arr_a)
mat_b = np.matrix(arr_b)
# 计算矢量积
vec_product = np.dot(mat_a, mat_b.T)
print("矢量积:")
print(vec_product)
# 计算数量积
num_product = np.dot(mat_a.flatten(), mat_b.T.flatten())
print("数量积:")
print(num_product)
相关问题
arr_x = np.array([1, 5, 7]) arr_y = np.array([2, 6, 8]) arr_con = np.array([True, False, True]) arr_res=np.where( , , ) 补齐参数,使得结果arr_res=[1,6,8]
arr_x = np.array([1, 5, 7])
arr_y = np.array([2, 6, 8])
arr_con = np.array([True, False, True])
arr_res=np.where(arr_con, arr_x, arr_y)
# 结果为 [1, 6, 8]
arr = np.arange(20).reshape(4,5)
这是一个创建一个 4 行 5 列的 NumPy 数组,其中元素从 0 到 19。你可以使用以下代码创建它:
```python
import numpy as np
arr = np.arange(20).reshape(4,5)
```
这将返回以下数组:
```
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
```