1 N维数组
NumPy使用ndarray作为N维数组类型,来描述相同类型的元素的“集合”。元素的类型是相同的,占用相同的内存大小。元素可以通过索引提取对应元素的Python对象。
2 ndarray类
class numpy.ndarray(shape,
dtype=float,
buffer=None,
offset=0,
strides=None,
order=None)
2.1 参数(Parameters)
- shape: tuple of ints,创造的数组的形状大小
- dtype: data-type, optional,元素的数据类型
- buffer: object exposing buffer interface, optional,缓冲区数据接口,用于填充数组
- offset: int, optional,缓冲区中数组数据向后偏移的字节数
- strides: tuple of ints, optional,内存中数据的移动
- order: {‘C’, ‘F’}, optional,C 行优先,F 列优先
>>>np.arange(24).reshape(3,8)
>array([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22, 23]])
# shape参数, buffer参数
>>>x = np.ndarray((3,2),
buffer=np.arange(24).reshape(3,8),
)
>>>x
>array([[0, 1],
[2, 3],
[4, 5]])
# dtype参数
>>>x = np.ndarray((3,2),
buffer=np.arange(24).reshape(3,8),
dtype=float
)
>>>x.dtype
>dtype('float64')
# order参数
>>>x = np.ndarray((3,2),
buffer=np.arange(24).reshape(3,8),
order="C"
)
>>>x
>array([[0, 1],
[2, 3],
[4, 5]])
>>>x = np.ndarray((3,2),
buffer=np.arange(24).reshape(3,8),
order="F"
)
>>>x
>array([[0, 3],
[1, 4],
[2, 5]])
# offset参数
>>>x = np.ndarray((3,2),
buffer=np.arange(24).reshape(3,8),
offset=8,
order="C"
)
>>>x
>array([[2, 3],
[4, 5],
[6, 7]])
# strides参数
>>>x = np.ndarray((3,2),
buffer=np.arange(24).reshape(3,8),
strides=(16,16),
order="C"
)
>>>x
>array([[ 0, 4],
[ 4, 8],
[ 8, 12]])
2.2 属性(Attributes)
- obj.T: ndarray类型,返回转置数组
- obj.data: buffer,返回数组开始地址
- obj.dtype: 数据类型对象,数组元素的数据类型
- obj.flags: dict类型,数组内存布局相关信息
- obj.flat: 迭代器,返回数组的一维迭代器
- obj.imag: ndarray类型,返回数组的虚部
- obj.real: ndarray类型,返回数组的实部
- obj.size: int类型,返回元素个数
- obj.itemsize: int类型,返回一个元素的字节长度
- obj.nbytes: int类型,全部元素的字节长度
- obj.ndim: int类型,返回数组维度
- obj.shaoe: tuple of ints,返回维度的元组
- obj.strides: 遍历数组时每个维度中步进的字节数元组
- obj.ctypes: 一个简化数组与ctypes模块交互的对象。
- obj.base: ndarray类型,如果数组来源于其它对象,则返回原数组,即buffer指向的数组
2.3 方法(Methods)
obj.all(axis=None, out=None, keepdims=False, *, where=True)
全部元素为True,返回True
axis为默认值None,全部元素执行and操作,axis为int or tuple,按照对应轴执行and操作;
out备用输出数组,与预期输出数组形状相同,并保持自身的数据类型不变。
keepdims为True时保持数组的维度,缩小是轴保留尺寸为1。obj.any(axis=None, out=None, keepdims=False, *, where=True)
任一元素为True,返回Trueobj.argmax(axis=None, out=None, keepdims=False)
返回最大值的下标obj.argmax(axis=None, out=None, keepdims=False)
返回最小值的下标obj.argsort(axis)
返回排序后的下标数组obj.astype(dtype)
复制并返回一个指定数据类型的数组obj.clip(min, max,out)
返回数组,将元素值限定在一定范围内obj.compress(condition, axis, out
返回给定轴组成的数组obj.cumprod(axis)
返回指定轴累乘的数组obj.cumsum(axis)
返回指定轴累加的数组obj.copy()
返回复制的数组obj.fill(value)
使用指定的value值填充数组obj.flatten()
将数组展开为一维数组并返回obj.getfield(dtype)
以特定类型返回给定数组的字段obj.item(*args)
返回数组指定位置的元素值obj.itemset(position, value)
将value插入到数组指定位置obj.max(axis=None, out=None, keepdims=False, *, where=True)
返回指定维度的最大值obj.mean(axis=None, out=None, keepdims=False, *, where=True)
返回指定维度的平均值obj.min(axis=None, out=None, keepdims=False, *, where=True)
返回指定维度的最小值obj.nonzero()
返回数组中非零元素的索引obj.prod(axis)
返回指定轴元素的乘积obj.ptp(axis)
返回指定轴元素最大值减最小值的结果obj.put(indices,value)
给数组指定位置设置指定值obj.ravel()
返回展开为一维的数组obj.repeat(repeats, axis)
重复数组元素obj.reshape(shape)
返回一个新的数组,重新设定形状obj.resize(new_shape)
返回一个新尺寸的数组obj.round()
返回四舍五入的结果obj.searchsorted(v)
返回元素在有序数组中应该插入的位置obj.sort(axis)
数组原地排序obj.std(axis, dtype)
返回标准差obj.sum(axis, dtype)
返回和obj.trace()
返回数组对角元素和obj.transpose()
返回数组的转置obj.std(axis, dtype)
返回数组方差