numpy rubber duck intro

Where I mainly cut and paste to build muscle memory.

see: https://docs.scipy.org/doc/numpy-dev/user/quickstart.html

From the docs: NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In NumPy dimensions are called axes. The number of axes is rank.


In [1]:
# Seems like everyone aliases numpy
import numpy as np

In [2]:
# Hello world for numpy
a = np.arange(15).reshape(3, 5)

In [3]:
a


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

In [4]:
a.shape


Out[4]:
(3, 5)

In [5]:
a.ndim


Out[5]:
2

In [6]:
a.dtype.name


Out[6]:
'int64'

In [7]:
a.itemsize


Out[7]:
8

In [8]:
a.size


Out[8]:
15

In [9]:
type(a)


Out[9]:
numpy.ndarray

In [10]:
b = np.array([6, 7, 8])

In [11]:
b


Out[11]:
array([6, 7, 8])

In [12]:
type(b)


Out[12]:
numpy.ndarray

In [13]:
coord1 = np.array((1, 1), dtype=int)

In [14]:
coord2 = np.array((1, 2), dtype=int)

In [15]:
# Element addition
coord1 + coord2


Out[15]:
array([2, 3])

In [16]:
# Element multiplication
coord1 * coord2


Out[16]:
array([1, 2])

In [17]:
# Dot product via function
coord1.dot(coord2)


Out[17]:
3

In [18]:
# apply operation from module
np.cross(coord1, coord2)


Out[18]:
array(1)

In [ ]: