# Daily Data Science Puzzle ## NumPy Diff Simply Explained [Bonus Video]

The NumPy diff function np.diff() calculates the difference between subsequent values in a NumPy array. For example, calling np.diff() on the NumPy array [1 2 4] would result in the difference array [1 2]. Here is a detailed example: This code snippet shows the most simple form of the np.diff() method: how to use it … ## How to Calculate the Weighted Average of a Numpy Array in Python?

NumPy’s average function computes the average of all numerical values in a NumPy array. When used without parameters, it simply calculates the numerical average of all values in the array, no matter the array’s dimensionality. For example, the expression np.average([[1,2],[2,3]]) results in the average value (1+2+2+3)/4 = 2.0. However, what if you want to calculate …

## Python Numpy 101: How to Calculate the Simple Average of a Numpy Array?

import numpy as np # stock prices (3x per day) # [morning, midday, evening] solar_x = np.array( [[2, 3, 4], # day 1 [2, 2, 5]]) # day 2 print(np.average(solar_x)) What is the output of this puzzle? *Beginner Level* (solution below) Numpy is a popular Python library for data science focusing on arrays, vectors, and …

## Python Numpy 101: How to Index Elements in Numpy Arrays?

Daily Data Science Puzzle import numpy as np # air quality index AQI data hong_kong = np.array( [ 42, 40, 41, 43, 44, 43 ]) new_york = np.array( [ 30, 31, 29, 29, 29, 30 ]) montreal = np.array( [ 11, 11, 12, 13, 11, 12 ]) hk_mean = (hong_kong + hong_kong[-1]) / 2.0 ny_mean …

## How to Calculate the Standard Deviation of a Numpy Array?

Daily Data Science Puzzle import numpy as np temp_sensor = np.array( [ 18, 22, 22, 18 ]) mean = np.mean(temp_sensor) std = np.std(temp_sensor) print(str(int(mean – std)) + "-" + str(int(mean + std))) What is the output of this puzzle? *Intermediate Level* (solution below) Numpy is a popular Python library for data science for array, vector, …

## How to Calculate the Standard Deviation in Numpy?

Daily Data Science Puzzle import numpy as np # daily stock prices # [open, close] google = np.array( [[1239, 1258], # day 1 [1262, 1248], # day 2 [1181, 1205]]) # day 3 # standard deviation y = np.std(google, axis=1) print(y == max(y)) What is the output of this puzzle? *Advanced Level* (solution below) Numpy …

## Python Numpy 101: How to Calculate the Row Variance of a Numpy 2D Array?

You can play with the following interactive Python code to calculate the variance of a 2D array (total, row, and column variance). Here’s another practical example: <br />import numpy as np</p> <p># stock prices (3x per day)<br /># [morning, midday, evening]<br />APPLE = np.array(<br />[[50,60,55], # day 1<br />[60,60,65]]) # day 2</p> <p># midday variance<br …

## A Case for Puzzle-based Learning Python

Overcome the Knowledge Gap The great teacher Sokrates delivered complex knowledge by asking a sequence of questions. Each question was building on answers to previous questions provided by the student. This more than 2400 year old teaching technique is still in wide-spread use today. A good teacher opens a gap between their’s and the learner’s …

## How to Calculate Variance of Python NumPy Arrays?

To calculate the variance of a Python NumPy array “x”, simply use the function “np.var(x)”. Here is an example: What is the output of this puzzle?*Intermediate Level* (solution below) Numpy is a popular Python library for data science focusing on arrays, vectors, and matrices. This puzzle introduces a new feature of the numpy library: the …

## How to index 1D numpy arrays?

Daily Data Science Puzzle import numpy as np # The fibonacci series F = np.array([0, 1, 1, 2, 3, 5, 8]) F[::3] = 0 print(sum(F[:4])) What is the output of this puzzle? *Intermediate Level* Numpy is a popular Python library for data science focusing on arrays, vectors, and matrices. This puzzle demonstrates indexing in numpy …

## [Numpy * Operator] Element-wise Multiplication in Python

How does element-wise multiplication of two numpy arrays a and b work in Python’s Numpy library? Simply use the star operator “a * b”! Here is a code example from my new NumPy book “Coffee Break NumPy”: Can you guess the output of this puzzle?*Advanced Level* (see solution below) NumPy is a popular Python library …

## Is This an Average Puzzle? Yes!

Daily Data Science Puzzle import numpy as np # Goals in five matches goals_brazil = np.array( [1,2,3,1,2]) goals_germany = np.array( [1,0,1,2,0]) br = np.average(goals_brazil) ge = np.average(goals_germany) print(br>ge) What is the output of this puzzle? *Beginner Level* Numpy is a popular Python library for data science focusing on arrays, vectors, and matrices. This puzzle introduces …