数组操作
更改形状
numpy.ndarray.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]]
bumpy.ndarray.flat可以将数组转化为一个一维数组,存储在内存中,并可以用for循环来遍历它
x = x.flat
print(x)
#<numpy.flatiter object at 0x7f9bbc976600>
for i in x:
print(i)
'''
1
2
3
4
5
6
7
8
'''
numpy.ndarray.flatten([order=‘C’]) 将数组的副本转换为一维数组,并返回。
order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘k’ – 元素在内存中的出现顺序。
注意:flatten函数返回的是副本,并没有改变原数组
更改形状函数
numpy.reshape(a, newshape[, order=‘C’])在不更改数据的情况下为数组赋予新的形状。
注意:reshape()函数当参数newshape = [rows,-1]时,将根据行数自动确定列数。
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]]
数组转置
numpy.transpose(a, axes=None)
numpy.ndarray.T
这两种方式是相同的
先写到这里,回头再更,先打卡啦,hhhh