- 矩阵全体减去列向量或者行向量会自动拓展,arrary类型需要多一步reshape
a = np.mat([[1,2,3],[4,5,6]])
b = np.mean(a,1) #b为一个列向量列表
print(b)
print(a-b)
a = np.array([[1,2,3],[4,5,6]])
average = np.mean(a,axis=1)
# 向量拓展为矩阵
average.shape = (average.size,1)
print(a-average) #[[-1. 0. 1.],[-1. 0. 1.]]
-
轴的问题
numpy根据序号列表取出行数
a = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
b = a[[0,2],:] # b = a[[0,2],] b = a[[0,2]]
print(b)
-
hstack,vstack
numpy的array数据类型乘以一个bool矩阵
x = np.array([1,-1,2])
y = np.array([2,3,4])
print((x>0)*y)
结果为[2 0 4]
,(x>0)为一个跟x一样的bool矩阵,乘的时候,True当作1,False当作0
检查所有元素都为真np.all
b1 == 0得到一个bool矩阵,大小跟b1一样,np.all再对bool矩阵进行判断。
np.all(b1 == 0)broadcast经典例子
np.array([3, 6, 1])
只会跟三维的相乘
def convert_to_grey_scale(image):
""" Change image to gray scale
Args:
image: numpy array of shape(image_height, image_width, 3)
Returns:
out: numpy array of shape(image_height, image_width, 1)
"""
out = None
### YOUR CODE HERE
out = np.sum(image * np.array([3, 6, 1]) / 10, axis=2)
print(out.shape)
### END YOUR CODE
return out
- view_as_blocks
见其函数说明可以知道,在计算HOG的时候用到
Examples
--------
>>> import numpy as np
>>> from skimage.util.shape import view_as_blocks
>>> A = np.arange(4*4).reshape(4,4)
>>> A
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
>>> B = view_as_blocks(A, block_shape=(2, 2))
>>> B[0, 0]
array([[0, 1],
[4, 5]])
>>> B[0, 1]
array([[2, 3],
[6, 7]])
>>> B[1, 0, 1, 1]
13