30 Quick Numpy Tips and Tricks for Beginners

Ayushi Trivedi Last Updated : 05 May, 2024
8 min read

Introduction

This guide focuses on mastering Python and NumPy, a powerful library for numerical computing. It offers 30 tips and tricks to enhance coding skills, covering foundational matrix operations and advanced statistical analysis techniques. Practical examples accompany each tip, allowing users to navigate complex data manipulations and scientific computations. The guide aims to unlock the full potential of Python and NumPy and discover Numpy tips and tricks for Python.

Numpy Tips and Tricks for Python

You can also enroll in our free python course today!

Numpy Tips and Tricks for Python

Here are 30 useful NumPy tricks covering various functionalities:

1. Create a Matrix of Zeros

A useful utility in the NumPy library for Python numerical computing is the numpy.zeros function. It produces an array that has just zeros in it. This function comes in handy when you need to initialize an array with zeros of a certain data type and form.

Generate a matrix of zeros with a specified shape:

import numpy as np
zeros_matrix = np.zeros((3, 3))

This line creates a 3×3 matrix filled with zeros using the np.zeros() function.

  • np.zeros() is a NumPy function that generates an array filled with zeros.
  • (3, 3) inside the function call specifies the shape of the array as a tuple (rows, columns). So, in this case, it creates a 3×3 matrix.

2. Create a Matrix of Ones

Developers usually create a ones matrix using NumPy’s np.ones() method, which generates an array containing elements that all have the value 1. This matrix finds frequent use in data processing and mathematical computations as it helps initialize matrices with a preset shape.

Generate a matrix of ones with a specified shape:

ones_matrix = np.ones((3, 3))

This line of code initializes a 3×3 matrix filled with elements having a value of 1 using NumPy’s np.ones() function. The resulting matrix is assigned to the variable ones_matrix, which can be used for further computations or processing.

3. Identity Matrix

We know a square matrix as an identity matrix, or 𝐼𝑛In, where every diagonal member is 1 and every non-diagonal element is 0.

Generate an identity matrix:

identity_matrix = np.eye(3)

This line of code creates a 3×3 identity matrix using NumPy’s np.eye() function. We assign the resulting matrix to the variable identity_matrix.

4. Generate a Range of Numbers

A NumPy function called np.arange() creates an array with entries uniformly spaced within a specified interval.

Create an array of numbers inside a certain range that are uniformly spaced:

numbers = np.arange(0, 10, 2)

This code creates a NumPy array called numbers with values ranging from 0 to 8 (inclusive) for each of its two iterations.

5. Random Array Generation

The np.random.rand() function generates random numbers from a uniform distribution over the interval [0, 1). One random float is produced when the function is invoked without any parameters.

Generate an array of random numbers:

random_array = np.random.rand(3, 3)

Using np.random.rand(), this line creates a 3×3 NumPy array full of random values drawn from a uniform distribution between 0 and 1. The variable random_array is given the resultant array.

6. Reshape an Array

NumPy’s reshape() method lets you modify an array’s shape without altering its contents. As long as the total number of elements is constant, it allows you to transform an array between different shapes.

Reshape an array to a different shape:

reshaped_array = np.arange(9).reshape(3, 3)

This line of code creates a 3×3 NumPy array named reshaped_array.

Using np.arange(9), it first creates a 1D array with values ranging from 0 to 8. We then use the reshape() function to transform this 1D array into a 3×3 2D array.

7. Transpose a Matrix

To transpose an array or matrix in NumPy, use the transpose() method. A view of the array with the axes reversed is returned.

Transpose a matrix:

transposed_matrix = np.transpose(matrix)

This line uses NumPy’s np.transpose() function to transpose a matrix stored in the variable matrix. The transposed matrix is then assigned to the variable transposed_matrix.

8. Extract Diagonal Elements

The NumPy method np.diag() takes a square matrix as input and returns its diagonal members as an array. Alternatively, it takes a 1D array as input and creates a diagonal matrix using those elements.

Get diagonal elements of a matrix:

diagonal_elements = np.diag(matrix)

This line of code extracts the diagonal elements from a matrix using NumPy’s np.diag() function and assigns them to the variable diagonal_elements.

9. Concatenate Arrays

NumPy’s np.concatenate() function takes arrays and joins them along a given axis. After receiving two or more arrays as input, it concatenates them using the given axis and outputs the finished array.

Concatenate arrays horizontally or vertically:

concatenated_array = np.concatenate((array1, array2), axis=0)

This line gives the result to concatenated_array after concatenating two arrays, array1 and array2, along the rows (axis=0).

10. Stack Arrays

NumPy’s np.stack() function moves arrays along a new axis for stacking. It creates a new axis by stacking an array sequence along the designated axis.

Stack arrays along different axes:

stacked_array = np.stack((array1, array2), axis=1)

This line allocates the result to stacked_array and stacks arrays 1 and 2 horizontally (along columns).

11. Indexing and Slicing

Access elements using indexing and slicing:

element = array[0, 0]
sub_array = array[:, 1:3]

These lines take one element out of the array at (0, 0) and assign it to element. Additionally, they extract and assign to sub_array a sub-array that contains every row and column from index 1 through index 3, excluding index 3.

12. Broadcasting

Perform operations between arrays of different shapes:

result = array1 + 5

This code creates a new array called result by adding 5 to each entry in array 1.

13. Vectorization

Each element in an array has its sine calculated by NumPy’s np.sin() function. Each element in the new array is the sine of its corresponding element in the input array, and it has the same shape as the original array.

This is known as vectorization because it allows mathematical operations to be applied to entire arrays (or vectors) at once, rather than iterating over individual elements.

Utilize vectorized operations for faster computation:

result = np.sin(array)

Using NumPy’s np.sin() method, each element in the array is sine-calculated in this line, and the result is assigned to the variable result.

14. Find Unique Elements

NumPy’s np.unique() method takes an array, eliminates duplicates, and returns the unique elements sorted in order.

Find unique elements in an array:

unique_elements = np.unique(array)

This code searches the array array for unique elements, assigning them to the unique_elements variable. After eliminating redundant elements, it yields a sorted array with just the unique values.

15. Filtering

Filter elements based on conditions:

filtered_array = array[array > 0]

This piece of code creates a new array called filtered_array by filtering the items of the array array by choosing only those that are greater than 0. To retrieve elements that meet the criteria array > 0, it employs boolean indexing.

16. Element-wise Functions

Each element in an array is squared by NumPy’s np.square() method, which returns a new array containing the squared values.

Apply functions element-wise:

squared_array = np.square(array)

By calculating the square of each element in the array array, this line of code creates a new array called squared_array that holds the squared values.

17. Matrix Operations

NumPy’s np.dot() method calculates the dot product of two arrays, which may be matrices or vectors. It computes the scalar product for vectors and matrix multiplication for matrices.

Perform matrix operations like dot product:

dot_product = np.dot(matrix1, matrix2)

This line creates a new array called dot_product by computing the dot product of matrix 1 and matrix 2.

18. Statistics

When calculating the arithmetic mean (average) of an array’s elements, NumPy’s np.mean() method returns a single result that represents the average of the array’s elements.

Calculate statistics like mean, median, etc.:

mean_value = np.mean(array)

This line determines the mean (average) value of each member in the array ‘array’, and then stores the result in the variable ‘mean_value’.

19. Save and Load

A NumPy function called np.save() is used to store arrays in a binary format to disk. The array to be saved and the filename (including the.npy suffix) are the two inputs it requires. This function effectively saves the array data to disk while maintaining the data types and form of the array.

A NumPy function called np.load() is used to load arrays from files that have previously been stored using np.save(). It returns the array contained in that file after receiving the filename as an input. You can interact with the data as though it were loaded straight from memory by using this function to recreate the array in memory.

Together, these functions provide a convenient way to save and load NumPy arrays, facilitating data persistence and reuse.

Save and load arrays to/from files:

np.save('array.npy', array)
loaded_array = np.load('array.npy')

This code uses NumPy’s np.save() function to save the array array to a file called “array.npy.” It then loads the saved array back into memory and assigns it to the variable loaded_array using np.load().

20. Interpolation

Using known data points, the NumPy np.interp() function uses linear interpolation to estimate values within a range. It returns an array of interpolated values depending on the query points and data points supplied.

Interpolate values in an array:

interpolated_values = np.interp(x_values, xp, fp)

Using the xp and fp arrays, this line of code interpolates the x_values linearly. Based on the linear interpolation between the values in xp and fp, it returns an array of interpolated values matching to x_values.

21. Linear Algebra

The NumPy function np.linalg.eig() calculates a square matrix’s eigenvalues and eigenvectors. The input matrix’s eigenvalues and matching eigenvectors are the two arrays that are returned.

Perform linear algebraic operations:

eigenvalues, eigenvectors = np.linalg.eig(matrix)

This piece of code uses NumPy’s np.linalg.eig() function to calculate the square matrix matrix’s eigenvalues and eigenvectors. It gives back two arrays: eigenvalues, which contain the matrix’s eigenvalues, and eigenvectors, which contain the matching eigenvectors.

22. Cumulative Sum

The NumPy np.cumsum() function calculates the cumulative sum of the elements in an array along a given axis. Each entry in the returned array represents the cumulative total of all the entries up to that index along the given axis.

Calculate cumulative sum along a specified axis:

cumulative_sum = np.cumsum(array, axis=0)

This line creates a new array called cumulative_sum by calculating the cumulative sum along the rows (axis=0) of the array array.

23. Sort Array Elements

The NumPy np.sort() function arranges an array’s items along a given axis in ascending order. The old array is left intact and is replaced with a new array that has the sorted elements in it.

Sort elements of an array:

sorted_array = np.sort(array)

This line assigns the result to the variable sorted_array and sorts the array array’s members in ascending order.

24. Element-wise Comparison

In NumPy, when comparing two arrays for equality, the np.array_equal() method returns True if their shapes and elements match, and False otherwise.

Perform element-wise comparison between arrays:

comparison_result = np.array_equal(array1, array2)

This line checks if two arrays, array1 and array2, are equal. If they are, it returns True; if not, it returns False. The variable comparison_result is allocated the result.

25. Repeating Elements

NumPy’s np.tile() function multiplies an array’s size by copying its contents along given dimensions. It requires a tuple indicating the number of repeats along each dimension in addition to the array to be tiled.

Repeat elements of an array:

repeated_array = np.tile(array, (2, 2))

By tiling or repeating the contents of array twice along both dimensions, this line creates a new array called repeated_array, which is larger than the original array.

26. Set Operations

When you union two arrays using NumPy’s np.union1d() function, it returns a new array with unique elements from both input arrays.

Perform set operations like union, intersection, etc.:

union = np.union1d(array1, array2)

This line adds the result to the variable union after calculating the union of the elements in arrays 1 and 2. Unique elements from either array1 or array2 are present in the resulting array.

27. Histogram

The NumPy function np.histogram() calculates an array’s histogram, which shows the frequency distribution of values inside designated bins. The line returns two arrays: the bin edges and the histogram values, or frequencies.

Compute histogram of array values:

hist, bins = np.histogram(array, bins=10)

In this line, we compute the histogram of the array ‘array’ with 10 bins, resulting in two arrays: ‘bins’, which contains the bin edges, and ‘hist’, which contains the frequencies of values in each bin.

28. Find Max and Min

NumPy routines called np.max() and np.min() are used to find the largest and least values in an array, respectively. Without taking into account any NaN values, they return the array’s highest and lowest values.

Find maximum and minimum values in an array:

max_value = np.max(array)
min_value = np.min(array)

These lines determine the array array’s maximum and minimum values, respectively, then assign them to the variables max_value and min_value.

29. Compute Dot Product

NumPy’s np.dot() function calculates the dot product of two arrays. It computes the dot product, also known as the scalar product, of vectors in 1-D arrays. It carries out matrix multiplication for 2-D arrays.

Compute dot product of vectors:

dot_product = np.dot(vector1, vector2)

The dot product of vectors 1 and 2 is calculated on this line, and the result is a scalar value that is assigned to the variable dot_product.

30. Broadcast to Compatible Shapes

The NumPy function np.broadcast_to() broadcasts an array’s contents to a given shape and creates a new array with that form. To fill the new shape, it repeats the elements in the input array as needed.

Automatically broadcast arrays to compatible shapes:

broadcasted_array = np.broadcast_to(array, (3, 3))

By broadcasting the contents of array to a larger form (3, 3), this line produces a new array called broadcasted_array. This indicates that the larger form will be filled by repeating the items of the array.

Conclusion

This guide provides 30 Numpy tips and tricks for Python, enhancing coding skills in numerical computing. It covers creating matrices, performing array manipulations, and conducting advanced statistical analysis. This knowledge equips users to tackle diverse challenges in data science, machine learning, and scientific research. The key to success lies in continuous learning and exploration. Embrace the power of Python and NumPy, and let your coding journey flourish.

You can also enroll in our free python course today!

My name is Ayushi Trivedi. I am a B. Tech graduate. I have 3 years of experience working as an educator and content editor. I have worked with various python libraries, like numpy, pandas, seaborn, matplotlib, scikit, imblearn, linear regression and many more. I am also an author. My first book named #turning25 has been published and is available on amazon and flipkart. Here, I am technical content editor at Analytics Vidhya. I feel proud and happy to be AVian. I have a great team to work with. I love building the bridge between the technology and the learner.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details