Array Functions¶

Most list functions can also be applied to arrays. For example, functions such as sum() and len() operate on arrays in exactly the same way as they would on lists. The numpy package also provides us with several additional functions that do not exist in base Python, as well as versions of standard Python functions that have been optimized for arrays.

For example, even though we can use sum() to calculate the sum of values in an array, numpy also provides us with a function called np.sum(), which can be used on both lists and arrays, but has been optimized for performance on arrays. To provide an example of this, we will import the time package, with provides us with a tool for measuring the execution time for portions of code.

import time

The function time.time() will report the current system time, in seconds. In the following cell, we have four loops. Each loop will calculate one million sums, using either sum() or np.sum(), and running on either a list or an array. We will check the time before and after each loop runs.

t1 = time.time()
for i in range(1000000):
    sum(myList)
    
t2 = time.time()
for i in range(1000000):
    sum(myArray)
    
t3 = time.time()
for i in range(1000000):
    np.sum(myList)
    
t4 = time.time()
for i in range(1000000):
    np.sum(myArray)

t5 = time.time()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-8349240bb5ba> in <module>
      1 t1 = time.time()
      2 for i in range(1000000):
----> 3     sum(myList)
      4 
      5 t2 = time.time()

NameError: name 'myList' is not defined

In the cell below, we will calculate the time required for each loop to run, and will plot the results using matplotlib.

import matplotlib.pyplot as plt
labels = ['sum on\nlist', 'sum on\narray', 'np.sum on\nlist', 'np.sum on\narray']
heights = [t5 - t4, t4 - t3, t3 - t2, t2 - t1]

plt.bar(labels, heights, color='cornflowerblue', edgecolor='k')
plt.ylabel('Time (in seconds)')
plt.show()

Numpy also provides the following functions:

  • np.prod() returns the product of the elements in an array.

  • np.max() returns the largest element in an array.

  • np.min() returns the smallest element in an array.

  • np.argmax() returns the index of the largest element in an array.

  • np.argmin() returns the index of the smallest element in an array.

  • np.mean() returns the mean of the elements in an array.

  • np.std() returns the standard deviation of elements in an array.

  • np.unique() returns an array of distinct elements in an array.

test_array = np.array([3.2, 4.8, 8.7, 8.7, 6.4, 5.3, 5.3, 
                       1.8, 4.8, 1.8, 5.4, 3.1, 3.2, 1.8])

print('Sum:               ', np.sum(test_array))
print('Product:           ', np.prod(test_array))
print('Max:               ', np.max(test_array))
print('Min:               ', np.min(test_array))
print('ArgMax:            ', np.argmax(test_array))
print('ArgMin:            ', np.argmin(test_array))
print('Mean:              ', np.mean(test_array))
print('Standard Deviation:', np.std(test_array))
print('Distinct Elements: ', np.unique(test_array))

Elementwise Functions¶

Numpy provides us with several elementwise functions. The functions will apply a certain operation to each element of an array, returning a new array. We will not cover all of these here, but will show you a few examples.

  • np.exp() raises e to each element within an array.

  • np.log() applies the natural logarithm to each element within an array.

  • np.round() rounds each element of an array to a specified number of decimal places.

float_array = [3.45143, 1.23498, 6.57618, 2.47508, 7.50698]

print('Example of np.exp:  ', np.exp(float_array))
print('Example of np.log:  ', np.log(float_array))
print('Example of np.round:', np.round(float_array, 2))
print('Example of np.round:', np.round(float_array, 0))

Example: Calculating Total Sales¶

Two lists, sales and prices are provided below. Each entry of sales provides the number of units of a different product sold by a store during a given week. The prices lists provides the unit price of each of the products.

Without using NumPy, write some code that will print out a single number totalSales that is equal to the store’s total revenue during the week.

sales = [24, 61, 17, 34, 41, 29, 32, 43]
prices = [10.50, 5.76, 13.49, 8.13, 7.79, 12.60, 9.51, 11.34]

totalSales = 0
for i in range(0, len(sales)):
    totalSales += sales[i] * prices[i]
    
print(totalSales)

The cell below converts the lists sales and price into arrays. Use NumPy to accomplish to calculate totalSales. See if you can do it with only one new line of code.

sales_array = np.array(sales)
prices_array = np.array(prices)

totalSales = np.sum(sales_array * prices_array)
print(totalSales)