>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 3, 4])
>>> np.stack((a, b))
array([[1, 2, 3],
[2, 3, 4]])
>>>
>>> np.stack((a, b), axis=-1)
array([[1, 2],
[2, 3],
[3, 4]])
axis=0 将a,b垂直方向拼接
axis=1 将a,b水平方向拼接
2.
array[n-1,:] 取数组的第n行
array[:,n-1] 取数组的第n列
array[:,0:3] 取数组的1到3列
np.ones((n, 1)) 生成一个n行1列的矩阵,数组元素全是1,用在T@point_cloud(np.hstack来造矩阵,满足矩阵相乘条件)
3. np.array和np.matrix的差别, np.array([1,2,3])这种不是矩阵, np.array([[1, 2, 3]]) 和 np.matrix(1, 2, 3) 才是矩阵,有shape
np.array和np.matrix的差别
4. 列表和数组相互转换
>>> c
array([[1, 2, 3],
[4, 5, 6]])
>>> c.tolist()
[[1, 2, 3], [4, 5, 6]]
>>> c.shape
(2, 3)
>>> np.array(c.tolist())
array([[1, 2, 3],
[4, 5, 6]])
6. np.argsort
可以给一个二维矩阵,按照某一列的大小做一个排序
>>> a = np.array([[3, 3, 3], [1, 1, 1], [2, 2, 2]])
>>> a
array([[3, 3, 3],
[1, 1, 1],
[2, 2, 2]])
#按照第二列大小做排序
>>> a[np.argsort(a[:, 2])]
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
7. array升一个维度
>>> a = np.array([1 ,2, 3])
>>> a
array([1, 2, 3])
>>> a[None, :]
array([[1, 2, 3]])
>>> a[:, None]
array([[1],
[2],
[3]])
8. np.savetxt()—将array保存到txt文件
9. 参考:Python squeeze()函数
作用:移除数组中维度为1的维度
函数形式:np.squeeze(a, axis=None)
参数:axis: 选择数组中的某一维度移除, 如果选择形状输入大于1的轴,则会引发错误。
>>> x = np.array([[[0], [1], [2]]])
>>> x.shape
(1, 3, 1)
>>> np.squeeze(x)
array([0, 1, 2])
>>> np.squeeze(x).shape
(3,)
>>> np.squeeze(x, axis = 0).shape
(3, 1)
>>> np.squeeze(x, axis = 0)
array([[0],
[1],
[2]])
>>> np.squeeze(x, axis = 1).shape
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<__array_function__ internals>", line 6, in squeeze
File "/home/moonx/miniconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py", line 1438, in squeeze
return squeeze(axis=axis)
ValueError: cannot select an axis to squeeze out which has size not equal to one
>>> np.squeeze(x, axis = 2).shape
(1, 3)
>>> np.squeeze(x, axis = 2)
array([[0, 1, 2]])
>>>