Daily Data Science Puzzle
import numpy as np
n = 100 # dimensionality
W = np.zeros((n,n))
for i in range(len(W)):
W[i][i] = 2
X = np.ones((n,n))
Y = W * X
What is the output of this puzzle?
Numpy is a popular Python library for data science focusing on linear algebra. When working with numpy, you must be fluent with matrix operations (e.g. multiplication).
This puzzle performs a simple linear regression calculation. It tests your understanding of three numpy concepts.
First, you can specify the shape of the numpy array as a tuple (n,m) where n is the number of rows and m the number of columns.
Second, you can create new numpy arrays of a specified shape using the functions
zeros(). The initial values of such a numpy array are 1s and 0s, respectively.
Third, you can do matrix multiplication using the intuitive multiplication operator ‘*’. Each cell (i,j) of the new matrix is the product of the row vector i of the first matrix with the column vector j of the second matrix.
As a result, we print the last element of the two-dimensional matrix Y (bottom-right).
Are you a master coder?
Test your skills now!