Numpy creating an Array
There are many routines to construct a new array using Numpy. Fundamentally, there are 5 general mechanisms for creating arrays, (read basics of array creation)
- Conversion from other Python structures (lists, tuples)
- Intrinsic numpy array creation objects ( arange, ones, zeros, etc.)
- Reading arrays from disk, either from standard or custom formats
- Creating arrays from raw bytes through the use of strings or buffers
- Use of special library functions ( random, etc.)
In this article, we are focusing a few important routines to construct new arrays.
numpy.empty
This method returns a new array of specified shape and type. The entries are not initialized in the array. The syntax of this method is,
empty(shape[, dtype, order])
Here, Shape is the shape of the empty array in int or tuple of int.
The dtype is the desired output dtype. It is Optional
This method returns a new array filled with zeros, with specified shape, dtype, and order. The syntax for this method is,
Order can be 'C' for C-style row-major array, 'F' for FORTRAN style column-major array
For example,
import numpy as np empty_arr = np.empty([3, 3], dtype = int) print(empty_arr) #Output [[ 139718313520056 43312800 139718313519992] [ 139718313519992 2318339454602921504 2314899022742180896] [7009325245101601364 8386828583731295084 80]]
numpy.zeros
This method returns a new array filled with zeros, with specified shape, dtype, and order. The syntax for this method is,
zeros(shape[, dtype, order])
For example,
import numpy as np zero_arr = np.zeros([3, 3], dtype = int) print(zero_arr) #Output [[0 0 0] [0 0 0] [0 0 0]]
OR, constructing an array filled with zeros, with user-defined dtype.
import numpy as np zero_arr = np.zeros((3,2), dtype = [('a', 'i2'), ('b', 'i2')]) print(zero_arr) #Output [[(0, 0) (0, 0)] [(0, 0) (0, 0)] [(0, 0) (0, 0)]]
numpy.ones
It is similar to numpy.zeros, except the array is filled with ones in place of zeros. The syntax is,
ones(shape[, dtype, order])
For example,
import numpy as np ones_arr = np.ones((3,2), dtype = [('a', 'i2'), ('b', 'i2')]) print(ones_arr) #Output [[(1, 1) (1, 1)] [(1, 1) (1, 1)] [(1, 1) (1, 1)]]
numpy.identity
Returns an identity array. The syntax is,
identity(n[, dtype])
For example,
import numpy as np ones_arr = np.ones(5, dtype = np.int) print(ones_arr) #Output [1 1 1 1 1]
numpy.array
This is one of the prominently used method of array construction. The syntax for this method is,
array(object[, dtype, copy, order, subok, ndmin])
If it is True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array (default).
ndmin is the int value and optional.
It specifies the minimum number of dimensions that the output array will have.
For example,
import numpy as np print(np.array([1, 2, 3])) #simple array form a list print(np.array([1, 2, 3, 5.68])) # upcasting #multi-dimensional print(np.array([[1, 2, 3],[4, 5, 6],[7, 8,9]])) print(np.array([1, 2, 3], ndmin=2)) #Output [1 2 3] [1. 2. 3. 5.68] [[1 2 3] [4 5 6] [7 8 9]] [[1 2 3]]
numpy.arange
This method returns evenly spaced values within a specified interval.
arange([start,] stop[, step,][, dtype])
Here, the start is start (by default 0) of the interval, stop is the end of the interval, step is the size of the step within the range.
For example,
import numpy as np print(np.arange(7)) # 0-7 array int print(np.arange(3.0)) # 0-3.0 array float print(np.arange(7,12)) #start 7, stop 12 print(np.arange(7,21,3)) #start 7, stop 21 step 3 #Output [0 1 2 3 4 5 6] [0. 1. 2.] [ 7 8 9 10 11] [ 7 10 13 16 19]