NumPy Practice One

Imports

import numpy

Prepare Inputs

def prepare_inputs(inputs):
    """transforms inputs and does some math

    Creates a 2-dimensional ndarray from the given 1-dimensional list
     and assigns  it to input_array

    Finds the minimum value in the input array and subtracts that
     value from all the elements of input_array.

    Finds the maximum value in inputs_minus_min and divides
     all of the values in inputs_minus_min by the maximum value.

    Args:
     inputs: one-dimensional list

    Returns:
     tuple: transposed inputs, inputs-minus-min, inputs-minus-min scaled by max
    """
    input_array = numpy.array([inputs])
    inputs_minus_min = input_array - input_array.min()
    inputs_div_max = inputs_minus_min/inputs_minus_min.max()
    return input_array, inputs_minus_min, inputs_div_max
inputs = [1, 2, 3]
transposed, less_min, divided_by_max = prepare_inputs(inputs)
print(transposed)
expected = numpy.array([0, 1, 2])
assert all(expected == less_min[0,:])
print(less_min)
assert all(expected/2 == divided_by_max[0, :])
print(divided_by_max)
[[1 2 3]]
[[0 1 2]]
[[0.  0.5 1. ]]

Multiply Inputs

def multiply_inputs(m1, m2):
    """Multiplies matrices

    Args:
     m1, m2: matrices to multiply

    Returns:
     matrix product or False if shapes are wrong
    """
    product = False
    okay, swap = m1.shape[1] == m2.shape[0], m1.shape[0] == m2.shape[1]
    if any((okay, swap)):
        product = m1.dot(m2) if okay else m2.dot(m1)
    return product
ROW, COLUMN = 0, 1
m_1 = numpy.array([1, 2, 3, 4, 5, 6]).reshape((2, 3))
m_2 = numpy.array([6, 5, 4, 3, 2, 1]).reshape((2, 3))
m_3 = m_2.reshape((3, 2))
m_4 = numpy.arange(12).reshape(6, 2)
print(m_3.shape)

print("{} x {}".format(m_1.shape, m_2.shape))
product = multiply_inputs(m_1, m_2)
assert not product
print(product)

print("\n{} x {}".format(m_1.shape, m_3.shape))
product = multiply_inputs(m_1, m_3)
assert product.shape == (m_1.shape[ROW], m_3.shape[COLUMN])
print(product)
print(product.shape)

print("\n{} x {}".format(m_1.shape, m_4.shape))
product = multiply_inputs(m_1, m_4)
assert product.shape == (m_4.shape[ROW], m_1.shape[COLUMN])
print(product)
print(product.shape)
(3, 2)
(2, 3) x (2, 3)
False

(2, 3) x (3, 2)
[[20 14]
 [56 41]]
(2, 2)

(2, 3) x (6, 2)
[[ 4  5  6]
 [14 19 24]
 [24 33 42]
 [34 47 60]
 [44 61 78]
 [54 75 96]]
(6, 3)

Find the mean

def find_mean(values):
    """Find the mean value

    Args:
     values: list of numeric values
    Returns:
     the average of the values in the given Python list
    """
    return numpy.array(values).mean()
inputs = [[1, 5, 9]]
outputs = find_mean(inputs)
print(outputs)
assert abs(sum(inputs[0])/len(inputs[0]) - outputs) < 0.1**5
5.0

More Outputs

input_array, inputs_minus_min, inputs_div_max = prepare_inputs([-1,2,7])
print("Input as Array: {}".format(input_array))
print("Input minus min: {}".format(inputs_minus_min))
print("Input  Array: {}".format(inputs_div_max))

print("Multiply 1:\n{}".format(multiply_inputs(numpy.array([[1,2,3],[4,5,6]]), numpy.array([[1],[2],[3],[4]]))))
print("Multiply 2:\n{}".format(multiply_inputs(numpy.array([[1,2,3],[4,5,6]]), numpy.array([[1],[2],[3]]))))
print("Multiply 3:\n{}".format(multiply_inputs(numpy.array([[1,2,3],[4,5,6]]), numpy.array([[1,2]]))))

print("Mean == {}".format(find_mean([1,3,4])))
Input as Array: [[-1  2  7]]
Input minus min: [[0 3 8]]
Input  Array: [[0.    0.375 1.   ]]
Multiply 1:
False
Multiply 2:
[[14]
 [32]]
Multiply 3:
[[ 9 12 15]]
Mean == 2.6666666666666665