Daily Data Science Puzzle
import numpy as np # salary in ($1000) [2015, 2016, 2017] dataScientist = [130, 132, 137] productManager = [127, 140, 145] designer = [118, 118, 127] softwareEngineer = [129, 131, 137] A = np.array([dataScientist, productManager, designer, softwareEngineer]) # salary raise for data scientists B = np.ones((4,1)) B = 1.1 C = A * B print(int(C))
What is the output of this puzzle?
Numpy is a popular Python library for data science focusing on arrays, vectors, and matrices. If you work with data, understanding numpy is a must.
This puzzle assumes that you have already basic knowledge of the numby library. You know how to create a numpy array (or matrix) as a list of lists. The matrix A holds the salary data (in $1000) of four job descriptions for three years 2015, 2016, and 2017.
Suppose that data scientists get a salary raise by 10% for the past three years. How to achieve this?
A convenient way is to use matrix multiplication. In particular, the vector B holds the factors with which each row (i.e., job) of matrix A should be multiplied. The goal is to multiply the first row (index 0) with 1.1 and all other rows with 1 keeping them equal. We use the array creation routine
np.ones() which takes the matrix shape (e.g. (n,m) for n rows and m columns) and creates a new matrix with value 1 for each cell.
Finally, we print the top-left element of the resulting matrix. This is the raised salary of a data scientist in the year 2015. Previously, he earned $130,000 which is raised by 10% resulting in $143,000.
Are you a master coder?
Test your skills now!