Introduction to Python for Finance
Adina Howe
Instructor
import numpy as np
months = [1, 2, 3]
prices = [238.11, 237.81, 238.91]
cpi_array = np.array([months, prices])
print(cpi_array)
[[ 1. 2. 3. ]
[ 238.11 237.81 238.91]]
print(cpi_array)
[[ 1. 2. 3. ]
[ 238.11 237.81 238.91]]
.shape
gives you dimensions of the array
print(cpi_array.shape)
(2, 3)
.size
gives you total number of elements in the array
print(cpi_array.size)
6
import numpy as np
prices = [238.11, 237.81, 238.91]
prices_array = np.array(prices)
np.mean()
calculates the mean of an input
print(np.mean(prices_array))
238.27666666666667
np.std()
calculates the standard deviation of an input
print(np.std(prices_array))
0.46427960923946671
numpy.arange()
creates an array with start, end, step
import numpy as np months = np.arange(1, 13)
print(months)
[ 1 2 3 4 5 6 7 8 9 10 11 12]
months_odd = np.arange(1, 13, 2)
print(months_odd)
[ 1 3 5 7 9 11]
numpy.transpose()
switches rows and columns of a numpy array
print(cpi_array)
[[ 1. 2. 3. ]
[ 238.11 237.81 238.91]]
cpi_transposed = np.transpose(cpi_array)
print(cpi_transposed)
[[ 1. 238.11]
[ 2. 237.81]
[ 3. 238.91]]
print(cpi_array)
[[ 1. 2. 3. ]
[ 238.11 237.81 238.91]]
# row index 1, column index 2
cpi_array[1, 2]
238.91
# all row slice, third column
print(cpi_array[:, 2])
[ 3. 238.91]
Introduction to Python for Finance