<jupyter_start><jupyter_text>Python for Finance (2nd ed.)**Mastering Data-Driven Finance**&copy; Dr. <NAME> | The Python Quants GmbH  Numerical Computing with NumPy  Arrays with Python Lists<jupyter_code>v = [0.5, 0.75, 1.0, 1.5, 2.0]  
m = [v, v, v]  
m  
m[1]
m[1][0]
v1 = [0.5, 1.5]
v2 = [1, 2]
m = [v1, v2]
c = [m, m]  
c
c[1][1][0]
v = [0.5, 0.75, 1.0, 1.5, 2.0]
m = [v, v, v]
m
v[0] = 'Python'
m
from copy import deepcopy
v = [0.5, 0.75, 1.0, 1.5, 2.0]
m = 3 * [deepcopy(v), ]  
m
v[0] = 'Python'  
m<jupyter_output><empty_output><jupyter_text>Interlude: Python Array Class<jupyter_code>v = [0.5, 0.75, 1.0, 1.5, 2.0]
import array
a = array.array('f', v)  
a
a.append(0.5)  
a
a.extend([5.0, 6.75])  
a
2 * a  
# causes intentional error
# a.append('string')  
a.tolist()  
f = open('array.apy', 'wb')  
a.tofile(f)  
f.close()  
with open('array.apy', 'wb') as f:  
    a.tofile(f)  
!ls -n arr*  
b = array.array('f')  
with open('array.apy', 'rb') as f:  
    b.fromfile(f, 5)  
b  
b = array.array('d')  
with open('array.apy', 'rb') as f:
    b.fromfile(f, 2)  
b<jupyter_output><empty_output><jupyter_text>Regular NumPy Arrays  The Basics<jupyter_code>import numpy as np  
a = np.array([0, 0.5, 1.0, 1.5, 2.0])  
a
type(a)  
a = np.array(['a', 'b', 'c'])  
a
a = np.arange(2, 20, 2)  
a
a = np.arange(8, dtype=np.float)  
a
a[5:]  
a[:2]  
a.sum()  
a.std()  
a.cumsum()  
l = [0., 0.5, 1.5, 3., 5.]
2 * l  
a
2 * a  
a ** 2  
2 ** a  
a ** a  
np.exp(a)  
np.sqrt(a)  
np.sqrt(2.5)  
import math  
math.sqrt(2.5)  
# causes intentional error
# math.sqrt(a)  
%timeit np.sqrt(2.5)  
%timeit math.sqrt(2.5)<jupyter_output>78.6 ns ± 0.723 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)<jupyter_text>Multiple Dimensions<jupyter_code>b = np.array([a, a * 2])  
b
b[0]  
b[0, 2]  
b[:, 1]  
b.sum()  
b.sum(axis=0)  
b.sum(axis=1)  
c = np.zeros((2, 3), dtype='i', order='C')  
c
c = np.ones((2, 3, 4), dtype='i', order='C')  
c
d = np.zeros_like(c, dtype='f16', order='C')  
d
d = np.ones_like(c, dtype='f16', order='C')  
d
e = np.empty((2, 3, 2))  
e
f = np.empty_like(c)  
f
np.eye(5)  
g = np.linspace(5, 15, 12) 
g<jupyter_output><empty_output><jupyter_text>Meta-Information<jupyter_code>g.size  
g.itemsize  
g.ndim  
g.shape  
g.dtype  
g.nbytes<jupyter_output><empty_output><jupyter_text>Reshaping, Resizing, Stacking, Flattening<jupyter_code>g = np.arange(15)
g
g.shape  
np.shape(g) 
g.reshape((3, 5))  
h = g.reshape((5, 3))  
h
h.T  
h.transpose()  
g
np.resize(g, (3, 1))  
np.resize(g, (1, 5))  
np.resize(g, (2, 5))  
n = np.resize(g, (5, 4))  
n
h
np.hstack((h, 2 * h))  
np.vstack((h, 0.5 * h))  
h
h.flatten()  
h.flatten(order='C')  
h.flatten(order='F')  
for i in h.flat:  
    print(i, end=',')
for i in h.ravel(order='C'):  
    print(i, end=',')
for i in h.ravel(order='F'):  
    print(i, end=',')<jupyter_output>0,3,6,9,12,1,4,7,10,13,2,5,8,11,14,<jupyter_text>Boolean Arrays<jupyter_code>h
h > 8  
h <= 7  
h == 5  
(h == 5).astype(int)  
(h > 4) & (h <= 12)  
h[h > 8]  
h[(h > 4) & (h <= 12)]  
h[(h < 4) | (h >= 12)]  
np.where(h > 7, 1, 0)  
np.where(h % 2 == 0, 'even', 'odd')  
np.where(h <= 7, h * 2, h / 2)<jupyter_output><empty_output><jupyter_text>Speed Comparison<jupyter_code>import random
I = 5000
%time mat = [[random.gauss(0, 1) for j in range(I)] \
             for i in range(I)]  
mat[0][:5]  
%time sum([sum(l) for l in mat])  
import sys
sum([sys.getsizeof(l) for l in mat])  
%time mat = np.random.standard_normal((I, I))  
%time mat.sum()  
mat.nbytes  
sys.getsizeof(mat)<jupyter_output><empty_output><jupyter_text>Structured Arrays<jupyter_code>dt = np.dtype([('Name', 'S10'), ('Age', 'i4'),
               ('Height', 'f'), ('Children/Pets', 'i4', 2)])  
dt  
dt = np.dtype({'names': ['Name', 'Age', 'Height', 'Children/Pets'],
             'formats':'O int float int,int'.split()})  
dt  
s = np.array([('Smith', 45, 1.83, (0, 1)),
              ('Jones', 53, 1.72, (2, 2))], dtype=dt)  
s  
type(s)  
s['Name']  
s['Height'].mean()  
s[0]  
s[1]['Age']<jupyter_output><empty_output><jupyter_text>Vectorization of Code<jupyter_code>np.random.seed(100)
r = np.arange(12).reshape((4, 3))  
s = np.arange(12).reshape((4, 3)) * 0.5  
r  
s  
r + s  
r + 3  
2 * r  
2 * r + 3  
r
r.shape
s = np.arange(0, 12, 4)  
s  
r + s  
s = np.arange(0, 12, 3)  
s  
# causes intentional error
# r + s  
r.transpose() + s  
sr = s.reshape(-1, 1)  
sr
sr.shape  
r + s.reshape(-1, 1)  
def f(x):
    return 3 * x + 5  
f(0.5)  
f(r)<jupyter_output><empty_output><jupyter_text>Memory Layout Cf. http://eli.thegreenplace.net/2015/memory-layout-of-multi-dimensional-arrays/<jupyter_code>x = np.random.standard_normal((1000000, 5))  
y = 2 * x + 3  
C = np.array((x, y), order='C')  
F = np.array((x, y), order='F')  
x = 0.0; y = 0.0  
C[:2].round(2)  
%timeit C.sum()  
%timeit F.sum()  
%timeit C.sum(axis=0)  
%timeit C.sum(axis=1)  
%timeit F.sum(axis=0)  
%timeit F.sum(axis=1)  
F = 0.0; C = 0.0<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21696410667736113, 'fraction_numerical': 0.05995588530178464, 'mean_word_length': 3.142857142857143, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '104651', 'license': 'CNRI-Python', 'n_tokens_mistral': 2612, 'n_tokens_neox': 2440, 'n_words': 544, 'path': 'code/ch04/04_numpy.ipynb', 'repo_name': 'saltyspades/pythonforfinance', 'size': '4997'}