import numpy as np
a = np.array([1, 2, 3])
b = np.array([
[1., 1.],
[1., 1.]
])
c = np.array([1,2, 3], dtype="int16")
print(a)
print(b)
print(c)
print('-------')
print(a.ndim)
print(b.ndim)
print(c.ndim)
print('-------')
print(a.shape)
print(b.shape)
print(c.shape)
[1 2 3] [[1. 1.] [1. 1.]] [1 2 3] ------- 1 2 1 ------- (3,) (2, 2) (3,)
Numpy Benefits
Fixed Type
Contigious memory
# get type
print(a.dtype)
print(b.dtype)
print(c.dtype)
int32 float64 int16
# get size - size of each item (B)
print(a.itemsize)
print(b.itemsize)
print(c.itemsize)
4 8 2
# get total size - total space used (B )
print(a.size * a.itemsize)
print(a.nbytes)
print(b.nbytes)
print(c.nbytes)
12 12 32 6
arr = [range(1, 5), range(5, 9)] # list of lists
e = np.array(arr) # 2d array
print(e)
x = e.tolist() # convert array back to list
print(type(x))
[[1 2 3 4] [5 6 7 8]] <class 'list'>
a = np.array(np.arange(24)).reshape((2, 3, 4))
a
array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]])
b = np.append(a, [5, 6, 7, 8])
b
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 5, 6, 7, 8])
b.shape
(28,)
b.reshape((7, 4))
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23], [ 5, 6, 7, 8]])
a
array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]])
c = np.array(np.arange(24)).reshape((2, 3, 4)) * 10 + 3
c
array([[[ 3, 13, 23, 33], [ 43, 53, 63, 73], [ 83, 93, 103, 113]], [[123, 133, 143, 153], [163, 173, 183, 193], [203, 213, 223, 233]]])
np.append(a, c, axis=0)
array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[ 12, 13, 14, 15], [ 16, 17, 18, 19], [ 20, 21, 22, 23]], [[ 3, 13, 23, 33], [ 43, 53, 63, 73], [ 83, 93, 103, 113]], [[123, 133, 143, 153], [163, 173, 183, 193], [203, 213, 223, 233]]])
np.append(a, c, axis=0).shape
(4, 3, 4)
np.append(a, c, axis=1)
array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [ 3, 13, 23, 33], [ 43, 53, 63, 73], [ 83, 93, 103, 113]], [[ 12, 13, 14, 15], [ 16, 17, 18, 19], [ 20, 21, 22, 23], [123, 133, 143, 153], [163, 173, 183, 193], [203, 213, 223, 233]]])
np.append(a, c, axis=1).shape
(2, 6, 4)
np.append(a, c, axis=2)
array([[[ 0, 1, 2, 3, 3, 13, 23, 33], [ 4, 5, 6, 7, 43, 53, 63, 73], [ 8, 9, 10, 11, 83, 93, 103, 113]], [[ 12, 13, 14, 15, 123, 133, 143, 153], [ 16, 17, 18, 19, 163, 173, 183, 193], [ 20, 21, 22, 23, 203, 213, 223, 233]]])
np.append(a, c, axis=2).shape
(2, 3, 8)
after_insert_array = np.insert(c, 1, 444, axis=0)
after_insert_array
array([[[ 3, 13, 23, 33], [ 43, 53, 63, 73], [ 83, 93, 103, 113]], [[444, 444, 444, 444], [444, 444, 444, 444], [444, 444, 444, 444]], [[123, 133, 143, 153], [163, 173, 183, 193], [203, 213, 223, 233]]])
after_insert_array = np.insert(c, 1, 444, axis=1)
after_insert_array
array([[[ 3, 13, 23, 33], [444, 444, 444, 444], [ 43, 53, 63, 73], [ 83, 93, 103, 113]], [[123, 133, 143, 153], [444, 444, 444, 444], [163, 173, 183, 193], [203, 213, 223, 233]]])
after_insert_array = np.insert(c, 1, 444, axis=2)
after_insert_array
array([[[ 3, 444, 13, 23, 33], [ 43, 444, 53, 63, 73], [ 83, 444, 93, 103, 113]], [[123, 444, 133, 143, 153], [163, 444, 173, 183, 193], [203, 444, 213, 223, 233]]])
d = np.empty(c.shape)
np.copyto(d, c)
d
array([[[ 3., 13., 23., 33.], [ 43., 53., 63., 73.], [ 83., 93., 103., 113.]], [[123., 133., 143., 153.], [163., 173., 183., 193.], [203., 213., 223., 233.]]])
np.delete(d, 1, axis=0)
array([[[ 3., 13., 23., 33.], [ 43., 53., 63., 73.], [ 83., 93., 103., 113.]]])
np.delete(d, 1, axis=1)
array([[[ 3., 13., 23., 33.], [ 83., 93., 103., 113.]], [[123., 133., 143., 153.], [203., 213., 223., 233.]]])
np.delete(d, 1, axis=2)
array([[[ 3., 23., 33.], [ 43., 63., 73.], [ 83., 103., 113.]], [[123., 143., 153.], [163., 183., 193.], [203., 223., 233.]]])
a = np.array([[1,2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14]])
print(a)
print(a.shape)
[[ 1 2 3 4 5 6 7] [ 8 9 10 11 12 13 14]] (2, 7)
# get a specific element [r, c]
print(a[1, 5])
print(a[1][5])
print(a[1][-2])
13 13 13
# get a specific row
a[0, :]
array([1, 2, 3, 4, 5, 6, 7])
# get a specific col
a[:, 2]
array([ 3, 10])
# getting fancy [startindex: endindex: stepsize]
print(a[0, 1:6:2])
[2 4 6]
# changing element(s)
a [1, 5] = 20
print(a)
print('-----')
a[:, 2] = 5
print(a)
print('-----')
a[:, 2] = [1, 2]
print(a)
[[ 1 2 3 4 5 6 7] [ 8 9 10 11 12 20 14]] ----- [[ 1 2 5 4 5 6 7] [ 8 9 5 11 12 20 14]] ----- [[ 1 2 1 4 5 6 7] [ 8 9 2 11 12 20 14]]
# 3-d
a = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(a)
print('------')
print(a.shape)
# think of number of rows as original number of rows
# inside each box columns become rows and depth becomes columns
# think of it as [1, 2] at at position a[0, 0]
# NumPy’s order for printing n-dimensional arrays is that the last axis
# is looped over the fastest, while the first is the slowest
[[[1 2] [3 4]] [[5 6] [7 8]]] ------ (2, 2, 2)
print(a[0, 0])
print(a[0, 1])
print(a[1, 0])
print(a[1, 1])
# get specific element
print(a[0,1,1])
[1 2] [3 4] [5 6] [7 8] 4
# replace
a[:, 1, :] = [[10, 11], [12, 13]]
print(a)
print('-----')
a[0, 1, :] = [99, 98]
print(a)
[[[ 1 2] [10 11]] [[ 5 6] [12 13]]] ----- [[[ 1 2] [99 98]] [[ 5 6] [12 13]]]
# all 0's matrix
np.zeros(shape=(2, 3))
array([[0., 0., 0.], [0., 0., 0.]])
# all 1's matrix
np.ones((4, 2, 2), dtype='int32')
array([[[1, 1], [1, 1]], [[1, 1], [1, 1]], [[1, 1], [1, 1]], [[1, 1], [1, 1]]])
# any other number
np.full((2, 2), 99)
array([[99, 99], [99, 99]])
# any other number (full_like)
a = np.array([[1,2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14]])
np.full_like(a, 8)
array([[8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8]])
# random decimal numbers
np.random.rand(4, 2, 3)
array([[[0.72906493, 0.55824471, 0.93723651], [0.398366 , 0.13578472, 0.31933003]], [[0.27063343, 0.90327415, 0.11766311], [0.47930855, 0.93668179, 0.99062391]], [[0.13237077, 0.74166895, 0.72342834], [0.37170613, 0.22426275, 0.13195296]], [[0.34001939, 0.10920622, 0.55247432], [0.09956932, 0.07601353, 0.25140426]]])
np.random.random_sample(a.shape)
array([[0.66350532, 0.53812603, 0.6518355 , 0.00824817, 0.56673366, 0.69292765, 0.02727708], [0.97756697, 0.28137299, 0.74520098, 0.73030616, 0.32350523, 0.28181451, 0.0838475 ]])
# random integer values
print(np.random.randint(7))
print(np.random.randint(7, size=(3, 3)))
0 [[6 3 6] [3 2 0] [2 3 6]]
# identity matrix
np.identity(3)
array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
print(np.logspace(0, 3, 4)) # 10^0 to 10^3 (inclusive) with 4 points
[ 1. 10. 100. 1000.]
# repeat an array
arr = np.array([[1, 2, 3]])
r1 = np.repeat(arr, 3, axis=0)
print(r1)
print('-----')
r2 = np.repeat(arr, 3, axis=1)
print(r2)
[[1 2 3] [1 2 3] [1 2 3]] ----- [[1 1 1 2 2 2 3 3 3]]
# creating a customized matrix
output = np.ones((5, 5))
print(output)
print('-----')
z = np.zeros((3, 3))
z[1, 1] = 9
print(z)
print('-----')
output[1:4, 1: 4] = z
print(output)
[[1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.]] ----- [[0. 0. 0.] [0. 9. 0.] [0. 0. 0.]] ----- [[1. 1. 1. 1. 1.] [1. 0. 0. 0. 1.] [1. 0. 9. 0. 1.] [1. 0. 0. 0. 1.] [1. 1. 1. 1. 1.]]
# careful while copying
a = np.array([1, 2, 3])
b = a # just points
b [0]= 100
print(a)
print(b)
print('-----')
a = np.array([1, 2, 3])
b = a.copy()
b [0]= 100
print(a)
print(b)
[100 2 3] [100 2 3] ----- [1 2 3] [100 2 3]
# adding an axis
a = np.array([0, 1])
a_col = a[:, np.newaxis]
print(a_col
)
[[0] [1]]
a_col.T
array([[0, 1]])
# flatten array
# always returns a flat copy of the original array
arr = np.arange(10, dtype=float).reshape((2, 5))
arr_flat = arr.flatten()
arr_flat[0] = 33
print(arr)
print(arr_flat)
[[0. 1. 2. 3. 4.] [5. 6. 7. 8. 9.]] [33. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
# Ravel: returns a view of the original array whenever possible.
arr_flt = arr.ravel()
arr_flt[0] = 33
print(arr)
print(arr_flt)
[[33. 1. 2. 3. 4.] [ 5. 6. 7. 8. 9.]] [33. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
# creating evenly spaced number over an specified interval
np.linspace(start = 0, stop=50, num=10)
np.linspace(5, 15, 10)
mylinspace = np.linspace(5, 15, 9, retstep=True)
mylinspace
(array([ 5. , 6.25, 7.5 , 8.75, 10. , 11.25, 12.5 , 13.75, 15. ]), 1.25)
a = np.array([1, 2, 3, 4])
print(a)
[1 2 3 4]
a + 2
array([3, 4, 5, 6])
a - 2
array([-1, 0, 1, 2])
a / 2
array([0.5, 1. , 1.5, 2. ])
b = np.array([1, 0, 1, 0])
a + b
array([2, 2, 4, 4])
np.sin(a)
array([ 0.84147098, 0.90929743, 0.14112001, -0.7568025 ])
my_first_matrix = np.matrix([[3, 1, 4], [1, 5, 9], [2, 6, 5]])
my_first_matrix
matrix([[3, 1, 4], [1, 5, 9], [2, 6, 5]])
my_first_matrix.T
matrix([[3, 1, 2], [1, 5, 6], [4, 9, 5]])
my_first_matrix.I # inverse of matrix
matrix([[ 0.32222222, -0.21111111, 0.12222222], [-0.14444444, -0.07777778, 0.25555556], [ 0.04444444, 0.17777778, -0.15555556]])
a = np.ones((2, 3))
print(a)
b = np.full((3, 2), 2)
print(b)
[[1. 1. 1.] [1. 1. 1.]] [[2 2] [2 2] [2 2]]
c = np.identity(4) # or np.eye(5)
print(np.linalg.det(c))
1.0
# Solve simultaneous linear equations
right_hand_side = np.matrix([[11],
[22],
[33]])
my_first_inverse = my_first_matrix.I
solution = my_first_inverse * right_hand_side
solution
matrix([[ 2.93333333], [ 5.13333333], [-0.73333333]])
# more efficient for large matrices
from numpy.linalg import solve
solve(my_first_matrix, right_hand_side)
matrix([[ 2.93333333], [ 5.13333333], [-0.73333333]])
# compute the eigenvalues and eigenvectors
from numpy.linalg import eig
eig(my_first_matrix)
(array([13.08576474, 2.58000566, -2.66577041]), matrix([[-0.31542644, -0.95117074, -0.32372474], [-0.72306109, 0.30781323, -0.70222933], [-0.61456393, 0.02291827, 0.63409484]]))
stats = np.array([[1, 2, 3], [4, 5,6]])
stats
array([[1, 2, 3], [4, 5, 6]])
print(np.min(stats, axis=0))
print(np.min(stats, axis=1))
print(np.max(stats, axis=0))
print(np.max(stats, axis=1))
[1 2 3] [1 4] [4 5 6] [3 6]
np.sum(stats, axis=0)
array([5, 7, 9])
rnd = np.random.randn(4, 2) # random normals in 4x2 array
print(rnd.mean())
print(rnd.std())
print(rnd.argmin()) # index of minimum element
print(rnd.sum())
print(rnd.sum(axis=0)) # sum of columns
print(rnd.sum(axis=1)) # sum of rows
-0.0025402071626459966 1.103773849306929 3 -0.020321657301167972 [ 1.08423164 -1.1045533 ] [-0.565446 -1.38446781 2.39228074 -0.46268859]
filedata = np.genfromtxt('data.txt', delimiter=',')
filedata = filedata.astype('int32')
print(filedata)
[[ 1 13 21 11 196 75 4 3 34 6 7 8 0 1 2 3 4 5] [ 3 42 12 33 766 75 4 55 6 4 3 4 5 6 7 0 11 12] [ 1 22 33 11 999 11 2 1 78 0 1 2 9 8 7 1 76 88]]
filedata > 50
array([[False, False, False, False, True, True, False, False, False, False, False, False, False, False, False, False, False, False], [False, False, False, False, True, True, False, True, False, False, False, False, False, False, False, False, False, False], [False, False, False, False, True, False, False, False, True, False, False, False, False, False, False, False, True, True]])
filedata[filedata > 50]
array([196, 75, 766, 75, 55, 999, 78, 76, 88])
# we can index using a list
a = np.array([1, 2, 3, 4,5, 6, 7,8,9])
a[[1, 2, 8]]
array([2, 3, 9])
np.any(filedata > 50, axis= 0)
array([False, False, False, False, True, True, False, True, True, False, False, False, False, False, False, False, True, True])
np.all(filedata > 50, axis= 0)
array([False, False, False, False, True, False, False, False, False, False, False, False, False, False, False, False, False, False])
((filedata > 50) & (filedata < 100))
array([[False, False, False, False, False, True, False, False, False, False, False, False, False, False, False, False, False, False], [False, False, False, False, False, True, False, True, False, False, False, False, False, False, False, False, False, False], [False, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False, True, True]])
(~(filedata > 50) & (filedata < 100))
array([[ True, True, True, True, False, False, True, True, True, True, True, True, True, True, True, True, True, True], [ True, True, True, True, False, False, True, False, True, True, True, True, True, True, True, True, True, True], [ True, True, True, True, False, True, True, True, False, True, True, True, True, True, True, True, False, False]])
filedata[((filedata > 50) & (filedata < 100))]
array([75, 75, 55, 78, 76, 88])
my_vector = np.array([-17, -4, 0, 2, 21, 37, 105])
zero_mod_7_mask = 0 == (my_vector % 7)
print(zero_mod_7_mask)
[False False True False True False True]
my_vector[zero_mod_7_mask]
array([ 0, 21, 105])
mod_test = 0 == (my_vector % 7)
posmask = my_vector > 0
combined_mask = np.logical_and(mod_test, posmask)
# combined_mask
print(my_vector[combined_mask])
[ 21 105]
# example indexing
a = np.arange(1, 11).reshape((2, 5))
b = np.arange(11, 21).reshape((2, 5))
c = np.arange(21, 31).reshape((2, 5))
final = np.vstack([a, b, c])
print(final)
[[ 1 2 3 4 5] [ 6 7 8 9 10] [11 12 13 14 15] [16 17 18 19 20] [21 22 23 24 25] [26 27 28 29 30]]
final[2:4, :2]
array([[11, 12], [16, 17]])
final[[0, 1, 2, 3], [1, 2, 3, 4]]
array([ 2, 8, 14, 20])
final[[0, 4, 5], 3:]
array([[ 4, 5], [24, 25], [29, 30]])
used for heterogeneous data while maintaining numpy's requirement that every element in an array use the same amount of memory space
person_data_def = [('name', '<U6'), ('height', 'f8'), ('weight', 'f8'), ('age', 'i8')]
person_data_def
[('name', '<U6'), ('height', 'f8'), ('weight', 'f8'), ('age', 'i8')]
people_array = np.zeros((4), dtype=person_data_def)
people_array
array([('', 0., 0., 0), ('', 0., 0., 0), ('', 0., 0., 0), ('', 0., 0., 0)], dtype=[('name', '<U6'), ('height', '<f8'), ('weight', '<f8'), ('age', '<i8')])
# https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html
# https://jakevdp.github.io/PythonDataScienceHandbook/02.09-structured-data-numpy.html
# dt = np.dtype('i4') # 32-bit signed integer
# dt = np.dtype('f8') # 64-bit floating-point number
# dt = np.dtype('c16') # 128-bit complex floating-point number
# dt = np.dtype('a25') # 25-length zero-terminated bytes
# dt = np.dtype('U25') # 25-character string
people_array
array([('', 0., 0., 0), ('', 0., 0., 0), ('', 0., 0., 0), ('', 0., 0., 0)], dtype=[('name', '<U6'), ('height', '<f8'), ('weight', '<f8'), ('age', '<i8')])
people_array[3] = ('delta', 73, 58, 28)
people_array[3]
('delta', 73., 58., 28)
people_array[1] = ('alpha', 83, 38, 48)
people_array[1]
('alpha', 83., 38., 48)
people_array
array([('', 0., 0., 0), ('alpha', 83., 38., 48), ('', 0., 0., 0), ('delta', 73., 58., 28)], dtype=[('name', '<U6'), ('height', '<f8'), ('weight', '<f8'), ('age', '<i8')])
people_array['age']
array([ 0, 48, 0, 28], dtype=int64)
people_big_array = np.zeros((4, 3, 2), dtype=person_data_def)
people_big_array
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), ('', 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)]], [[('', 0., 0., 0), ('', 0., 0., 0)], [('', 0., 0., 0), ('', 0., 0., 0)], [('', 0., 0., 0), ('', 0., 0., 0)]]], dtype=[('name', '<U6'), ('height', '<f8'), ('weight', '<f8'), ('age', '<i8')])
people_big_array[3, 2, 1] = ('echo', 10, 20, 30)
people_big_array
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), ('', 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)]], [[('', 0., 0., 0), ('', 0., 0., 0)], [('', 0., 0., 0), ('', 0., 0., 0)], [('', 0., 0., 0), ('echo', 10., 20., 30)]]], dtype=[('name', '<U6'), ('height', '<f8'), ('weight', '<f8'), ('age', '<i8')])
# creating Record arrays
person_record_array = np.rec.array([('Delta', 73, 205, 34), ('alpha', 83, 38, 48)], dtype=person_data_def)
person_record_array
rec.array([('Delta', 73., 205., 34), ('alpha', 83., 38., 48)], dtype=[('name', '<U6'), ('height', '<f8'), ('weight', '<f8'), ('age', '<i8')])
person_record_array[0].age
# using attributes instead of index
34
before = np.array([[1, 2, 3 , 4], [5, 6, 7, 8]])
print(before)
print(before.reshape((8,1)))
[[1 2 3 4] [5 6 7 8]] [[1] [2] [3] [4] [5] [6] [7] [8]]
# vertically stacking arrays
v1 = np.array([1, 2, 3, 4])
v2 = np.array([11, 22, 33, 44])
np.vstack((v1, v2, v2))
array([[ 1, 2, 3, 4], [11, 22, 33, 44], [11, 22, 33, 44]])
# horizontal stacking
h1 = np.ones((2, 4))
h2 = np.zeros((2, 2))
np.hstack([h1, h2])
array([[1., 1., 1., 1., 0., 0.], [1., 1., 1., 1., 0., 0.]])
my_start_array = np.array(np.arange(24))
my_3_8_array = my_start_array.reshape((3, 8))
my_2_3_4_array = my_start_array.reshape((2, 3, 4))
# fliplr - flip left right
my_3_8_array
array([[ 0, 1, 2, 3, 4, 5, 6, 7], [ 8, 9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22, 23]])
np.fliplr(my_3_8_array)
array([[ 7, 6, 5, 4, 3, 2, 1, 0], [15, 14, 13, 12, 11, 10, 9, 8], [23, 22, 21, 20, 19, 18, 17, 16]])
my_2_3_4_array
array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]])
np.fliplr(my_2_3_4_array) # flipping takes place over the last index
array([[[ 8, 9, 10, 11], [ 4, 5, 6, 7], [ 0, 1, 2, 3]], [[20, 21, 22, 23], [16, 17, 18, 19], [12, 13, 14, 15]]])
# flip upside down
np.flipud(my_3_8_array)
array([[16, 17, 18, 19, 20, 21, 22, 23], [ 8, 9, 10, 11, 12, 13, 14, 15], [ 0, 1, 2, 3, 4, 5, 6, 7]])
np.flipud(my_2_3_4_array)
array([[[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]], [[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]])
my_start_array
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])
# roll
np.roll(my_start_array, 5)
array([19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18])
np.roll(my_start_array, -5)
array([ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4])
np.roll(my_2_3_4_array, 2)
array([[[22, 23, 0, 1], [ 2, 3, 4, 5], [ 6, 7, 8, 9]], [[10, 11, 12, 13], [14, 15, 16, 17], [18, 19, 20, 21]]])
my_3_8_array
array([[ 0, 1, 2, 3, 4, 5, 6, 7], [ 8, 9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22, 23]])
# rotate 90 degree
np.rot90(my_3_8_array) # rotate in +ve direction (counter-clockwise)
array([[ 7, 15, 23], [ 6, 14, 22], [ 5, 13, 21], [ 4, 12, 20], [ 3, 11, 19], [ 2, 10, 18], [ 1, 9, 17], [ 0, 8, 16]])
np.rot90(my_3_8_array, k=-1) # rotate in -ve direction (clockwise)
array([[16, 8, 0], [17, 9, 1], [18, 10, 2], [19, 11, 3], [20, 12, 4], [21, 13, 5], [22, 14, 6], [23, 15, 7]])
my_start_array = np.array(np.arange(24))
my_3_8_array = my_start_array.reshape((3, 8))
my_2_3_4_array = my_start_array.reshape((2, 3, 4))
print(my_start_array)
print('-----')
print(my_start_array.T)
# or
# print(np.transpose(my_start_array))
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23] ----- [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
print(my_3_8_array)
print('-----')
print(my_3_8_array.T)
[[ 0 1 2 3 4 5 6 7] [ 8 9 10 11 12 13 14 15] [16 17 18 19 20 21 22 23]] ----- [[ 0 8 16] [ 1 9 17] [ 2 10 18] [ 3 11 19] [ 4 12 20] [ 5 13 21] [ 6 14 22] [ 7 15 23]]
print(my_2_3_4_array)
print('-----')
print(np.transpose(my_2_3_4_array, axes=(0,2,1)))
# transpose over axes index by 2 and axes index by 1
# axes = By default, reverse the dimensions,
# otherwise permute the axes according to the values given.
[[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]] ----- [[[ 0 4 8] [ 1 5 9] [ 2 6 10] [ 3 7 11]] [[12 16 20] [13 17 21] [14 18 22] [15 19 23]]]
# swapaxes(a, axis1, axis2) - interchange two axes of an array
print(my_2_3_4_array)
print('-----')
print(np.swapaxes(my_2_3_4_array, 1, 0) )
[[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]] ----- [[[ 0 1 2 3] [12 13 14 15]] [[ 4 5 6 7] [16 17 18 19]] [[ 8 9 10 11] [20 21 22 23]]]
# np.rollaxis - roll the specified axis backwards, until it lies in a given position
print(my_2_3_4_array.shape)
print('-----')
print(np.rollaxis(my_2_3_4_array, axis=1, start=3).shape)
# axis 3 is not present but theoretically will be after axis 2 so axis
# 1 is rolled till it is behind axis 3
(2, 3, 4) ----- (2, 4, 3)
print(my_2_3_4_array.shape)
print('-----')
print(np.rollaxis(my_2_3_4_array, axis=1).shape)
print(np.rollaxis(my_2_3_4_array, axis=2, start=1).shape)
(2, 3, 4) ----- (3, 2, 4) (2, 4, 3)
# np.moveaxis(a, source, destination)
# Move axes of an array to new positions.
# Other axes remain in their original order.
print(my_2_3_4_array.shape)
print('-----')
print(np.moveaxis(my_2_3_4_array, 0, -1).shape)
print(np.moveaxis(my_2_3_4_array, -1, 0).shape)
(2, 3, 4) ----- (3, 4, 2) (4, 2, 3)
# truncated binomial: returns (x+1) ** 3 - (x) ** 3
def truncated_binomial(x):
return (x+1) ** 3 - (x) ** 3
np.testing.assert_equal(truncated_binomial(4), 61)
np.testing.assert_equal(truncated_binomial(4), 65)
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) <ipython-input-135-c7c9431d1595> in <module> ----> 1 np.testing.assert_equal(truncated_binomial(4), 65) ~\Anaconda3\lib\site-packages\numpy\testing\_private\utils.py in assert_equal(actual, desired, err_msg, verbose) 415 # Explicitly use __eq__ for comparison, gh-2552 416 if not (desired == actual): --> 417 raise AssertionError(msg) 418 419 except (DeprecationWarning, FutureWarning) as e: AssertionError: Items are not equal: ACTUAL: 61 DESIRED: 65
my_numpy_function = np.frompyfunc(truncated_binomial, 1, 1)
my_numpy_function
test_array = np.arange(10)
my_numpy_function(test_array)
big_test_array = np.outer(test_array, test_array)
big_test_array
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-136-00440f746be4> in <module> ----> 1 big_test_array = np.outer(test_array, test_array) 2 big_test_array NameError: name 'test_array' is not defined
my_numpy_function(big_test_array)
$X^n + Y^n = Z ^n$
def is_integer(x):
return np.equal(np.mod(x, 1), 0)
numpy_is_integer = np.frompyfunc(is_integer, 1, 1)
number_of_triangles = 9
base = np.arange(number_of_triangles) + 1
height = np.arange(number_of_triangles) + 1
# https://docs.scipy.org/doc/numpy/reference/generated/numpy.ufunc.outer.html
hypotenuse_squared = np.add.outer(base ** 2, height ** 2)
hypotenuse = np.sqrt(hypotenuse_squared)
numpy_is_integer(hypotenuse)
array([[False, False, False, False, False, False, False, False, False], [False, False, False, False, False, False, False, False, False], [False, False, False, True, False, False, False, False, False], [False, False, True, False, False, False, False, False, False], [False, False, False, False, False, False, False, False, False], [False, False, False, False, False, False, False, True, False], [False, False, False, False, False, False, False, False, False], [False, False, False, False, False, True, False, False, False], [False, False, False, False, False, False, False, False, False]], dtype=object)
Another method
for $m$ and $n$ $+ve$ integers, and m $\geq$ n: $X = m^2 - n^2; Y= 2mn; Z = m^2 + n^2$