# Month: May 2018

## Analyzing Stock Data with Numpy – The Reshape and Average Functions

Daily Data Science Puzzle [python] import numpy as np # apple stock prices (May 2018) prices = [ 189, 186, 186, 188, 187, 188, 188, 186, 188, 188, 187, 186 ] prices = np.array(prices) data_3day = prices.reshape(4,3) print(int(np.average(data_3day))) print(int(np.average(data_3day[-1]))) [/python] What is the output of this puzzle? Numpy is a popular Python library for data …

## How to Create a Sequence of Evenly Spaced Values with Numpy Linspace?

Daily Data Science Puzzle [python] import numpy as np year = np.linspace(0, 365, 366) print(int(year[-1] – year[-2])) [/python] What is the output of this puzzle? Numpy is a popular Python library for data science focusing on linear algebra. This puzzle is about the useful function linspace. In particular, linspace(start, stop, num) returns evenly spaced numbers …

## How to Create a Sequence of Linearly Increasing Values with Numpy Arange?

Daily Data Science Puzzle [python] import numpy as np # save \$122.50 per month x = 122.5 net_wealth = np.arange(0, 1000, x) # how long to save >\$1000? print(len(net_wealth)) [/python] What is the output of this puzzle? Numpy is a popular Python library for data science focusing on linear algebra. This puzzle is about the …

## How to Initialize a Numpy Array with Zeros and Ones

Daily Data Science Puzzle [python] 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 print(int(Y[-1][-1])) [/python] What is the output of this puzzle? Numpy is a popular Python library for data science focusing on linear algebra. When …

## Numpy Matrix Multiplication

Daily Data Science Puzzle [python] 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 …

## The Dimension of a Numpy Matrix

What is the output of this puzzle?   [python] import numpy as np # salary in (\$1000) [2015, 2016, 2017] dataScientist = [133, 132, 137] productManager = [127, 140, 145] designer = [118, 118, 127] softwareEngineer = [129, 131, 137] a = np.array([dataScientist, productManager, designer, softwareEngineer]) print(a.ndim) [/python]   Numpy is a popular Python library …

## Daily Data Science Puzzle: How to Get the Shape of a Numpy Matrix?

What is the output of this puzzle?   [python] import numpy as np # salary in (\$1000) [2015, 2016, 2017] dataScientist = [133, 132, 137] productManager = [127, 140, 145] designer = [118, 118, 127] softwareEngineer = [129, 131, 137] a = np.array([dataScientist, productManager, designer, softwareEngineer]) print(a.shape) print(a.shape) [/python]   Numpy is a popular Python …

## Daily Python Puzzle: How to Intersect two Sets in Python?

What is the output of this puzzle? [python] # cancer-fighting foods cf = {“basil”, “berries”, “broccoli”, “curcume”, “garlic”, “kale”, “oranges”, “seeds”, “spinach”, “sprouts”} # blood pressure reducing foods bf = {“bananas”, “berries”, “fish”, “garlic”, “kale”, “red beets”, “salmon”, “seeds”, “spinach”, “yogurt”} print(len(cf & bf)) [/python] This puzzle is about two pieces of basic knowledge. The …

## Daily Python Puzzle: How to Find the Maximum in a Dictionary?

What is the output of this puzzle?   [python] # mg per 100g omega3_table = { "Salmon" : 2260, "Hering" : 1729, "Sardines" : 1480, "Flaxseeds" : 53400, "Eggs" : 400 } y = max(omega3_table, key=lambda x : omega3_table[x]) print(y) [/python]   In this puzzle, we learn two things. First, we can retrieve the maximal …

## How to Sort Dictionaries in Python?

What is the output of this puzzle?   [python] # mg per 100g omega3_table = { "Salmon" : 2260, "Hering" : 1729, "Sardines" : 1480, "Flaxseeds" : 53400, "Eggs" : 400 } y = sorted(omega3_table, key=lambda x : omega3_table[x]) print(y[-1]) [/python]   In this puzzle, we show how to sort a dictionary not after the …

## How to Boost your Logic Skills (Series)? The “in” and “is” Keywords.

What is the output of this puzzle?   [python] meal_1 = "Meat" meal_2 = "Flaxseeds" meal_3 = "Marshmallows" healthyFoods = ["Kale", "Apple", "Strawberry", "Banana", "Flaxseeds"] def isHealthy(food): return food in healthyFoods m_1 = isHealthy(meal_1) m_2 = isHealthy(meal_2) m_3 = isHealthy(meal_3) print((not m_1 or m_2) and (meal_2 is "Flaxseeds") and isHealthy("Kale")) [/python]   This puzzle shows …

## Boost your Logic Skills Series

What is the output of this puzzle?   [python] # Logic Statements A = True # Temperatures are high B = True # Arctic ice is melting C = True # Sea levels are rising D = False # All regions become hot def follows(A,B): """ Returns True if ‘A -> B’ in words: ‘B …