NumPy


In [5]:
# Getting set up to use the NumPy Library
import numpy as np

In [8]:
# Arrays have shape (dimensions) and data types
rows = 1
cols = 3
np.zeros(shape=(rows, cols), dtype=float)


Out[8]:
array([[ 0.,  0.,  0.]])

In [9]:
# Arrays have shape (dimensions) and data types
rows = 3
cols = 3
np.zeros(shape=(rows, cols), dtype=float)


Out[9]:
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])

In [11]:
# Three dimensional array
rows = 3
cols = 3
depth = 3
np.zeros(shape=(rows, cols, depth), dtype=float)


Out[11]:
array([[[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]]])

In [14]:
my_ones = np.ones(shape=(rows, cols), dtype=int)
my_ones


Out[14]:
array([[1, 1, 1],
       [1, 1, 1],
       [1, 1, 1]])

In [13]:
print(my_ones)


[[1 1 1]
 [1 1 1]
 [1 1 1]]

In [31]:
a = np.arange(16)

In [32]:
a


Out[32]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])

In [33]:
a[0:4]


Out[33]:
array([0, 1, 2, 3])

In [34]:
a[4:6]


Out[34]:
array([4, 5])

In [35]:
a[0:]


Out[35]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])

In [36]:
a[:]


Out[36]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])

In [40]:
a[:-1]


Out[40]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])

In [39]:
a[::-1]


Out[39]:
array([15, 14, 13, 12, 11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1,  0])

In [38]:
a + 20


Out[38]:
array([20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35])

In [41]:
a


Out[41]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])

In [42]:
a.reshape(4, 4)


Out[42]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

In [46]:
a.cumsum()


Out[46]:
array([  0,   1,   3,   6,  10,  15,  21,  28,  36,  45,  55,  66,  78,
        91, 105, 120])

The Matrix


In [51]:
M = np.matrix('1.0 2.0 3.0 4.0; 5.0 6.0 10.0 11.0; 12.0 20.0 50.0 9.0')

In [52]:
M


Out[52]:
matrix([[  1.,   2.,   3.,   4.],
        [  5.,   6.,  10.,  11.],
        [ 12.,  20.,  50.,   9.]])

In [53]:
# Transpose
M.T


Out[53]:
matrix([[  1.,   5.,  12.],
        [  2.,   6.,  20.],
        [  3.,  10.,  50.],
        [  4.,  11.,   9.]])

In [62]:
X = np.matrix('100 0 0 0; 0 100 0 0; 0 0 100 0')
X


Out[62]:
matrix([[100,   0,   0,   0],
        [  0, 100,   0,   0],
        [  0,   0, 100,   0]])

In [63]:
M + X


Out[63]:
matrix([[ 101.,    2.,    3.,    4.],
        [   5.,  106.,   10.,   11.],
        [  12.,   20.,  150.,    9.]])

In [64]:
# Inverse
X.I


Out[64]:
matrix([[ 0.01,  0.  ,  0.  ],
        [ 0.  ,  0.01,  0.  ],
        [ 0.  ,  0.  ,  0.01],
        [ 0.  ,  0.  ,  0.  ]])

In [1]:
print("Hello Linear Algebra")


Hello Linear Algebra

In [2]:
print("Numbers, numbers, and more numbers")


Numbers, numbers, and more numbers

In [18]:
import matplotlib.pyplot as plt
x = np.arange(-20, 20, 0.25)
y = x.reshape(-1, 1)
r = np.sqrt(x**2 + y**2)
z = np.cos(r) / (r + 5)
plt.show(z)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-4c0ff4bbf703> in <module>()
      4 r = np.sqrt(x**2 + y**2)
      5 z = np.cos(r) / (r + 5)
----> 6 plt.show(z)

/usr/local/opt/pyenv/versions/anaconda3-2.1.0/lib/python3.4/site-packages/matplotlib/pyplot.py in show(*args, **kw)
    152     """
    153     global _show
--> 154     return _show(*args, **kw)
    155 
    156 

/usr/local/opt/pyenv/versions/anaconda3-2.1.0/lib/python3.4/site-packages/matplotlib/backend_bases.py in __call__(self, block)
    153 
    154         if block is not None:
--> 155             if block:
    156                 self.mainloop()
    157                 return

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

In [16]:
plt.clf()
plt.imshow(z, origin = 'lower', vmin = -10, vmax = 65)
plt.colorbar()


Out[16]:
<matplotlib.colorbar.Colorbar at 0x10704a0f0>

In [ ]: