NumPy: Arrays - Overview NumPy (Numerical Python) is a ...
NumPy: Arrays - Overview ? NumPy (Numerical Python) is a scientific package for Python ? The primary object it addresses is the ND array (class ndarray) ? Like most other languages, NumPy arrays are homogeneous: they hold values
of the same data type ? NumPy has its own data types (that correspond to standard Python data
types) ? The standard Python data types can be used interchangeably with the NumPy data types
? To use NumPy, the standard usage is import numpy as np
? A dimension is referred to as an axis
1
NumPy: Arrays - Creation
? There are numerous ways to create an array in NumPy
1. Using a constructor ? Syntax: np.array(shape [comma separated option list]) (array is an alias for ndarray) ? Shape can take the form of A tuple of dimensions; e.g., (2, 3) An array literal; e.g. [ [7, 3, 5], [3, 9, 6] ] ? The data type is inferred from the values provided in the argument to the constructor; by default the type is float64 The type can be explicitly indicated using the dtype = type option: np.ndarray( [ [7, 3, 5], [3, 9, 6] ], dtype = int16)
2. Using specialized constructors ? zeros: Creates an array of zeroes ? ones: Creates an array of ones ? empty: Creates an an empty array whose values are undefined ? eye(n): Creates an identity matrix of dimensions n ? n ? identity(n): Same as eye() ? full(shape, fill value): Creates an array of specified dimensions all of whose elements are fill value
3. Using the arange function ? Syntax: arange(start value, end value, step) ? Create a 1D array (vector) whose first element is start value, last element is end value, with intermediate elements start value+step, start value+ 2 step, etc. ? When using floats, it is recommended that linspace() is used instead Its third argument specifies the number of elements in the array, rather than the step size
? Reshaping
? The reshape method allows changing the dimensions of an array Syntax: ndarray.reshape(dimension tuple)
? The ravel method flattens an array Syntax: ndarray.ravel()
? Both of the above return copies and do not affect the original
2
NumPy: Arrays - Accessing Elements ? Single array elements are indicated by a single pair of square brackets enclosing
a comma-separated list of indices ? Slicing works the same as for other Python sequences
>>> a array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729]) >>> a[2] 8 >>> a[2:5] array([ 8, 27, 64]) >>> b array([[ 0, 1, 2, 3],
[10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33], [40, 41, 42, 43]]) >>> b[2,3] 23 >>> b[0:5, 1] array([ 1, 11, 21, 31, 41]) >>> b[ : ,1] array([ 1, 11, 21, 31, 41]) >>> b[1:3, : ] array([[10, 11, 12, 13], [20, 21, 22, 23]])
3
NumPy: Arrays - Display ? For 1D and 2D arrays, the display is obvious:
>>> v = np.array([1, 2, 3]) >>> v array([1, 2, 3] >>> m = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) >>> m array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8]]
? For nD arrays, maybe not so:
>>> a = np.array([[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], [[13, 1 >>> a array([[[ 1, 2, 3, 4],
[ 5, 6, 7, 8], [ 9, 10, 11, 12]],
[[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24]]])
? This creates a 2 ? 3 ? 4 array ? The outer level represents the number of rows (there are 2); the next the
number of columns (3), and the innermost the depth (4) ? The first row, first column has the values 1, 2, 3, 4 from front to back ? As displayed, the top block of values represents the top plane of the array,
the second the bottom plane ? If each plane were rotated 90 degrees, it would more accurately reflect the
3D nature of the array; i.e., the first column of the top plane holds the values 1 - 4, the second column the values 5 - 8, and the third column 9 - 12
4
NumPy: Arrays - Attributes ? ndarray.ndim the number of axes (dimensions) of the array. ? ndarray.shape the actual dimensions of the array. This is a tuple of integers
indicating the size of the array in each dimension. ? ndarray.size the total number of elements of the array. This is equal to the
product of the elements of shape. ? ndarray.dtype an object describing the type of the elements in the array.
5
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- numpy array declaration in python
- numpy arrays overview numpy numerical python is a
- python for data science cheat sheet lists also see
- numpy cbse board array
- numpy arrays
- an introduction to numpy and scipy
- legate numpy accelerated and distributed array
- python for data science cheat sheet lists also see numpy
- introduction chapter to numpy
Related searches
- python what is a method
- what is a python notebook
- numpy arrays matrix multiplication
- stack numpy arrays different sizes
- python is a string
- python check if is a number
- python is string a number
- what is a python docstring
- concatenate numpy arrays python
- python check if input is a number
- python check if variable is a list
- what is a python wheel