In [1]:
from platform import python_version

In [2]:
python_version()


Out[2]:
'3.6.7'

In [3]:
import numpy as np

In [4]:
np.__version__


Out[4]:
'1.16.3'

reverse


In [5]:
myarray = np.array([1,2,3])
myarray[::-1]


Out[5]:
array([3, 2, 1])

In [6]:
myarray = np.array([
    [1,2,3],
    [4,5,6],
    [7,8,9]
])
myarray[::-1]


Out[6]:
array([[7, 8, 9],
       [4, 5, 6],
       [1, 2, 3]])

scientific notation


In [7]:
# np.set_printoptions(edgeitems=3,infstr='inf',
# linewidth=75, nanstr='nan', precision=8,
# suppress=False, threshold=1000, formatter=None)

y=np.array([1.0e-10,1.0,100,1000])
y


Out[7]:
array([1.e-10, 1.e+00, 1.e+02, 1.e+03])

In [8]:
np.set_printoptions(suppress=True)
y


Out[8]:
array([   0.,    1.,  100., 1000.])

selecting


In [9]:
myarray = np.array([
    [1,2,3],
    [4,5,6],
    [7,8,9]
])
myarray


Out[9]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

In [10]:
myarray[:,1]


Out[10]:
array([2, 5, 8])

In [11]:
myarray[0,:]


Out[11]:
array([1, 2, 3])

In [12]:
x = np.array([
    [1,2,3],
    [4,5,6]
])

In [13]:
y = x.copy()

In [14]:
y.fill(1)

In [15]:
x


Out[15]:
array([[1, 2, 3],
       [4, 5, 6]])

In [16]:
y


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

add row to multimensional array


In [17]:
myarray = np.array([
    [1,2,3],
    [4,5,6]
])

In [18]:
newrow = np.array([9,9,9])

In [19]:
np.vstack([myarray,newrow])


Out[19]:
array([[1, 2, 3],
       [4, 5, 6],
       [9, 9, 9]])

add column


In [20]:
myarray = np.array([
    [1,2,3],
    [4,5,6]
])

In [21]:
newcol = np.array([[9],[9]])

In [22]:
np.hstack([myarray,newcol])


Out[22]:
array([[1, 2, 3, 9],
       [4, 5, 6, 9]])

compare equality


In [28]:
a = np.array([1,2,3])
b = np.array([0,0,0])
np.array_equal(a,b)


Out[28]:
False

In [32]:
a = np.array([1,np.nan,3])
b = np.array([1,np.nan,3])

np.array_equal(a,b)


Out[32]:
False

In [33]:
a = np.array([1,np.nan,3])
b = np.array([1,np.nan,3])
np.allclose(a, b, equal_nan=True)


Out[33]:
True

In [30]:
a = np.array([1,2,3])
b = np.array([1,2,3])

np.array_equal(a,b)


Out[30]:
True