How to Initialize a NumPy Array? 6 Easy Ways

Problem Formulation and Solution Overview In this article, you’ll learn how to initialize a NumPy array in Python using six (6) of the most commonly used methods. Background: NumPy is Python’s impressive array-based data structure library used to perform intense mathematical calculations popularized by the Machine Learning and Data Science community. Let’s start by creating … Read more

np.argsort() — A Simpe Illustrated Guide

In Python, the numpy.argsort() function returns the indices that would sort an array in ascending order.  Here is the argument table of the numpy.argsort() function. If it sounds great to you, please continue reading, and you will fully understand the numpy.argsort() function through Python code snippets and vivid visualization. This tutorial is about numpy.argsort() function.  … Read more

np.gradient() — A Simple Illustrated Guide

In Python, the numpy.gradient() function approximates the gradient of an N-dimensional array. It uses the second-order accurate central differences in the interior points and either first or second-order accurate one-sided differences at the boundaries for gradient approximation. The returned gradient hence has the same shape as the input array.  Here is the argument table of … Read more

How to Call an Element from a Numpy Array?

[toc] Problem: Given a Numpy array; how will you call an element from the given array? Example: When you call an element from a Numpy array, the element being referenced is retrieved from a specified index. Let’s have a look at the following scenario, which demonstrates the concept: Given: my_array = [[1, 2, 3, 4, … Read more

Sample a Random Number from a Probability Distribution in Python

Problem Formulation Challenge: Given a list. How will you select a number randomly from the list using probability distribution? When you select a number randomly from a list using a given probability distribution, the output number generated will be a number returned based on the relative weights (probability) of the given numbers. Let’s try to … Read more

Matplotlib Color Palette

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. Problem and Solution Overview Problem: When presenting data, the color that you assign to a plot is very important; a bad color choice can make your data difficult to … Read more

How to Divide Each Element in a List in Python

Summary: The most Pythonic approach to divide each element in a list is to use the following list comprehension: [element/divisor for element in given_list]. Read ahead to discover numerous other solutions. Problem: How to divide each element in a list and return a resultant list containing the quotients? Example: li = [38, 57, 76, 95, … Read more

Normal Distribution and Shapiro-Wilk Test in Python

Normal distribution is a statistical prerequisite for parametric tests like Pearson’s correlation, t-tests, and regression. Testing for normal distribution can be done visually with sns.displot(x, kde=true). The Shapiro-Wilk test for normality can be done quickest with pingouin‘s pg.normality(x). 💡 Note: Several publications note that normal distribution is the least important prerequisite for parametric tests and … Read more

Pearson Correlation in Python

A good solution to calculate Pearson’s r and the p-value, to report the significance of the correlation, in Python is scipy.stats.pearsonr(x, y). A nice overview of the results delivers pingouin’s pg.corr(x, y).  What is Pearson’s “r” Measure? A statistical correlation with Pearson’s r measures the linear relationship between two numerical variables. The correlation coefficient r … Read more

How to Remove Specific Elements in a Numpy Array

Summary: The most straightforward way to remove an element at a given index from a NumPy array is to call the function np.delete(array, index) that returns a new array with the element removed. Problem: Given a Numpy Array; how to remove specific elements from the Numpy array? Example: Consider the following Numpy array as shown below: Challenge: How will you … Read more

How to Calculate z-scores in Python?

The z-scores can be used to compare data with different measurements and for normalization of data for machine learning algorithms and comparisons. 💡 Note: There are different methods to calculate the z-score. The quickest and easiest one is: scipy.stats.zscore(). What is the z-score? The z-score is used for normalization or standardization to make differently scaled … Read more