1.apply_along_axis(func1d, axis, arr) Apply a function to 1-D slices along the given axis.
import numpy as np
x = np.array([
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = np.apply_along_axis(np.sum, 0, x)
print(y) # [105 110 115 120 125]
y = np.apply_along_axis(np.sum, 1, x)
print(y) # [ 65 90 115 140 165]
在对数组进行操作时,为了满足格式和计算的要求通常会改变其形状。
import numpy as np
x = np.array([1, 2, 9, 4, 5, 6, 7, 8])
print(x.shape) # (8,)
x.shape = [2, 4]
print(x)
# [[1 2 9 4]
# [5 6 7 8]]
numpy.ndarray.flat 将数组转换为一维的迭代器,可以用for访问数组每一个元素。
import numpy as np
x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = x.flat
print(y)
#
for i in y:
print(i, end=' ')
# 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
numpy.ndarray.flatten([order=‘C’]) 将数组的副本转换为一维数组,并返回。
a. order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘k’ – 元素在内存中的出现顺序。(简记)
b. order:{'C / F,'A,K},可选使用此索引顺序读取a的元素。'C’意味着以行大的C风格顺序对元素进行索引,最后一个轴索引会更改F表示以列大的Fortran样式顺序索引元素,其中第一个索引变化最快,最后一个索引变化最快。请注意,'C’和’F’选项不考虑基础数组的内存布局,仅引用轴索引的顺序.A’表示如果a为Fortran,则以类似Fortran的索引顺序读取元素在内存中连续,否则类似C的顺序。“ K”表示按照步序在内存中的顺序读取元素,但步幅为负时反转数据除外。默认情况下,使用Cindex顺序。
flatten() 函数返回的是拷贝。
numpy.ravel(a, order=‘C’) Return a contiguous flattened array.
ravel() 返回的是视图。order=F 就是拷贝
比较两者的差别
numpy.reshape(a, newshape[, order=‘C’]) 在不更改数据的情况下为数组赋予新的形状。
reshape() 函数当参数newshape = [rows,-1] 时,将根据行数自动确定列数。
import numpy as np
x = np.arange(12)
y = np.reshape(x, [3, -1])
print(y)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
y[0, 1] = 10
print(x)
# [ 0 10 2 3 4 5 6 7 8 9 10 11](改变x去reshape后y中的值,x对应元素也改变)
reshape() 函数当参数newshape = -1 时,表示将数组降为一维。
import numpy as np
x = np.random.rand(5, 5) * 10 #rand是产生随机数
x = np.around(x, 2) #函数返回指定数字的四舍五入值
print(x)
# [[6.74 8.46 6.74 5.45 1.25]
# [3.54 3.49 8.62 1.94 9.92]
# [5.03 7.22 1.6 8.7 0.43]
# [7.5 7.31 5.69 9.67 7.65]
# [1.8 9.52 2.78 5.87 4.14]]
y = x.T
print(y)
# [[6.74 3.54 5.03 7.5 1.8 ]
# [8.46 3.49 7.22 7.31 9.52]
# [6.74 8.62 1.6 5.69 2.78]
# [5.45 1.94 8.7 9.67 5.87]
# [1.25 9.92 0.43 7.65 4.14]]
y = np.transpose(x)
print(y)
# [[6.74 3.54 5.03 7.5 1.8 ]
# [8.46 3.49 7.22 7.31 9.52]
# [6.74 8.62 1.6 5.69 2.78]
# [5.45 1.94 8.7 9.67 5.87]
# [1.25 9.92 0.43 7.65 4.14]]
当创建一个数组之后,还可以给它增加一个维度,这在矩阵计算中经常会用到。
import numpy as np
x = np.array([1, 2, 9, 4, 5, 6, 7, 8])
print(x.shape) # (8,)
print(x) # [1 2 9 4 5 6 7 8]
y = x[np.newaxis, :]
print(y.shape) # (1, 8)
print(y) # [[1 2 9 4 5 6 7 8]]
y = x[:, np.newaxis]
print(y.shape) # (8, 1)
print(y)
# [[1]
# [2]
# [9]
# [4]
# [5]
# [6]
# [7]
# [8]]
参考newaxis
numpy.squeeze(a, axis=None) 从数组的形状中删除单维度条目,即把shape中为1的维度去掉。
a 表示输入的数组;
b. axis 用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错;
在机器学习和深度学习中,通常算法的结果是可以表示向量的数组(即包含两对或以上的方括号形式[[]]),如果直接利用这个数组进行画图可能显示界面为空(见后面的示例)。我们可以利用squeeze() 函数将表示向量的数组转换为秩为1的数组,这样利用 matplotlib 库函数画图时,就可以正常的显示结果了。
import numpy as np
x = np.array([[[0], [1], [2]]])
print(x.shape) # (1, 3, 1)
print(x)
# [[[0]
# [1]
# [2]]]
y = np.squeeze(x)
print(y.shape) # (3,)
print(y) # [0 1 2]
y = np.squeeze(x, axis=0)
print(y.shape) # (3, 1)
print(y)
# [[0]
# [1]
# [2]]
y = np.squeeze(x, axis=2)
print(y.shape) # (1, 3)
print(y) # [[0 1 2]]
y = np.squeeze(x, axis=1)
# ValueError: cannot select an axis to squeeze out which has size not equal to one
结论:根据上述例可知,np.squeeze()函数可以删除数组形状中的单维度条目,即把shape中为1的维度去掉,但是对非单维的维度不起作用。
numpy.concatenate((a1, a2, …), axis=0, out=None) Join a sequence of arrays along an existing axis.
连接沿现有轴的数组序列(原来x,y都是一维的,拼接后的结果也是一维的)
原来x,y都是二维的,拼接后的结果也是二维的。
import numpy as np
x = np.array([1, 2, 3]).reshape(1, 3)
y = np.array([7, 8, 9]).reshape(1, 3)
z = np.concatenate([x, y])
print(z)
# [[ 1 2 3]
# [ 7 8 9]]
z = np.concatenate([x, y], axis=0)
print(z)
# [[ 1 2 3]
# [ 7 8 9]]
z = np.concatenate([x, y], axis=1)
print(z)
# [[ 1 2 3 7 8 9]]
x,y在原来的维度上进行拼接。
import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[7, 8, 9], [10, 11, 12]])
z = np.concatenate([x, y])
print(z)
# [[ 1 2 3]
# [ 4 5 6]
# [ 7 8 9]
# [10 11 12]]
z = np.concatenate([x, y], axis=0)
print(z)
# [[ 1 2 3]
# [ 4 5 6]
# [ 7 8 9]
# [10 11 12]]
z = np.concatenate([x, y], axis=1)
print(z)
# [[ 1 2 3 7 8 9]
# [ 4 5 6 10 11 12]]
numpy.stack(arrays, axis=0, out=None) Join a sequence of arrays along a new axis.
import numpy as np
x = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.stack([x, y])
print(z.shape) # (2, 3)
print(z)
# [[1 2 3]
# [7 8 9]]
z = np.stack([x, y], axis=1)
print(z.shape) # (3, 2)
print(z)
# [[1 7]
# [2 8]
# [3 9]]
参考stack
import numpy as np
x = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.vstack((x, y))
print(z.shape) # (2, 3)
print(z)
# [[1 2 3]
# [7 8 9]]
z = np.stack([x, y])
print(z.shape) # (2, 3)
print(z)
# [[1 2 3]
# [7 8 9]]
z = np.hstack((x, y))
print(z.shape) # (6,)
print(z)
# [1 2 3 7 8 9]
z = np.concatenate((x, y))
print(z.shape) # (6,)
print(z) # [1 2 3 7 8 9]
hstack(),vstack() 分别表示水平和竖直的拼接方式。在数据维度等于1时,比较特殊。而当维度大于或等于2时,它们的作用相当于concatenate ,用于在已有轴上进行操作。
numpy.split(ary, indices_or_sections, axis=0) Split an array into multiple sub-arrays as views into ary.
import numpy as np
x = np.array([
[11, 12, 13, 14],
[16, 17, 18, 19],
[21, 22, 23, 24]])
y = np.split(x, [1, 3])
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
# [21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]
y = np.split(x, [1, 3], axis=1)
print(y)
# [array([
#[11],
# [16],
# [21]]),
#array([[12, 13],
# [17, 18],
# [22, 23]]),
#array([[14],
# [19],
# [24]])]
numpy.vsplit(ary, indices_or_sections) Split an array into multiple sub-arrays vertically (row-wise).
numpy.hsplit(ary, indices_or_sections) Split an array into multiple sub-arrays horizontally (column-wise).
numpy.tile(A, reps) Construct an array by repeating A the number of times given by reps.
tile 是瓷砖的意思,顾名思义,这个函数就是把数组像瓷砖一样铺展开来。
numpy.repeat(a, repeats, axis=None) Repeat elements of an array.
a. axis=0 ,沿着y轴复制,实际上增加了行数。
b. axis=1 ,沿着x轴复制,实际上增加了列数。
c. repeats ,可以为一个数,也可以为一个矩阵。
numpy.unique(ar, return_index=False, return_inverse=False,return_counts=False, axis=None) Find the unique elements of an array.
a. return_index:the indices of the input array that give the unique values
b. return_inverse:the indices of the unique array that reconstruct the input array
c. return_counts:the number of times each unique value comes up in the input array