简介
Provides
- An array object of arbitrary homogeneous items
- Fast mathematical operations over arrays
- Linear Algebra, Fourier Transforms, Random Number Generation
- NumPy 在内部将数据存储在
连续的内存块上
它包含的每一个元素均为相同类型
意图明显… - NumPy 的算法库是用C语言编写的,操作内存时,不需要
类型检查或者其他管理操作
目的加快计算security - Numpy数组使用的内存量也小于Python内建序列
- NumPy可以针对全量数组进行复杂计算而无需Python循环
import numpy as np
my_arr= np.arange(1000 000)
my_arr= my_arr * 2 # 每个序列同时乘以2
Java 加速内存操作 : 直接利用操作系统提供的
通道和缓冲器
Chanel Java NIO :ByteBuffer MappedByteBuffer
Android 中通信 Binder机制
基础方法
- array的构建
def array(p_object, dtype=None, copy=True, order='K', subok=False, ndmin=0): # real signature unknown; restored from __doc__
"""
array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)
Parameters
----------
copy : bool, optional
If true (default), then the object is copied. Otherwise, a copy will
only be made if __array__ returns a copy, if obj is a nested sequence,
or if a copy is needed to satisfy any of the other requirements
(`dtype`, `order`, etc.).
order : {'K', 'A', 'C', 'F'}, optional
创建数组的样式,C为行方向 行优先,F为列方向 列优先,A为任意方向(默认)
Fortran Or C order
"""
- 切片
In [13]: arr1[2:4] = 5 # numpy.ndarray 数值被传送给整个切片
In [14]: rawarr[2:4] = 5 # list TypeError
区别于Python 内建列表,Numpy 数组切片是原数组的视图,任何对视图的修改都会反映在原数组上。
加速操作速度,减少内存占用。
In [36]: arr1=np.arange(10)
In [37]: arr1_slice=arr1[2:4]
In [39]: arr1_slice[0]=99
In [40]: arr1
Out[40]: array([ 0, 1, 99, 3, 4, 5, 6, 7, 8, 9])
获取数组切片的拷贝 arr1[2:4].copy()
In [48]: arr1_slice=arr1[2:4].copy()
In [49]: arr1_slice[0]=2222
In [50]: arr1_slice
Out[50]: array([2222, 3])
In [51]: arr1
Out[51]: array([ 0, 1, 99, 3, 4, 5, 6, 7, 8, 9])
copy
def copy(self, order='C'): # real signature unknown; restored from __doc__
"""
a.copy(order='C')
Return a copy of the array.
numpy.copy
numpy.copyto
Examples
--------
>>> x = np.array([[1,2,3],[4,5,6]], order='F')
>>> y = x.copy()
>>> x.fill(0)
>>> x
array([[0, 0, 0],
[0, 0, 0]])
>>> y
array([[1, 2, 3],
[4, 5, 6]])
>>> y.flags['C_CONTIGUOUS']
True
"""
pass
矩阵的切片
In [57]: arr1
Out[57]:
array([['1', '2', '3'],
['a', 'b', '3'],
['4', '5', '6'],
['7', '8', '9']], dtype='<U11')
arr1[i:j,m:n] 第i行到第j-1行 第m列到第n-1列 0开始
arr1[:2] 前两行
arr1[:,:2] 前两列
- reshape
同样是原矩阵视图
def reshape(self, shape, order='C'): # real signature unknown; restored from __doc__
"""
a.reshape(shape, order='C')
Returns an array containing the same data with a new shape.
Refer to `numpy.reshape` for full documentation.
"""
In [76]: arr2=arr1.reshape((3,4))
In [79]: arr1
Out[79]:
array([['111', '111', '3'],
['111', '111', '3'],
['111', '111', '6'],
['111', '111', '9']], dtype='<U11')
In [80]: arr2[0,:]=999
In [81]: arr2
Out[81]:
array([['999', '999', '999', '999'],
['111', '3', '111', '111'],
['6', '111', '111', '9']], dtype='<U11')
In [82]: arr1
Out[82]:
array([['999', '999', '999'],
['999', '111', '3'],
['111', '111', '6'],
['111', '111', '9']], dtype='<U11')
# 转换为一维数组
In [84]: arr1.reshape((12,))
Out[84]:
array(['999', '999', '999', '999', '111', '3', '111', '111', '6', '111',
'111', '9'], dtype='<U11')
# 转化为。。。
In [83]: arr1.reshape((1,12))
Out[83]:
array([['999', '999', '999', '999', '111', '3', '111', '111', '6', '111',
'111', '9']], dtype='<U11')
- concatenate
In [19]: np.concatenate?
Docstring:
concatenate((a1, a2, ...), axis=0, out=None)
Join a sequence of arrays along an existing axis.
Parameters
----------
a1, a2, ... : sequence of array_like
The arrays must have the same shape, except in the dimension
corresponding to `axis` (the first, by default).
axis : int, optional
The axis along which the arrays will be joined. If axis is None,
arrays are flattened before use. Default is 0.
out : ndarray, optional
If provided, the destination to place the result.
The shape must be correct, matching that of what concatenate would have returned
if no out argument were specified.
Returns
-------
res : ndarray
The concatenated array.
Notes
-----
When one or more of the arrays to be concatenated is a MaskedArray,
this function will return a MaskedArray object instead of an ndarray,
but the input masks are *not* preserved. In cases where a MaskedArray
is expected as input, use the ma.concatenate function from the masked
array module instead.
Examples
--------
>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
[3, 4],
[5, 6]])
>>> np.concatenate((a, b.T), axis=1)
array([[1, 2, 5],
[3, 4, 6]])
>>> np.concatenate((a, b), axis=None)
array([1, 2, 3, 4, 5, 6])
This function will not preserve masking of MaskedArray inputs.
>>> a = np.ma.arange(3)
>>> a[1] = np.ma.masked
>>> b = np.arange(2, 5)
>>> a
masked_array(data=[0, --, 2],
mask=[False, True, False],
fill_value=999999)
>>> b
array([2, 3, 4])
>>> np.concatenate([a, b])
masked_array(data=[0, 1, 2, 2, 3, 4],
mask=False,
fill_value=999999)
>>> np.ma.concatenate([a, b])
masked_array(data=[0, --, 2, 2, 3, 4],
mask=[False, True, False, False, False, False],
fill_value=999999)
- ma.concatenate : Concatenate function that preserves input masks.
- array_split : Split an array into multiple sub-arrays of equal or near-equal size.
- split : Split array into a list of multiple sub-arrays of equal size.
- hsplit : Split array into multiple sub-arrays horizontally (column wise)
- vsplit : Split array into multiple sub-arrays vertically (row wise)
- dsplit : Split array into multiple sub-arrays along the 3rd axis (depth).
- stack : Stack a sequence of arrays along a new axis.
- hstack : Stack arrays in sequence horizontally (column wise)
- vstack : Stack arrays in sequence vertically (row wise)
- dstack : Stack arrays in sequence depth wise (along third dimension)
- block : Assemble arrays from blocks.
flags属性
flags = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
"""Information about the memory layout of the array.
Attributes
----------
C_CONTIGUOUS (C)
F_CONTIGUOUS (F)
OWNDATA (O)
The array owns the memory it uses or borrows it from another object.
WRITEABLE (W)
The data area can be written to. Setting this to False locks the data,
making it read-only.
A view (slice, etc.) inherits WRITEABLE from its base array at creation time,
but a view of a writeable array may be subsequently locked
while the base array remains writeable.
(The opposite is not true, in that a view of a locked array may not
be made writeable.
However, currently, locking a base object does not
lock any views that already reference it, so under that circumstance
it is possible to alter the contents of a locked array via a previously
created writeable view onto it.)
Attempting to change a non-writeable array raises a RuntimeError exception.
ALIGNED (A)
The data and all elements are aligned appropriately for the hardware.
WRITEBACKIFCOPY (X)
This array is a copy of some other array.
The C-API function
PyArray_ResolveWritebackIfCopy must be called before deallocating
to the base array will be updated with the contents of this array.
UPDATEIFCOPY (U)
(Deprecated, use WRITEBACKIFCOPY) This array is a copy of some other array.
When this array is
deallocated, the base array will be updated with the contents of
this array.
Notes
-----
Only the WRITEBACKIFCOPY, UPDATEIFCOPY, WRITEABLE, and ALIGNED flags can be
changed by the user, via direct assignment to the attribute or dictionary
entry, or by calling `ndarray.setflags`.
The array flags cannot be set arbitrarily:
- UPDATEIFCOPY can only be set ``False``.
- WRITEBACKIFCOPY can only be set ``False``.
- ALIGNED can only be set ``True`` if the data is truly aligned.
- WRITEABLE can only be set ``True`` if the array owns its own memory
or the ultimate owner of the memory exposes a writeable buffer
interface or is a string.
Arrays can be both C-style and Fortran-style contiguous simultaneously.
This is clear for 1-dimensional arrays, but can also be true for higher
dimensional arrays.
Even for contiguous arrays a stride for a given dimension
``arr.strides[dim]`` may be *arbitrary* if ``arr.shape[dim] == 1``
or the array has no elements.
It does *not* generally hold that ``self.strides[-1] == self.itemsize``
for C-style contiguous arrays or ``self.strides[0] == self.itemsize`` for
Fortran-style contiguous arrays is true."""
In [2]: arr1=np.arange(15).reshape((3,5))
In [6]: arr1_slice=arr1[:2,:2]
In [8]: arr1.flags['WRITEABLE']=False # 不可更改
In [9]: arr1_slice[1,:]=99 # 修改 locking a base object does not lock any views that already reference it
In [10]: arr1_slice[0,:]=99
In [11]: arr1_slice
Out[11]:
array([[99, 99],
[99, 99]])
In [12]: arr1
Out[12]:
array([[99, 99, 2, 3, 4],
[99, 99, 7, 8, 9],
[10, 11, 12, 13, 14]])
In [13]: arr1_slice2=arr1[:2,:2] # A view (slice, etc.) inherits WRITEABLE from its base array at creation time
In [14]: arr1_slice2.flags
Out[14]:
WRITEABLE : False
In [15]: arr1_slice2[1,:]=88 # err
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-15-c6d7f816e909> in <module>
----> 1 arr1_slice2[1,:]=88
ValueError: assignment destination is read-only
广播
在不同形状的数组之间进行计算
规则: 如果对每个结尾维度(即从尾部开始的),轴长度都匹配或者长度都是1,两个二维数组就是可以兼容和广播的。之后,广播会在丢失的或长度为1的轴上进行。