1.what’s numpy
numpy的核心是ndarray 对象。
numpy中的array和python中的序列(sequence)有如下不同:
1. numpy中的array是固定大小的,python中的list是可以动态变化的。numpy中改变一个ndarray的大小会新建一个新的array并删除旧的数组。
2. numpy数组的元素必须是同一数据类型。但是也有例外,数组中存放对象的话就不需要相同的size 。
3. NumPy arrays facilitate advanced mathematical and other types of operations on large numbers of data.
4. 越来越多的基于python的科学和数学包使用numpy数组,所以学习numpy是必要的。
序列大小和速度对科学计算来说很重要。numpy的处理方式:
element-by-element operations are the “default mode” when an ndarray is involved, but the element-by-element operation is speedily executed by pre-compiled C code.
即以预编译的c代码来完成各种操作。
two of NumPy’s features which are the basis of much of its power: vectorization and broadcasting.
***Vectorization
Broadcasting is the term used to describe the implicit element-by-element behavior of operations; generally speaking, in NumPy all operations (i.e., not just arithmetic operations, but logical, bit-wise, functional, etc.) behave in this implicit element-by-element fashion, i.e., they broadcast. Moreover, in the example above, a and b could be multidimensional arrays of the same shape, or a scalar and an array, or even two arrays of with different shapes. Provided that the smaller array is “expandable” to the shape of the larger in such a way that the resulting broadcast is unambiguous (for detailed “rules” of broadcasting see numpy.doc.broadcasting).
NumPy
数据类型
There are 5 basic numerical types representing booleans (bool), integers (int), unsigned integers (uint) floating point (float) and complex. Those with numbers in their name indicate the bitsize of the type (i.e. how many bits are needed to represent a single value in memory). Some types, such as int and intp, have differing bitsizes, dependent on the platforms (e.g. 32-bit vs. 64-bit machines). This
import numpy as np
x = np.float32(1.0)
x 1.0
y = np.int_([1,2,4])
y
array([1, 2, 4])
z = np.arange(3, dtype=np.uint8)
z
array([0, 1, 2], dtype=uint8)
To convert the type of an array, use the .astype() method (preferred) or the type itself as a function
NumPy knows that int refers to np.int_, bool means np.bool_, that float is np.float_ and complex is np.complex_. The other data-types do not have Python equivalents
Numpy generally returns elements of arrays as array scalars (a scalar with an associated dtype). Array scalars differ from Python scalars, but for the most part they can be used interchangeably
The primary advantage of using array scalars is that they preserve the array type (Python may not have a matching scalar type available, e.g. int16). Therefore, the use of array scalars ensures identical behaviour between arrays and scalars, irrespective of whether the value is inside an array or not. NumPy scalars also have many of the same methods arrays do.
There are 5 general mechanisms for creating arrays:
1. Conversion from other Python structures (e.g., lists, tuples)
2. Intrinsic numpy array array creation objects (e.g., arange, ones, zeros, etc.)
3. Reading arrays from disk, either from standard or custom formats
4. Creating arrays from raw bytes through the use of strings or buffers
5. Use of special library functions (e.g., random)
使用array()函数把python中的array-like structrue(list, tuple)转化为array类型。