Using NumPy mathematical functions
One reason NumPy arrays are useful is we can execute math operations more easily and in less compute time. This speed boost is due to something called vectorization, where operations are applied to a whole array instead of one element at a time. For example, if we want to scale down our closing bitcoin prices by 1,000 (putting the units in kilodollars), we can do this:
kd_close = close_array / 1000
Common math operations, including addition, subtraction, and so on, are available. Of course, we could do this with a list comprehension or for
loop:
kd_close_list = [c / 1000 for c in close_list]
The advantage of NumPy is that it executes much faster, since NumPy is mostly written in C and is vectorized. We can use the magic command %timeit
(or %%timeit
for more than one line of code) in Jupyter Notebook or IPython to measure how long the execution is for the two preceding examples:
%timeit kd_close = close_array / 1000
and
...