更改形状
在对数组进行操作时,为了满足格式和计算的要求通常会改变其形状。
numpy.ndarray.shape表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 ndim 属性(秩)。
【例】通过修改 shape 属性来改变数组的形状。
import numpy as np
x=np.array([1,2,3,4,5,6,7,8])
print(x.shape) # (8,)
x.shape=[2,4]
print(x)
'''
[[1 2 3 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)
# <numpy.flatiter object at 0x0000026582447C10>
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
y[3]=0
print(end='\n')
print(x)
'''
[[11 12 13 0 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’]) 将数组的副本转换为一维数组,并返回。
order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘k’ – 元素在内存中的出现顺序。(简记)
order:{'C / F,'A,K},可选使用此索引顺序读取a的元素。'C’意味着以行大的C风格顺序对元素进行索引,最后一个轴索引会更改F表示以列大的Fortran样式顺序索引元素,其中第一个索引变化最快,最后一个索引变化最快。请注意,'C’和’F’选项不考虑基础数组的内存布局,仅引用轴索引的顺序.A’表示如果a为Fortran,则以类似Fortran的索引顺序读取元素在内存中连续,否则类似C的顺序。“ K”表示按照步序在内存中的顺序读取元素,但步幅为负时反转数据除外。默认情况下,使用Cindex顺序。
【例】flatten()函数返回的是拷贝。
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.flatten()
print(y)
'''
[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[3]=0
print(x)
'''
[[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]]
'''
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.flatten(order='F')
print(y)
# [11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 30
# 35]
y[3] = 0
print(x)
# [[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]]
【例】ravel()返回的是视图。
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.ravel(x)
print(y)
'''
[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[3]=0
print(x)
'''
[[11 12 13 0 15]
[16 17 18 19 20]
[21 22 23 24 25]
[26 27 28 29 30]
[31 32 33 34 35]]
'''
【例】order=F 就是拷贝
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.ravel(x, order='F')
print(y)
# [11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 30
# 35]
y[3] = 0
print(x)
# [[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]]
reshape()函数当参数newshape = [rows,-1]时,将根据行数自动确定列数。
import numpy as np
x=np.arange(12)
y=np.reshape(x,[3,4])
print(y.dtype) # int32
print(y)
'''
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]'''
y=np.reshape(x,[3,-1])
print(y)
'''
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]'''
y=np.reshape(x,[-1,3])
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.randint(12,size=[2,2,3])
print(x)
'''
[[[7 5 5]
[7 3 9]]
[[4 6 9]
[2 8 4]]]'''
y=np.reshape(x,-1)
print(y)
# [7 5 5 7 3 9 4 6 9 2 8 4]
数组转置
import numpy as np
x=np.array([
[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15]])
print(x)
'''
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]]'''
y=x.T
print(y)
'''
[[ 1 6 11]
[ 2 7 12]
[ 3 8 13]
[ 4 9 14]
[ 5 10 15]]'''
z=np.transpose(x)
print(z)
'''
[[ 1 6 11]
[ 2 7 12]
[ 3 8 13]
[ 4 9 14]
[ 5 10 15]]'''
更改维度
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)
y = x[:, np.newaxis]
print(y.shape) # (8, 1)
print(y)
'''
[[1]
[2]
[9]
[4]
[5]
[6]
[7]
[8]]'''