Updated March 21, 2023
Overview of NumPy Array Functions
In python, we do not have built-in support for the array data type. But do not worry; we can still create arrays in python by converting python structures like lists and tuples into arrays or by using intrinsic numpy array creation objects like arrange, ones, zeros, etc. This particular post will discuss intrinsic numpy array functions to create and work with arrays.
Array Creation: Numpy provides us with several built-in functions to create and work with arrays from scratch. A typical numpy array function for creating an array 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
Examples of NumPy Array Creation
Below are the different examples of NumPy Array Functions:
Example #1 – Array Creation using np.array() Function
Code:
#importing numpy
import numpy as np
#creating an array a
a = np.array( [[ 1, 2, 3, 4],
[ 5, 6, 7,8],
[9,10,11,12]]
)
#printing array a
print ("Array is:",a)
#we can also print the other attributes like dimensions,shape and size of an array
print ("Dimensions of a are:", a.ndim)
print ("Shape of a is", a.shape)
print ("Size of a is", a.size)
Output:
Example #2 – Creating an Empty Array using empty_like Function
Empty_like function returns an empty array with shape and type as input.
np.empty(shape, dtype)
Code:
import numpy as np
#creating an array an empty square array of dimensions 2X2
empty_array = np.empty([2,2], dtype = int)
#np.empty() creates an array with random values
print ("Array is:", empty_array)
Output:
Example #3 – Creating an Array with Zeros using zero_like Function
Zero_like function returns an array of zeros with shape and type as input.
np.zeros(shape,dtype)
Code:
import numpy as np
#creating an array a zeros square array of dimensions 2X2
zeros_array = np.zeros([2,2], dtype = int)
print ("Array is:", zeros_array)
Output:
There are few other similar functions for creating arrays like ones_like, full_like, eye(), arange() np.asarray(), etc.
Examples of Array Manipulation
Following are the different examples of an array manipulation in NumPy Array Functions:
Example #1 – Copying from One Array to Another
We can copy content from one array to another using the copyto function.
np.copyto(destination, source)
Code:
import numpy as np
#creating an array a zeros square array of dimensions 2X2
zeros_array = np.zeros([2,2], dtype = int)
print ("Array zeros is:", zeros_array)
ones_array = np.ones([2,2], dtype = int)
print ("Array ones is :", ones_array)
#copying content from ones_array to zeros
np.copyto(zeros_array,ones_array)
print ("New zeros array :", zeros_array)
Output:
Example 2 – Changing the Shape of an Array
Reshape changes the shape of an array without changing the data in it.
np.reshape(object, shape)
Code:
import numpy as np
#creating an array a 1D array
a = np.array([[1,2],[3,4]])
print ("array a is :", a)
#changing the shape of array from 2D to 1D
print ("reshape array a is:",np.reshape(a,4))
Output:
Example 3 – Transposing an Array
Transpose_like array functions help in transposing the array.
ndarray.T
Code:
import numpy as np
#creating an array a 1D array
a = np.array([[1,2],[3,4]])
print ("array a is :", a)
#transposing array a using array.T
print ("transposed array a is:", a.T)
Output:
We can also use,
np.moveaxis(a, source, destination), np.rollaxis(a, axis) and np.swapaxes(a, axis1, axis2) to transpose an array.
Example 4 – Joining Two or More Arrays
Concatenate function helps in joining two or more array along the given axis.
np.concatenate((a1, a2, ...), axis=0, out=None)
Code:
import numpy as np
#creating two arrays a and b
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
#joining a and b vertically
print ("concatenated array vertically:", np.concatenate((a, b), axis=0))
#joining a and b horizontally
print ("concatenated array horizontally:", np.concatenate((a, b), axis=None))
Output:
Other numpy array functions such as np.stack(array, axis) and np.block(array1,array2, etc) can also be used to join two or more arrays together along the desired axes.
Example #5 – Splitting an Array Into Multiple Sub-Arrays
The split function helps splitting an array into multiple sub-arrays of equal or near-equal size.
np.split(array, indices)
Code:
import numpy as np
#creating an array using arange function.
a = np.arange(8)
print (a)
#splitting array a into 4 equal parts
print ("sub-parts of array a:", np.split(a, 4))
Output:
There are few other functions like hsplit(array,index), vsplit(array,index), array_split(array,index,axis) that can be employed to perform the similar task.
Example 6 – Adding Elements to an Existing Array
We can use np.insert(array, index, value) to insert values along the given axis before the given indices. But, if we want to add values at the end of the array, we can use,
np.append(array, value, axis = 0)
Code:
import numpy as np
#creating an array using arange function.
a = np.array([[1,2,3],[1,2,3]])
print ("array a is :", a)
#inserting elements along the y axis at index 1
print ("array a after insertion :", np.insert(a,1,5, axis = 1))
Output:
Example 7 – Deleting the Elements from an Array
Delete function can be used to delete an axis of the given array and returns a new array with sub-arrays along the deleted axis.
np.delete(array, object, axis)
Code:
import numpy as np
#creating an array using arange function.
a = np.array([[1,2,3],[1,2,3]])
print ("array a is :", a)
#deleting elements
print ("array a after deletion :", np.delete(a,[1,2,3], axis = 0))
Output:
Example 8 – Rotating the Elements of an Array by 90 Degrees
We can use np.rot90() to rotate an array by 90 degrees in the plane specified by axes.
np.rot90(array, no_of_times_to_rotate, axes)
Code:
import numpy as np
#creating an array using arange function.
a = np.array([[1,2],[3,4]])
print ("array a is :", a)
#rotating elements by 90 degrees once along (1,0)
print ("rotated array is :",np.rot90(a,1,(1,0)))
Output:
Conclusion
In this post, we have discussed some basic and commonly used array functions. There are many other versions of these functions. If you are curious to earn more about them, keep experimenting with the discussed functions along with different arrays, axes, shapes, and indices.
Recommended Articles
This is a guide to NumPy Array Functions. Here we discuss the overview and various examples of array creation and array manipulation in NumPy Array Functions. You may also look at the following articles to learn more –