Core Data Collections and Numpy Functions
Authors
Section 1: Making Collections
| Code | Description |
|---|---|
[1, 2, 3] |
Create a list (changeable sequence). |
(1, 2, 3) |
Create a tuple (fixed sequence). |
{1, 2, 3} |
Create a set (mathematical set with unique elements). |
"123" or '123' |
Create a string (sequence of text characters). |
Python also has operators for collecting related data together. Most of this course will revolve around the pros and cons of different ways of collecting data, but let’s take a look at them:
Exercises
Example: Make a list of the first three positive even numbers
[2, 4, 6][2, 4, 6]Exercise: Make a tuple of the first three odd numbers.
Solution
(1, 3, 5)(1, 3, 5)Exercise: Make a list containing the names of two countries in Europe.
Solution
["Germany", "Italy"]['Germany', 'Italy']Exercise: Make a set of three animals.
Solution
{'Dog', 'Hamster', 'Cat'}{'Cat', 'Dog', 'Hamster'}Exercise: Make a tuple of the two possible bool values.
Solution
(True, False)(True, False)Section 2: Statistics Functions from Numpy
| Code | Description |
|---|---|
np.mean(data) |
Calculate the mean (average) of the data. |
np.max(data) |
Find the maximum value in the data. |
np.min(data) |
Find the minimum value in the data. |
np.sum(data) |
Calculate the sum of all values in the data. |
np.var(data) |
Calculate the variance of the data. |
np.std(data) |
Calculate the standard deviation of the data. |
np.median(data) |
Calculate the median (middle value) of the data. |
np.ptp(data) |
Calculate the peak-to-peak (range) of the data. |
Numpy is a Python package that, among other things, has many useful statistics functions. These take any array-like object as an input and can be found inside the np library. Sometimes, the same functionality can be found both as a Numpy function and an array method, giving you the choice of how you’d like to use it.
>>> np.mean([1, 2, 3, 4])
2.5
>>> np.ptp([1, 2, 3, 4])
3A couple lists of functions in Numpy can be found here:
- Math: https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.math.html
- Statistics: https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.statistics.html
Some useful function names: mean, min, sum, max, var, std, ptp, median, nanmedian, nanmax, nanmean, nanmin
You’ll see more later.
Using only Numpy functions, calculate the statistics on the following numbers:
Exercises
data = [2, 8, 5, 9, 2, 4, 6]
data[2, 8, 5, 9, 2, 4, 6]Example: What is the maximum of the data?
np.max(data)9Exercise: What is the mean of the data?
Solution
np.mean(data)5.142857142857143Exercise: What is the sum of the data?
Solution
np.sum(data)36Exercise: What is the minimum of the data?
Solution
np.min(data)2Exercise: What is the variance of the data?
Solution
np.var(data)6.408163265306122Exercise: What is the standard deviation of the data?
Solution
np.std(data)2.531435020952764Exercise: What is the median of the data?
Solution
np.median(data)5.0Section 3: Arrays in Numpy
Numpy has a very useful data collection: the array. Arrays are very similar to lists, with one exception:
- all elements in the array must be of the same data type (e.g. int, float, bool)
Despite that limitation, arrays are incredibly useful for data analysis, and we’ll be taking advantage of its many features throughout the course. So let’s start by learning how to easily generate different patterns of data with arrays!
Building Arrays
| Code | Description |
|---|---|
np.array([2, 5, 3]) |
Convert a list into an array. |
np.arange(2, 7) |
Create an array with integers from 2 to 7 (exclusive). |
np.arange(2, 7, 0.3) |
Create an array from 2 to 7 with spacing of 0.3. |
np.linspace(2, 3, 10) |
Create an array of 10 evenly-spaced values between 2 and 3. |
np.zeros(5) |
Create an array of 5 zeros. |
np.ones(3) |
Create an array of 3 ones. |
np.random.random(100) |
Create an array of 100 random numbers (uniform distribution 0-1). |
np.random.randn(100) |
Create an array of 100 normally-distributed random numbers. |
Let’s generate some arrays using Numpy functions! Some commonly-used examples are arange(), linspace(), zeros(), and the random number generation functions in random.
Exercises
Exercise: Import the numpy package as np:
Solution
import numpy as npExercise: Turn this list into an array:
[4, 7, 6, 1][4, 7, 6, 1]Solution
np.array([4, 7, 6, 1])array([4, 7, 6, 1])Exercise: Make an array containing the integers from 1 to 15.
Solution
np.arange(1, 15)array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])Exercise: Make an array of the values from 2 and 6, spaced 0.5 from each other.
Solution
np.arange(2, 6.1, 0.5)array([2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. ])Exercise: Make an array of only 6 numbers between 1 and 10, evenly-spaced between them.
Solution
np.linspace(1, 10, 10)array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])Exercise: How about an array of 10 evenly-spaced values between 100 and 1000?
np.linspace(100, 1000, 10)array([ 100., 200., 300., 400., 500., 600., 700., 800., 900.,
1000.])Solution
np.arange(100, 1000+100, 100)array([ 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000])Exercise: Turn this list into a an array…
a = [True, False, False, True]
a[True, False, False, True]Solution
np.array(a)array([ True, False, False, True])Exercise: Make an array containing 20 zeros.
Solution
a = np.zeros(20)
aarray([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0.])Exercise: Make an array containing 20 ones.
Solution
np.ones(20)array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1.])Exercise: Generate an array of 10 random numbers
Solution
np.random.random(10)array([0.01366507, 0.31869231, 0.6357354 , 0.54693742, 0.29979927,
0.85864429, 0.14286405, 0.46649301, 0.0638231 , 0.19134368])Combining array generation with statistics functions
These exercises all involve two steps:
- Make the data
- Calculate something on the data
for example:
np.mean(np.arange(1, 10)) # the mean of the integers from 1 to 9Example: What is the standard deviation of the integers between 2 and 20?
np.std(np.arange(2, 21))5.477225575051661Exercise: Is it the same as the standard deviation of the integers between 1 and 10?
Solution
np.std(np.arange(11))3.1622776601683795Exercise: What is the standard deviation of the numbers generated from the np.random.randn() function?
Solution
np.std(np.random.randn())0.0Exercise: What is the sum of an array of 100 ones?
Solution
np.sum(np.ones(100))100.0Exercise: What is the sum of an array of 100 zeros?
Solution
np.sum(np.zeros(100))0.0Section 4: Array Broadcasting: Combining Arrays with Operators
| Code | Description |
|---|---|
array + value |
Add a value to all elements in the array. |
array * value |
Multiply all elements in the array by a value. |
array - value |
Subtract a value from all elements in the array. |
array / value |
Divide all elements in the array by a value. |
array ** value |
Raise all elements in the array to a power. |
np.abs(array) |
Calculate the absolute value of all elements. |
np.sqrt(array) |
Calculate the square root of all elements. |
np.log(array) |
Calculate the natural logarithm of all elements. |
np.sin(array), np.cos(array), np.tan(array) |
Calculate trigonometric functions on all elements. |
Remember our math operators? We can use them on arrays, too!
Numpy also has functions that can transform each value in an array using a math operation. For example: np.log(), np.abs(), np.sin(), np.cos(), np.tan(), np.sqrt()
Exercises
Example: Add 10 to all of the numbers below
x = np.array([1, 3, 6, 8, 10])
x + 10array([11, 13, 16, 18, 20])Exercise: Multiply everything in the array below by 10
np.array([1, 3, 6, 8, 10])array([ 1, 3, 6, 8, 10])Solution
x = np.array([1, 3, 6, 8, 10])
x * 10array([ 10, 30, 60, 80, 100])Exercise: Multiply all the numbers from 1 to 100 by 1000.
Solution
np.arange(1, 101) * 1000array([ 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000,
9000, 10000, 11000, 12000, 13000, 14000, 15000, 16000,
17000, 18000, 19000, 20000, 21000, 22000, 23000, 24000,
25000, 26000, 27000, 28000, 29000, 30000, 31000, 32000,
33000, 34000, 35000, 36000, 37000, 38000, 39000, 40000,
41000, 42000, 43000, 44000, 45000, 46000, 47000, 48000,
49000, 50000, 51000, 52000, 53000, 54000, 55000, 56000,
57000, 58000, 59000, 60000, 61000, 62000, 63000, 64000,
65000, 66000, 67000, 68000, 69000, 70000, 71000, 72000,
73000, 74000, 75000, 76000, 77000, 78000, 79000, 80000,
81000, 82000, 83000, 84000, 85000, 86000, 87000, 88000,
89000, 90000, 91000, 92000, 93000, 94000, 95000, 96000,
97000, 98000, 99000, 100000])Example: Calculate the absolute value of the following data.
x = np.array([-5, 7, -2, 4, -10])
np.abs(x)array([ 5, 7, 2, 4, 10])Exercise: Calculate the cosine of all integers between 0 and 6.
Solution
np.cos(np.arange(7))array([ 1. , 0.54030231, -0.41614684, -0.9899925 , -0.65364362,
0.28366219, 0.96017029])Exercise: Calculate the square root of 10 uniformly-generated numbers between 1 and 5 (tip: np.random.uniform).
Solution
np.sqrt(np.random.uniform(low = 1, high = 5, size = 10))array([1.87632452, 2.08160884, 1.53367252, 1.7213499 , 1.51250966,
1.11351476, 1.47087963, 1.75175495, 1.9017713 , 1.51756389])