1. Single array iteration
>>> a = (6).reshape(2,3) >>> for x in (a): ... print x, ... 0 1 2 3 4 5
That is, the default is row-major order (or C-order), and the purpose of such iterative traversal is to achieve consistency with the memory distribution pattern in order to improve accessibility;
>>> for x in (): ... print x, ... 0 1 2 3 4 5 >>> for x in ((order='C')): ... print x, ... 0 3 1 4 2 5
That is, the traversal of a and performs the order of agreement, i.e., the order in which they are actually stored in memory.
2. Control of traversal order
for x in (a, order='F'):Fortran order,i.e., columnar order first; for x in (, order='C'):C order,i.e., line order first.;
3. Modifying the values of elements in an array
By default, nditer will treat the array to be iteratively traversed as read-only. In order to traverse the array while making changes to the array elements, you must specify read-write or write-only mode.
>>> a array([[0, 1, 2], [3, 4, 5]]) >>> for x in (a, op_flags=['readwrite']): ... x[...] = 2 * x ... >>> a array([[ 0, 2, 4], [ 6, 8, 10]])
4. Use of external loops
Shifting the innermost loop of a dimension to an outer loop iterator makes numpy's vectorization operations more efficient when working with larger data.
>>> a = (6).reshape(2,3) >>> for x in (a, flags=['external_loop']): ... print x, ... [0 1 2 3 4 5] >>> >>> for x in (a, flags=['external_loop'], order='F'): ... print x, ... [0 3] [1 4] [2 5]
5. Tracking a single index or multiple indexes (multi-index)
>>> a = (6).reshape(2,3) >>> a array([[0, 1, 2], [3, 4, 5]]) >>> it = (a, flags=['f_index']) >>> while not : ... print "%d <%d>" % (it[0], ), ... () ... 0 <0> 1 <2> 2 <4> 3 <1> 4 <3> 5 <5> # Index number,prioritize by columnar order
>>> it = (a, flags=['multi_index']) >>> while not : ... print "%d <%s>" % (it[0], it.multi_index), ... () ... 0 <(0, 0)> 1 <(0, 1)> 2 <(0, 2)> 3 <(1, 0)> 4 <(1, 1)> 5 <(1, 2)>
Supplementary: detailed explanations
Vectors, matrices & multidimensional arrays are essential tools in numerical computation; by batch processing of array data, explicit looping over array elements is avoided, which results in concise, more maintainable code, and allows the use of lower-level libraries to implement array operations. As a result, vectorized computations are much faster compared to sequential element-by-element computations.
In the Python scientific computing environment, the Numpy library provides efficient data structures for working with arrays, and the core of Numpy, which is implemented in C, provides a number of functions for working with and manipulating arrays.
NumPy supports a wider variety of number types than Python, with five basic number types:
Boolean (bool)
Integer (int)
Unsigned integer (uint)
Floating point (float)
Complex
The core of the Numpy library is the representation of homogeneous multidimensional data - each element occupies a block of memory of the same size, and all blocks are interpreted in exactly the same way. How each element of an array is interpreted is specified by a separate data type object, one of which is associated with each array. In addition to basic types (integers, floats, etc.), datatype objects can also represent data structures.
1, create Numpy array
NumPy provides an N-dimensional array type, ndarray, which describes a collection of "items" of the same type. Items can be indexed using, for example, N integers. Items extracted from an array (e.g., by indexing) are represented by Python objects of one of the array scalar types constructed in NumPy. Array scalars allow easy manipulation of more complex arrangements of data.
Difference between ndarray and array
is just a convenience function to create an ndarray, it is not a class itself.
ndarray arrays, which are objects of class ndarray objects.
So ndarray is a class object and array is a method.
There are five conventional mechanisms for creating arrays:
Conversion from other Python structures (e.g., lists, tuples)
numpy native array creation (e.g., range, ones, zeros, etc.)
Reading arrays from disk, either in standard or custom formats
Creating arrays from raw bytes by using strings or buffers
Using special library functions (e.g., random)
1、
An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. The dimensions and the number of items in the array are defined by its shape, which is a tuple (tuple) of N non-negative integers that specify the size of each dimension. The type of the items in the array is specified by a separate data-type object (dtype), one of which is associated with each ndarray.
As with other container objects in Python, the contents of an ndarray can be accessed and modified by indexing or slicing the array (e.g., using N integers) and through the ndarray's methods and properties.
Unlike ndarrays, ndarrays can share the same data, so changes made in one ndarray may be visible in another. That is, an ndarray can be the "view" of another ndarray, and the data it refers to is processed by the "base" ndarray. ndarrays can also be views of Python-owned memory strings or objects that implement the buffer or array interfaces.
Created by () & ()
# Create an array. (object, dtype=None, copy=True, order='K', subok=False, ndmin=0) (shape, dtype=float, buffer=None, offset=0, strides=None, order=None)
An ndarray is a multidimensional container of items of the same type and size.
The dimensions and the number of items in the array are defined by its shape, which is a tuple (tuple) of N non-negative integers that specify the size of each dimension.
The difference is that ndarrays can share the same data, so changes made in one ndarray may be visible in another. That is, an ndarray can be the "view" of another ndarray, and the data it refers to is processed by the "base" ndarray. ndarrays can also be views of Python's own in-memory strings or objects that implement the buffer or array interfaces.
Examples:
>>> ([1, 2, 3]) array([1, 2, 3]) >>> ([1, 2, 3.0]) array([ 1., 2., 3.]) >>> ([[1, 2], [3, 4]]) array([[1, 2], [3, 4]]) >>> ([1, 2, 3], ndmin=2) array([[1, 2, 3]]) >>> ([1, 2, 3], dtype=complex) array([ 1.+, 2.+, 3.+]) >>> x = ([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')]) >>> x['a'] array([1, 3]) >>> (('1 2; 3 4')) array([[1, 2], [3, 4]]) >>> (('1 2; 3 4'), subok=True) matrix([[1, 2], [3, 4]]) >>> (shape=(2,2), dtype=float, order='F') array([[ -1.13698227e+002, 4.25087011e-303], [ 2.88528414e-306, 3.27025015e-309]]) #random >>> ((2,), buffer=([1,2,3]), offset=np.int_().itemsize, dtype=int) # offset = 1 * itemsize, . skip first element array([2, 3])
2. Basic Properties
Array properties reflect information inherent in the array itself. Typically, accessing an array through its properties allows you to get and sometimes set the internal properties of an array without having to create a new array. The publicly available properties are the core part of the array, and only some properties can be meaningfully reset without creating a new array. Information about each property follows.
memory layout
The following properties contain information about the memory layout of the array:
methodologies trace (a drawing) narrate | | Information about the memory layout of arrays。 | | Tuples of array dimensions。 | | Byte tuples in each dimension when traversing an array。 | | array dimension。 | | PythonThe buffer object points to the beginning of the data in the array.。 | | Number of elements in the array。 | | The length of an array element,byte-based。 | | Total bytes consumed by array elements。 | | If the memory comes from another object,then the base object。
data type
The datatype object associated with this array can be found in the dtype attribute :
methodologies | trace (a drawing) narrate | | Data types of array elements。
Other Properties
methodologies | trace (a drawing) narrate | | Transpose Array。 | | The real part of the array。 | | Imaginary part of an array。 | | One-dimensional iterators over arrays。 | | A simplified array withctypesObjects for module interaction。
3, Numpy native array create ndarray
methodologies | trace (a drawing) narrate | eye(N[, M, k, dtype, order]) | Returns a two-dimensional array,On the diagonal there is a,Zero elsewhere. | identity(n[, dtype]) | Returns an array of identifiers。 | ones(shape[, dtype, order]) | Returns a new array of the given shape and type,and filled with1 | ones_like(a[, dtype, order, subok, shape]) | Returns an array of the same shape and type as the given array.。 | zeros(shape[, dtype, order]) | Returns a new array of the given shape and type,and filled with zeros。 | zeros_like(a[, dtype, order, subok, shape]) | Returns an array of zeros of the same shape and type as the given array.。 | full(shape, fill_value[, dtype, order]) | Returns a new array of the given shape and type,amalgamatefill_valuepadding | full_like(a, fill_value[, dtype, order, …]) | Returns a complete array of the same shape and type as the given array | empty(shape[, dtype, order]) | Returns a new array of the given shape and type,without the need to initialize the entry | empty_like(prototype[, dtype, order, subok, …]) | Returns a new array with the same shape and type as the given array
Functions with _like() such as zeros_like(), ones_like(), empty_like(), etc. create arrays of the same shape and type as the argument array.
Functions such as frombuffer(), fromstring(), and fromfile() create arrays from byte sequences or files.
4、
| methodologies | trace (a drawing) narrate | arange([start,] stop[, step,][, dtype]) | Returns the value of the uniform interval within the given interval。 | linspace(start, stop[, num, endpoint, …]) | Returns equally spaced numbers in the specified interval。 | logspace(start, stop[, num, endpoint, base, …]) | Returns are uniformly distributed on a logarithmic scale。 | geomspace(start, stop[, num, endpoint, …]) | Returns the number in logarithmic scale(exponential increase)uniform distribution。 | meshgrid(*xi, **kwargs) | Returns a coordinate matrix from a coordinate vector。 | mgridnd_grid | an actual example,It returns a dense multidimensional “meshgrid” | ogridnd_grid | an actual example,It returns an open multidimensional “meshgrid”
2. Created from existing data
methodologies trace (a drawing) narrate | array(object[, dtype, copy, order, subok, ndmin]) | Creating an Array | asarray(a[, dtype, order]) | Converting inputs to arrays | asanyarray(a[, dtype, order]) | Converts the input tondarray,harmlessndarraysubcategories | ascontiguousarray(a[, dtype]) | Returns a contiguous array in memory(ndim > = 1)(Csequences) | asmatrix(data[, dtype]) | Interpret the input as a matrix | copy(a[, order]) | Returns an array copy of the given object | frombuffer(buffer[, dtype, count, offset]) | Interpreting buffers as one-dimensional arrays | fromfile(file[, dtype, count, sep, offset]) | Constructing an array from data in a text or binary file | fromfunction(function, shape, **kwargs) | Construct an array by executing a function at each coordinate | fromiter(iterable, dtype[, count]) | Creating a new one-dimensional array from an iterable object | fromstring(string[, dtype, count, sep]) | A new one-dimensional array initialized with text data from a string. | loadtxt(fname[, dtype, comments, delimiter, …]) | Loading data from a text file
3. Creating the matrix
methodologies | trace (a drawing) narrate | mat(data[, dtype]) | Interpret the input as a matrix | bmat(obj[, ldict, gdict]) | From String,Nested sequences or arrays to build matrix objects | tril(m[, k]) | The lower triangle of the array。 | triu(m[, k]) | The upper triangle of the array。 | vander(x[, N, increasing]) | Generating the van der Merwe matrix | diag(v[, k]) | Extracting diagonals or constructing diagonal arrays。 | diagflat(v[, k]) | Creating 2D arrays using flattened inputs as diagonals。 | tri(N[, M, k, dtype]) | Arrays at and below a given diagonal and zero at other locations。
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more. If there is any mistake or something that has not been fully considered, please do not hesitate to give me advice.