Basic Installation Guide (Windows)
Open CMD and type:
pip install numpy
… and done. That’s it.
Just write down the following command:
import numpy as np
Here, ‘np’ is just a shorthand convention in place of numpy which is widely adopted in programming community.
Python List Vs. NumPy Array
Python list can contain different data types within a single list, all of the elements in a NumPy array should be homogeneous. This facilitates for all the mathematical operations meant to be performed using numpy.
- NumPy arrays are faster and more compact than Python lists.
- NumPy uses much less memory to store data and it provides a mechanism of specifying the data types. This allows the code to be optimized even further.
1D Array, 2D Array & ND array
ndarray class is used to represent both matrices and vectors.
- A vector is an array with a single dimension (there’s no difference between row and column vectors).
- A matrix refers to an array with two dimensions. For 3-D or higher dimensional arrays, the term tensor is also commonly used.
In NumPy, dimensions are called axes.
# One dimensional array has 1 axis
arr = np.array([1,2,4,5,2,3,8])
# Two dimensional array has 2 axes
arr2 = np.array([[45,48,45,12],[78,15,6,25]])
Creating An Array In NumPy
- np.array( )
- np.zeros( )
- np.ones( )
- np.empty( )
# Here, output is random
- np.arange( start, end, [step] )
array([2, 3, 4, 5, 6, 7, 8, 9])
array([2, 5, 8])
- np.linspace( )
>>> np.linspace(2,10,num = 3) array([ 2., 6., 10.])
- Specifying your data type
While the default data type is floating point (
np.float64), you can explicitly specify which data type you want using the
>>> a = np.array([1,5,78.2], dtype=np.int64)
# Output : <class 'numpy.int64'>
# Output : 78
You can quickly sort an array in ascending order using np.sort( )
>>> arr = np.array([12,24,5467,5])
array([ 5, 12, 24, 5467])
In addition to np.sort(), you can see all the methods that can be applied here.
You can concatenate arrays with
>>> a = np.array([1, 2, 3, 4, 5])
>>> b = np.array([5, 6, 7, 8])
>>> np.concatenate((a, b))
array([1, 2, 3, 4, 5, 5, 6, 7, 8])
To see more about concatenation, click here,
Shape and Size of an Array
>>> arr = np.array([[[0, 1, 2, 3], [4, 5, 6, 7]], [[0, 1, 2, 3], [4, 5, 6, 7]], [[0 ,1 ,2, 3], [4, 5, 6, 7]]])
# Number of axis / dimension
# Output : 3
# Total number of elements in array
# Output : 24
# Displays a tuple of integers indicating the number of elements stored along each dimension of array
# Output : (3, 2, 4)
Reshaping an Array
arr.reshape() will give a new shape to an array without changing the data. Just remember that when you use the reshape method, the array you want to produce needs to have the same number of elements as the original array. If you start with an array with 12 elements, you’ll need to make sure that your new array also has a total of 12 elements.
>>> a = np.array([4,8,5,6,7,2])
array([4, 8, 5, 6, 7, 2])
array([[4, 8], [5, 6], [7, 2]])
ValueError: cannot reshape array of size 6 into shape (3,3)
For more information on shape manipulation, click here.