NumPy Array Creation
Numpy provides many functions to create arrays. For Example:
a = np.array([1, 2, 3, 4, 5, 6, 7])
Note that the np.array() takes only one input parameter, that is a list of elements. If you want to create a multi-dimensional array, then pass tuples of numbers on the list. For example:
b = np.array([(1,2,3), (4,5,6)])
The type of the array can also be explicitly specified at creation time:
c = np.array( [ [1,2], [3,4] ], dtype=complex )
Apart from array method, we have some predefined functions that will generate a Numpy array, if the size of the array is known.
import numpy as np a = np.zeros((2,2)) # Create an array of all zeros print(a) # Prints "[[ 0. 0.] # [ 0. 0.]]" b = np.ones((1,2)) # Create an array of all ones print(b) # Prints "[[ 1. 1.]]" c = np.full((2,2), 7) # Create a constant array print(c) # Prints "[[ 7. 7.] # [ 7. 7.]]" d = np.eye(2) # Create a 2x2 identity matrix print(d) # Prints "[[ 1. 0.] # [ 0. 1.]]" e = np.random.random((2,2)) # Create an array filled with random values print(e)
To create sequences of numbers, NumPy provides a function analogous to the range that returns arrays instead of lists. It is arange() method.
numpy.arange(start, stop, step, dtype)
import numpy as np x = np.arange(5) print(x) #[0 1 2 3 4]
There is another function called linespace().This function is similar to arange() function. In this function, instead of step size, the number of evenly spaced values between the interval is specified
numpy.linspace(start, stop, num, endpoint, retstep, dtype)
import numpy as np x = np.linspace(10,20,5) print(x) #[10. 12.5 15. 17.5 20.]
Numpy array from existing data
This function is useful for converting Python sequence into ndarray. Given below is the proper format to write asarray.
numpy.asarray(a, dtype = None, order = None)
Lets learn more about the parameters, a is input data or sequence in any form such as list, list of tuples, tuples, tuple of tuples or tuple of lists. Then dtype is useful to specify the data type of the final ndarray but by default, the data type of input data is applied to the resultant ndarray. Order parameter has 2 values C (row major) or F (column major), C is the default.
# convert list to ndarray import numpy as np x = [1,2,3] a = np.asarray(x) print(a)
We can perform basic arithmetic operations on any two numpy arrays. Basic operations like addition, subtraction, multiplication and division can be done using both +, -, *, / symbols or add(), subtract(), multiply(), divide() methods. We can also find the square root of each element in numpy array by using sqrt() function.
import numpy as np x = np.array([[1,2],[3,4]], dtype=np.float64) y = np.array([[5,6],[7,8]], dtype=np.float64) # Elementwise sum; both produce the array # [[ 6.0 8.0] # [10.0 12.0]] print(x + y) print(np.add(x, y)) # Elementwise difference; both produce the array # [[-4.0 -4.0] # [-4.0 -4.0]] print(x - y) print(np.subtract(x, y)) # Elementwise product; both produce the array # [[ 5.0 12.0] # [21.0 32.0]] print(x * y) print(np.multiply(x, y)) # Elementwise division; both produce the array # [[ 0.2 0.33333333] # [ 0.42857143 0.5 ]] print(x / y) print(np.divide(x, y)) # Elementwise square root; produces the array # [[ 1. 1.41421356] # [ 1.73205081 2. ]] print(np.sqrt(x))
n-dimension array can be treated as n-order matrix. We can perform matrix operations on these numpy array matrix such as calculating dot product, transpose of a matrix, etc.
import numpy as np v = np.array([9,10]) w = np.array([11, 12]) # Inner product of vectors; both produce 219 print(v.dot(w)) print(np.dot(v, w)) x = np.array([[1,2], [3,4]]) print(x) # Prints "[[1 2] # [3 4]]" print(x.T) # Prints "[[1 3] # [2 4]]"
We can also calculate the sum of an entire row or column in a numpy array matrix by using sum() function and specifying the axis along which sum has to be computed. Similarly using min() we can calculate minimum value in each row or column
import numpy as np x = np.array([[1,2],[3,4]]) print(np.sum(x)) # Compute sum of all elements; prints "10" print(np.sum(x, axis=0)) # Compute sum of each column; prints "[4 6]" print(np.sum(x, axis=1)) # Compute sum of each row; prints "[3 7]" print(np.min(axis=1)) # Compute min of each row
Complete list of mathematical functions that can be performed on numpy arrays
Here are the links to tutorials to get started with basics of Python.