将一个n*3的numpy和1*3的numpy数组按列合并
时间: 2023-11-29 19:05:27 浏览: 83
您可以使用numpy模块中的concatenate函数将一个n*3的numpy数组和1*3的numpy数组按列合并。具体实现方法如下:
```python
import numpy as np
# 生成一个n*3的numpy数组
arr1 = np.random.rand(5, 3)
print("arr1 shape:", arr1.shape)
print("arr1:", arr1)
# 生成一个1*3的numpy数组
arr2 = np.array([1, 2, 3]).reshape(1, -1)
print("arr2 shape:", arr2.shape)
print("arr2:", arr2)
# 使用concatenate函数按列合并
result = np.concatenate((arr1, arr2), axis=0)
print("result shape:", result.shape)
print("result:", result)
```
输出结果为:
```
arr1 shape: (5, 3)
arr1: [[0.22375957 0.30889207 0.7559323 ]
[0.30150094 0.49554708 0.63399332]
[0.78774532 0.59915793 0.13907708]
[0.21433207 0.30091976 0.52053389]
[0.36300591 0.2493658 0.66726204]]
arr2 shape: (1, 3)
arr2: [[1 2 3]]
result shape: (6, 3)
result: [[0.22375957 0.30889207 0.7559323 ]
[0.30150094 0.49554708 0.63399332]
[0.78774532 0.59915793 0.13907708]
[0.21433207 0.30091976 0.52053389]
[0.36300591 0.2493658 0.66726204]
[1. 2. 3. ]]
```
其中reshape函数用于将一维数组转换为二维数组,axis参数用于指定按列合并。
阅读全文