Introduction to NumPy Arrays
Numpy arrays are a very good substitute for python lists. They are better than python lists as they provide better speed and takes less memory space. For those who are unaware of what numpy arrays are, let’s begin with its definition. These are a special kind of data structure. They are basically multi-dimensional matrices or lists of fixed size with similar kind of elements.
A typical array function looks something like this:
numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)
Here, all attributes other than objects are optional. So, do not worry even if you do not understand a lot about other parameters.
- Object: Specify the object for which you want an array
- Dtype: Specify the desired data type of the array
- Copy: Specify if you want the array to be copied or not
- Order: Specify the order of memory creation
- Subok: Specify if you want a sub-class or a base-class type array
- Ndmin: Specify the dimensions of an array
Attributes of an Array
An array has the following six main attributes:
- Size: The total number of elements in an array
- Shape: The shape of an array
- Dimension: The dimension or rank of an array
- Dtype: Data type of an array
- Itemsize: Size of each element of an array in bytes
- Nbytes: Total size of an array in bytes
Example of NumPy Arrays
Now, we will take the help of an example to understand the different attributes of an array.
Example #1 – To Illustrate the Attributes of an Array
import numpy as np #creating an array to understand its attributes A = np.array([[1,2,3],[1,2,3],[1,2,3]]) print("Array A is:\n",A) #type of array print("Type:", type(A)) #Shape of array print("Shape:", A.shape) #no. of dimensions print("Rank:", A.ndim) #size of array print("Size:", A.size) #type of each element in the array print("Element type:", A.dtype)
How to Create an Array in NumPy?
Numpy provides us with several built-in functions to create and work with arrays from scratch. An array can be created using the following functions:
- ndarray(shape, type): Creates an array of the given shape with random numbers
- array(array_object): Creates an array of the given shape from the list or tuple
- zeros(shape): Creates an array of the given shape with all zeros
- ones(shape): Creates an array of the given shape with all ones
- full(shape,array_object, dtype): Create an array of the given shape with complex numbers
- arange(range): Creates an array with the specified range
Example #2 – Creation of a NumPy Array
import numpy as np #creating array using ndarray A = np.ndarray(shape=(2,2), dtype=float) print("Array with random values:\n", A) # Creating array from list B = np.array([[1, 2, 3], [4, 5, 6]]) print ("Array created with list:\n", B) # Creating array from tuple C = np.array((1 , 2, 3)) print ("Array created with tuple:\n", C)
# Creating array with all ones D = np.ones((3, 3)) print ("Array with all ones:\n", D) # Creating array with all zeros E = np.zeros((3, 3)) print ("Array with all zeroes:\n",E) # Creating an array with complex data type F = np.full((3, 3), 1, dtype = 'complex') print ("Array of complex data type:\n", F) #creating an array with buffer G = np.ndarray((2,), buffer=np.array([1,2,3]),dtype=int) print ("Array with buffer specified:\n", G) #creating an array with range H = np.arange(10) print ("Array with range specified:\n", H)
How to Access Array Elements in NumPy?
We can access elements of an array by using their indices. We can take the help of the following examples to understand it better.
Example #3 – Element Accessing in a 2D Array
import numpy as np #creating an array to understand indexing A = np.array([[1,2,1],[7,5,3],[9,4,8]]) print("Array A is:\n",A) #accessing elements at any given indices B = A[[0, 1, 2], [0, 1, 2]] print ("Elements at indices (0, 0),(1, 1), (2, 2) are : \n",B) #changing the value of elements at a given index A[0,0] = 12 A[1,1] = 4 A[2,2] = 7 print("Array A after change is:\n", A)
Example #4 – Array Indices in a 3D Array
import numpy as np #creating a 3d array to understand indexing in a 3D array I = np.array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]) print("3D Array is:\n", I) print("Elements at index (0,0,1):\n", I[0,0,1]) print("Elements at index (1,0,1):\n", I[1,0,1]) #changing the value of elements at a given index I[1,0,2] = 31 print("3D Array after change is:\n", I)
Array Operation in NumPy
The example of an array operation in NumPy explained below:
Following is an example to Illustrate Element-Wise Sum and Multiplication in an Array
import numpy as np A = np.array([[1, 2, 3], [4,5,6],[7,8,9]]) B = np.array([[1, 2, 3], [4,5,6],[7,8,9]]) # adding arrays A and B print ("Element wise sum of array A and B is :\n", A + B) # multiplying arrays A and B print ("Elementwise multiplication of array A and B:\n", A*B)
Conclusion – NumPy Arrays
A NumPy array is a multidimensional list of the same type of objects. It is immensely helpful in scientific and mathematical computing. As such, they find applications in data science and machine learning.
This is a guide to NumPy Arrays. Here we discuss how to create and access array elements in numpy with examples and code implementation. You may also look at the following articles to learn more-