Skip to content
# Add your Python code here!
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
(np.array([[1,2,3,4], [5,6,7,8]]), np.array([[1,2,3,4], [5,6,7,8]]).shape)
costs = np.column_stack(([3, 2, 1, 3], 
                         [7, 6, 6, 5]))
costs # 2.5
print(costs)
costs[1, :]
np.mean(costs[:, 0])
np.std(np.array([4,5,3,2,1,8]))
Create Matrix
# 3 x 3 matrix
x = [[col for col in range(1,4)] for row in range(3)]
print(x)
# Make into column stack
print(np.column_stack(x))
# make into row for range each
print(np.array(x))
  • Convert this nested for loop into a nested list comprehension. The lettersvariable has been loaded for you.
letters = ['A', 'B', 'C'] pairs = [] for letter in letters: for num in range(0, 2): l_and_n.append((letter, num))
  • [('A', 0), ('A', 1), ('B', 0), ('B', 1), ('C', 0), ('C', 1)]
letters = ['A', 'B', 'C']
pairs_one = [[(i, letter) for letter in letters] for i in range(0,2)]
print(pairs_one)
pairs_two = [(letter, i) for letter in letters for i in range(0,2)]
print(pairs_two)
(sorted([-1, 2, -5], reverse=True), sorted([-1,2,-5]))
  • Consider the Following Array
    • array([21, 17, 65, 43, 29, 50, 35])
    • Return Output - array([65,50])
test_ar = np.array([21,17,65,43,29,50,35])
print(test_ar)
mask = test_ar >= 50
print(test_ar[mask])
  • Unpack using np.where for a list of tuples w/row&col pairs for values higher than 7
array_2D = np.array([[ 5,  1,  3,  2,  4,  6],
       [ 5,  8,  0,  3,  1,  2],
       [ 3, 10,  6, 21, 15,  1]])
array_2D
np.where(array_2D > 7)
row_col_greater_7 = list(zip(*np.where(array_2D > 7)))
row_col_greater_7