View Post

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

You already know numpy’s average function, don’t you? This article goes a step further and shows you how to calculate the weighted average of a numpy array. You can pass four arguments to the numpy average function: The numpy array which can be multi-dimensional. (Optional) The axis along which you want to average. If you don’t specify the argument, the …

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 matrices. This puzzle introduces the …

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[0] + hong_kong[-1]) / 2.0 ny_mean = (new_york[1] + new_york[-3]) / …

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, and matrix computations. This puzzle …

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[2] == max(y)) What is the output of this puzzle? *Advanced Level* (solution below) Numpy is a popular Python library …

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

Daily Data Science Puzzle import numpy as np # stock prices (3x per day) # [morning, midday, evening] APPLE = np.array( [[50,60,55], # day 1 [60,60,65]]) # day 2 # midday variance y = np.var(APPLE, axis=0)[1] print(int(y)) What is the output of this puzzle? *Advanced Level* (solution below) Numpy is a popular Python library for data science focusing on arrays, …

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 knowledge. This knowledge gap makes …

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 variance function. When applied to …

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 arrays. You most likely know …

A Simple Guide to Element-wise Multiplication in NumPy

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 for data science. Numpy focuses …

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 one new feature of the …

Do You Know Numpy’s Awesome @ Operator?

Daily Data Science Puzzle import numpy as np # graphics data a = [[1, 1], [1, 0]] a = np.array(a) # stretch vectors b = [[2, 0], [0, 2]] b = np.array(b) c = a @ b d = np.matmul(a,b) print((c == d)[0,0]) What is the output of this puzzle? Numpy is a popular Python library for data science focusing …