Numpy

Errors

In [121]:
x = 1.0 / 3.0
print(x)

y = 10**30
print(y)

print(x * y)
print('{0:f}'.format(x * y))
0.3333333333333333
1000000000000000000000000000000
3.333333333333333e+29
333333333333333316505293553664.000000

Basics

In [1]:
import numpy as np
In [7]:
x = np.array([1,2,3,4])
x
Out[7]:
array([1, 2, 3, 4])
In [14]:
x = np.array([[1,2,3,4], [5,6,7,8]])
x
Out[14]:
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
In [18]:
x = np.ones([2,3])
x
Out[18]:
array([[1., 1., 1.],
       [1., 1., 1.]])
In [19]:
x = np.zeros([2,3])
x
Out[19]:
array([[0., 0., 0.],
       [0., 0., 0.]])
In [69]:
x = np.array([[1.0, 2.0], [3.0, 4.0]])
y = np.array([[5.0, 6.0], [7.0, 8.0]])

print(x)
print(y)
[[1. 2.]
 [3. 4.]]
[[5. 6.]
 [7. 8.]]
In [44]:
print(x + y)
[[ 6.  8.]
 [10. 12.]]
In [46]:
print(x - y)
[[-4. -4.]
 [-4. -4.]]
In [27]:
print(x * y)
[[ 5. 12.]
 [21. 32.]]
In [30]:
print(np.matmul(x, y))
[[19. 22.]
 [43. 50.]]
In [47]:
print(x**2)
[[ 1.  4.]
 [ 9. 16.]]

Types

In [48]:
x = np.array([[1, 2], [3, 4]])
x.dtype
Out[48]:
dtype('int32')
In [49]:
x = np.array([[1.0, 2.0], [3.0, 4.0]])
x.dtype
Out[49]:
dtype('float64')
In [50]:
x = np.array([[1, 2], [3.0, 4.0]])
x.dtype
Out[50]:
dtype('float64')
In [51]:
x = np.array([[True, False],[False, True]])
print(x)
x.dtype
[[ True False]
 [False  True]]
Out[51]:
dtype('bool')
In [52]:
x.astype(int)
Out[52]:
array([[1, 0],
       [0, 1]])

Slicing

In [55]:
x = np.arange(0.0, 10.0, 0.1)
x
Out[55]:
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1, 1.2,
       1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4, 2.5,
       2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8,
       3.9, 4. , 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1,
       5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6. , 6.1, 6.2, 6.3, 6.4,
       6.5, 6.6, 6.7, 6.8, 6.9, 7. , 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7,
       7.8, 7.9, 8. , 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 9. ,
       9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9])
In [56]:
x.shape
Out[56]:
(100,)
In [63]:
x = x.reshape([10, 10])
x
Out[63]:
array([[0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
       [1. , 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9],
       [2. , 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9],
       [3. , 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9],
       [4. , 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9],
       [5. , 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9],
       [6. , 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8, 6.9],
       [7. , 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, 7.8, 7.9],
       [8. , 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9],
       [9. , 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9]])
In [65]:
x[0]
Out[65]:
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
In [66]:
x[:,0]
Out[66]:
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
In [67]:
x[2,3]
Out[67]:
2.3000000000000003
In [68]:
x[4:8,1:4]
Out[68]:
array([[4.1, 4.2, 4.3],
       [5.1, 5.2, 5.3],
       [6.1, 6.2, 6.3],
       [7.1, 7.2, 7.3]])
In [78]:
x = np.array([[1.0, 2.0], [3.0, 4.0]])
y = np.array([[5.0, 6.0], [7.0, 8.0]])

print(x)
print(y)
[[1. 2.]
 [3. 4.]]
[[5. 6.]
 [7. 8.]]
In [75]:
np.vstack([x, y])
Out[75]:
array([[1., 2.],
       [3., 4.],
       [5., 6.],
       [7., 8.]])
In [76]:
np.hstack([x, y])
Out[76]:
array([[1., 2., 5., 6.],
       [3., 4., 7., 8.]])
In [80]:
x = np.array([1.0, 2.0])
y = np.array([[5.0, 6.0], [7.0, 8.0]])

print(x)
print(y)
[1. 2.]
[[5. 6.]
 [7. 8.]]
In [81]:
np.vstack([x,y])
Out[81]:
array([[1., 2.],
       [5., 6.],
       [7., 8.]])
In [82]:
np.hstack([x,y])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-82-65c95db36c10> in <module>()
----> 1 np.hstack([x,y])

~\Anaconda3\lib\site-packages\numpy\core\shape_base.py in hstack(tup)
    284     # As a special case, dimension 0 of 1-dimensional arrays is "horizontal"
    285     if arrs and arrs[0].ndim == 1:
--> 286         return _nx.concatenate(arrs, 0)
    287     else:
    288         return _nx.concatenate(arrs, 1)

ValueError: all the input arrays must have same number of dimensions

Tutorial

In [132]:
import numpy as np
import matplotlib.pyplot as pl

def mandelbrot(h, w, maxit=20):
    """Returns an image of the Mandelbrot fractal of size (h,w)."""
    y, x = np.ogrid[-1.4:1.4:h*1j, -2:0.8:w*1j]
    c = x + y * 1j
    z = c
    divtime = maxit + np.zeros(z.shape, dtype=int)

    for i in range(maxit):
        z = z**2 + c
        diverge = z * np.conj(z) > 2**2       # who is diverging
        div_now = diverge & (divtime==maxit)  # who is diverging now
        divtime[div_now] = i                  # note when
        z[diverge] = 2                        # avoid diverging too much

    return divtime

pl.imshow(mandelbrot(400,400))
Out[132]:
<matplotlib.image.AxesImage at 0x20ab1a9e048>