In [7]:
%autosave 20
import numpy as np
Autosaving every 20 seconds
In [2]:
print('Hello')
print('World')
a = 123
Hello
World
In [3]:
print(a)
123
In [4]:
pi = 3.14
r = 1
2 * pi * r
Out[4]:
6.28
In [5]:
%timeit '{} {}'.format('hello', 'world')
%timeit 'hello' + ' ' + 'world'
414 ns ± 2.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
15.1 ns ± 0.0186 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
In [6]:
%%timeit
pi = 3.14
r = 1
2 * pi * r
107 ns ± 0.0872 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [19]:
a = np.array([1, 2, 3, 4], dtype=np.uint8)
a[0] - a[1]
/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:2: RuntimeWarning: overflow encountered in ubyte_scalars
  
Out[19]:
255
In [20]:
a = np.linspace(0, 1, 11)
a
Out[20]:
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
In [31]:
a = np.arange(0, 1+1/9, 1/9, dtype=float)
a
Out[31]:
array([0.        , 0.11111111, 0.22222222, 0.33333333, 0.44444444,
       0.55555556, 0.66666667, 0.77777778, 0.88888889, 1.        ,
       1.11111111])
In [32]:
a = np.arange(10, 0, -1)
a
Out[32]:
array([10,  9,  8,  7,  6,  5,  4,  3,  2,  1])
In [39]:
a, delta = np.linspace(1, 0, 10, endpoint=False, retstep=True)
print(a, delta)
[1.  0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1] -0.1
In [43]:
np.zeros(10, dtype=str)
Out[43]:
array(['', '', '', '', '', '', '', '', '', ''], dtype='<U1')
In [46]:
np.ones(5, dtype=float)
Out[46]:
array([1., 1., 1., 1., 1.])
In [49]:
np.full(5, np.pi, dtype=int)
Out[49]:
array([3, 3, 3, 3, 3])
In [50]:
np.array([{1,2,3}, ('abc', 'def',), 'x'])
Out[50]:
array([{1, 2, 3}, ('abc', 'def'), 'x'], dtype=object)
In [55]:
np.empty(7, dtype=np.int32)
Out[55]:
array([25911424,        0,        0,    32725,        1,        0,
             -1], dtype=int32)
In [59]:
a = np.arange(5, dtype=int)
b = np.ones_like(a)
b1 = np.ones(len(a), dtype=a.dtype)
print(b, b1)
c = np.zeros_like(a, dtype=complex)
print(c)
[1 1 1 1 1] [1 1 1 1 1]
[0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j]
In [78]:
list0 = list(range(10))
assert list0[:3] == list0[0:3]
print(list0[2:-0])
assert list0[2:] == list0[2:len(list0)]
print(list0[:len(list0)//2])
print(list0[::-1])
list0_slice = list0[2:5]
list0_slice[0] = -100
print(list0, list0_slice)
[]
[0, 1, 2, 3, 4]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [-100, 3, 4]
In [81]:
s = 'abcdefgh'
print(s[2:5])
sl = slice(2, 5)
assert s[sl] == s[2:5]
cde
In [91]:
a = np.arange(10)
b = a[5:8]
b[0] = -100
print(a, b)
print(a.flags.owndata, b.flags.owndata)
print(a, '\n', b.base)
assert a.base is None
[   0    1    2    3    4 -100    6    7    8    9] [-100    6    7]
True False
[   0    1    2    3    4 -100    6    7    8    9] 
 [   0    1    2    3    4 -100    6    7    8    9]
In [94]:
a = np.arange(10, dtype=int)
b = np.zeros_like(a, dtype=float)
# b = a  -  a is b
b[:] = a  #  -  a == b  
# b = a.copy()  # - a == b
assert a is not b
print(a, b)
[0 1 2 3 4 5 6 7 8 9] [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
In [99]:
a = np.arange(5)
b = np.zeros(10)
b[:5] = a
print(b)
b[5:] = np.pi
d = np.ones_like(b)
c = b + d
print(c)
b[::2] *= -1
print(b)
[0. 1. 2. 3. 4. 0. 0. 0. 0. 0.]
[1.         2.         3.         4.         5.         4.14159265
 4.14159265 4.14159265 4.14159265 4.14159265]
[-0.          1.         -2.          3.         -4.          3.14159265
 -3.14159265  3.14159265 -3.14159265  3.14159265]
In [107]:
a = np.arange(10)
a[0] = 100
b = a[:1]
assert not b.flags.owndata
print(b)
[100]
In [116]:
a = np.arange(10)**2
idx = [3, 5, 1, 1]
b = a[idx]
assert b.flags.owndata
print(b)
print(a[2], a[(2,)], a[[2,]])
a[idx] = np.array([-1, -2, -3, -4])
print(a)
[ 9 25  1  1]
4 4 [4]
[ 0 -4  4 -1 16 -2 36 49 64 81]
In [118]:
a = np.arange(4)**2
idx = [False, True, True, False]
b = a[idx]
assert b.flags.owndata
print(a, idx, b)
[0 1 4 9] [False, True, True, False] [1 4]
In [124]:
a = np.arange(10)
idx = a % 2 == 0
print(a[idx])
print(a > 5)
print(a[a > 5])
a[a > 5] *= -1
print(a)
[0 2 4 6 8]
[False False False False False False  True  True  True  True]
[6 7 8 9]
[ 0  1  2  3  4  5 -6 -7 -8 -9]
In [127]:
x = np.linspace(0, 5*np.pi, 10)
y = np.sin(x)
x[y > 0]
Out[127]:
array([ 1.74532925,  6.98131701,  8.72664626, 13.96263402, 15.70796327])
In [131]:
a = np.arange(5)
b = a.copy()
if np.array_equal(a, b):
    print('a == b')
a == b
In [139]:
a = np.array([[1, 2, 2.6],
              [3, 3.14, 4]], dtype=float)
print(a.dtype, a.ndim, a.shape, a.size)
assert a.ndim == len(a.shape)
assert np.prod(a.shape) == a.size
float64 2 (2, 3) 6
In [142]:
np.ones(shape=(5, 6), dtype=complex)
Out[142]:
array([[1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j],
       [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j],
       [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j],
       [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j],
       [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j]])
In [159]:
a = np.array([[1, 2, 2.6],
              [3, 3.14, 4]], dtype=float)
b = a.T  # a.transpose()
assert np.array_equal(b.base, a)
b[0,0] = -100
assert a[1][2] == a[1,2]
a[:,:-1]
Out[159]:
array([[-100.  ,    2.  ],
       [   3.  ,    3.14]])
In [164]:
a = np.arange(16)
print(a.shape)
b = a.reshape(4, 4)
print(b.shape)
print(b)
b = a.reshape(2, 2, 2, 2)
print(b.ndim)
a.shape = (4, 4)
# a.resize(4, 4)
print(a)
(16,)
(4, 4)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
4
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]