The Numpy Library

Selective Indexing

Conditional Indexing: How to Conditionally Select Elements in a NumPy Array?

Problem Description: You have a Numpy array. You want to select specific elements from the array. But neither slicing nor indexing seem to solve your problem. What can you do? In this short tutorial, I show you how to select specific Numpy array elements via Boolean matrices. A feature called conditional indexing or selective indexing. …

Conditional Indexing: How to Conditionally Select Elements in a NumPy Array? Read More »

NumPy Tutorial – Everything You Need to Know to Get Started

This tutorial gives you a simple introduction to Python’s NumPy library. You don’t need any prerequisites to follow the tutorial. My goal was to give a practical and fun NumPy introduction for absolute beginners and with many examples. By reading through this tutorial, you will gain a basic understanding of the most important NumPy functionality. …

NumPy Tutorial – Everything You Need to Know to Get Started Read More »

[Solved] NumPy RuntimeWarning: All-NaN slice encountered

Problem Formulation You use NumPy’s np.nanmedian() function in your code that is supposed to ignore NaN values when computing the mean of a NumPy array. But when using it, NumPy raises a RuntimeWarning: All-NaN slice encountered message: What is the reason for this warning and how to fix it? Solution + Explanation The reason this …

[Solved] NumPy RuntimeWarning: All-NaN slice encountered Read More »

How to Replace One or More List Elements at Specific Indices in Python?

Problem Formulation: Replacing One Element Given List lst Element x Index i How to replace the element at index i in the list lst with the new element x? Solution Indexing You use simple indexing using the square bracket notation lst[i] = x to replace the element at index i in list lst with the …

How to Replace One or More List Elements at Specific Indices in Python? Read More »

How to Extract Text from Images in Python using OpenCV and EasyOCR

You can extract text from images with EasyOCR, a deep learning-based OCR tool in Python. EasyOCR performs very well on invoices, handwriting, car plates, and public signs. First released in 2007, PyTesseract [1] is the to-go library for extracting text from images. It uses classical computer vision methods to perform optical character recognition (OCR), then …

How to Extract Text from Images in Python using OpenCV and EasyOCR Read More »

NumPy arange(): A Simple Illustrated Guide

The np.arange() function appears in 21% of the 35 million Github repositories that use the NumPy library! This illustrated tutorial shows you the ins and outs of the NumPy arange function. So let’s get started! What’s the NumPy Arange Function? The np.arange([start,] stop[, step]) function creates a new NumPy array with evenly-spaced integers between start …

NumPy arange(): A Simple Illustrated Guide Read More »

How to Plot Matplotlib’s Color Palette — and Choose Your Plot Color?

In this article, we’ll learn how to generate the matplotlib color palette and then we will use it to select a specific color for our plot. When presenting data, the color that you assign to a plot is very important; a bad color choice can make your data difficult to understand or even less interesting. …

How to Plot Matplotlib’s Color Palette — and Choose Your Plot Color? Read More »

Numpy Weighted Average np.average(array, axis=0, weights=[0.1,0.1,0.8])

How to Calculate the Weighted Average of a Numpy Array in Python?

Problem Formulation: How to calculate the weighted average of the elements in a NumPy array? Definition weighted average: Each array element has an associated weight. The weighted average is the sum of all array elements, properly weighted, divided by the sum of all weights. Here’s the problem exemplified: Quick solution: Before we discuss the solution …

How to Calculate the Weighted Average of a Numpy Array in Python? Read More »

How to Calculate the Average of a NumPy 2D Array?

NumPy is a popular Python library for data science focusing on arrays, vectors, and matrices. This article introduces the np.average() function from the NumPy library. When applied to a 1D array, this function returns the average of the array values. When applied to a 2D array, NumPy simply flattens the array. The result is the …

How to Calculate the Average of a NumPy 2D Array? Read More »

How to Index Elements in NumPy Arrays?

NumPy is a popular Python library for data science for array, vector, and matrix computations. This puzzle introduces basic indexing of elements in NumPy arrays. Problem Formulation: How to index elements in NumPy arrays? Indexing 1D Arrays with Positive Indices The most simple use of indexing is with the square bracket notation and positive integers: …

How to Index Elements in NumPy Arrays? Read More »

How to Calculate the Standard Deviation in NumPy?

Problem Formulation: How to calculate the standard deviation in NumPy? Differentiations: There are many different variants of this problem: Calculate the standard deviation of a 1D array Calculate the standard deviation of a 2D array Calculate the standard deviation of a 3D array Then you can also calculate the standard deviation along an axis: Calculate …

How to Calculate the Standard Deviation in NumPy? Read More »