Notes by Jinyu Du
Mar.5.2022
Link to the course is here
Course author: Terezija Semenski
NumPy is the most important package for scientific computing in Python. It is the base for many other packages. It can do things such as arithemetic operations, statistics operations, bitwise operations, copying and viewing arrays, stacking, searching, sorting, counting, mathematical operations, linear algegra, broadcasting, matrix operations.
Why NumPy?
Python lists have:
But NumPy arrays do:
NumPy code is cleaner. That is, same tasks with fewer lines and fewer loops because operations work directly work on vectors and matrices. NumPy has advanced mathematical functions. Machine leaning packages use NumPy heavily. Some examples of packages that use NumPy features are SciPy, scikit learn, Pandas, matplotlib, statsmodel.
import numpy as np
# lists
%time
my_list_1 = [1, 3, 7]
my_list_2 = [i*5 for i in my_list_1]
my_list_2
CPU times: user 1 µs, sys: 0 ns, total: 1 µs Wall time: 5.01 µs
[5, 15, 35]
# NumPy arrays
%time
my_array_1 = np.array([1, 3, 7])
my_array_2 = my_array_1*5
my_array_2
CPU times: user 1 µs, sys: 0 ns, total: 1 µs Wall time: 3.1 µs
array([ 5, 15, 35])
# indexing
my_array_1[0]
1
# modify array elements
my_array_1[1] = 21
my_array_1
array([ 1, 21, 7])
my_array_1[1] = 21.7
my_array_1
array([ 1, 21, 7])
Notice that the value is truncated to integer. This is because in NumPy, all the data has to be of the same type.
# check the array data type
my_array_1.dtype
dtype('int64')
# check the memory usage
my_array_1.nbytes
24
# change the array data type
my_array_3 = np.array(my_array_1, dtype=np.int8)
my_array_3.dtype
dtype('int8')
# check the memory usage
my_array_3.nbytes
3
floats_array = np.array([1.2,2.3,3.4,5.1,8.3])
floats_array
array([1.2, 2.3, 3.4, 5.1, 8.3])
A one-dimensional array is a vector.
$$\left[\begin{array}{c} 1\\ 7 \end{array}\right]$$A two-dimensional array is a matrix.
$$\left[\begin{array}{cc} 1 & 2\\ 7 & 19 \end{array}\right]$$A three-dimensional array is a tensor.
$$\left[\begin{array}{cc} \left[\begin{array}{cc} 1 & 2\\ 7 & 19 \end{array}\right] & \left[\begin{array}{cc} 1 & 2\\ 7 & 19 \end{array}\right]\\ \left[\begin{array}{cc} 1 & 2\\ 7 & 19 \end{array}\right] & \left[\begin{array}{cc} 1 & 2\\ 7 & 19 \end{array}\right] \end{array}\right]$$array_2d = np.array([[1,2,3, 4, 5], [6, 7, 8, 9, 10]])
array_2d
array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]])
array_2d[0,0]
1
array_2d[1,3]
9
array_2d.ndim
2
multi_arr = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
multi_arr
array([[[ 1, 2, 3], [ 4, 5, 6]], [[ 7, 8, 9], [10, 11, 12]]])
multi_arr[1,0,2]
9
first_list=[1,2,3,4,5,6,7,8,9,10]
first_array=np.array(first_list)
first_array
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
third_list=['Ann',111111,'Peter',111112,'Susan',111113,'John',111114]
third_array=np.array(third_list)
third_array
array(['Ann', '111111', 'Peter', '111112', 'Susan', '111113', 'John', '111114'], dtype='<U21')
first_tuple=(5, 10, 15, 20, 25, 30)
array_from_tuple=np.array(first_tuple)
array_from_tuple
array([ 5, 10, 15, 20, 25, 30])
multi_dim_list=[[[0,1,2], [3,4,5]], [[6,7,8],[9,10,11]]]
arr_from_multi_dim_list=np.array(multi_dim_list)
arr_from_multi_dim_list
array([[[ 0, 1, 2], [ 3, 4, 5]], [[ 6, 7, 8], [ 9, 10, 11]]])
An NumPy array must have elements of the SAME type.
integers_array=np.arange(10)
integers_array
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
integers_second_array=np.arange(100,130)
integers_second_array
array([100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129])
integers_third_array=np.arange(100,151,2)
integers_third_array
array([100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150])
# default to 50 data points
first_floats_arr=np.linspace(10,20)
first_floats_arr
array([10. , 10.20408163, 10.40816327, 10.6122449 , 10.81632653, 11.02040816, 11.2244898 , 11.42857143, 11.63265306, 11.83673469, 12.04081633, 12.24489796, 12.44897959, 12.65306122, 12.85714286, 13.06122449, 13.26530612, 13.46938776, 13.67346939, 13.87755102, 14.08163265, 14.28571429, 14.48979592, 14.69387755, 14.89795918, 15.10204082, 15.30612245, 15.51020408, 15.71428571, 15.91836735, 16.12244898, 16.32653061, 16.53061224, 16.73469388, 16.93877551, 17.14285714, 17.34693878, 17.55102041, 17.75510204, 17.95918367, 18.16326531, 18.36734694, 18.57142857, 18.7755102 , 18.97959184, 19.18367347, 19.3877551 , 19.59183673, 19.79591837, 20. ])
second_floats_arr=np.linspace(10,20,5)
second_floats_arr
array([10. , 12.5, 15. , 17.5, 20. ])
# NumPy random library and rand function
# one-dimensionay array
first_rand_arr=np.random.rand(10)
first_rand_arr
array([0.65923737, 0.84962674, 0.02791938, 0.08742444, 0.48361135, 0.97312373, 0.9668202 , 0.97171537, 0.43305084, 0.84293931])
# two-dimensionay array
second_rand_arr=np.random.rand(4,4)
second_rand_arr
array([[0.653541 , 0.5033519 , 0.89511225, 0.29628853], [0.46767686, 0.79973228, 0.10465297, 0.83364617], [0.53954257, 0.32151762, 0.06551745, 0.50995447], [0.60957203, 0.28763666, 0.61075859, 0.199069 ]])
# randint generates random integers from
# (start, end, num_elements)
third_rand_arr=np.random.randint(0,100,20)
third_rand_arr
array([92, 87, 28, 84, 95, 50, 55, 17, 78, 40, 84, 61, 85, 74, 29, 50, 6, 19, 9, 96])
first_z_array=np.zeros(5)
first_z_array
array([0., 0., 0., 0., 0.])
second_z_array=np.zeros((4,5))
second_z_array
array([[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]])
first_ones_array=np.ones(6)
first_ones_array
array([1., 1., 1., 1., 1., 1.])
second_ones_array=np.ones((7,8))
second_ones_array
array([[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., 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., 1., 1., 1., 1., 1., 1.]])
third_ones_array=np.ones((4,5),dtype=int)
third_ones_array
array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]])
first_fill_array=np.empty(10,dtype=int)
first_fill_array.fill(12)
first_fill_array
array([12, 12, 12, 12, 12, 12, 12, 12, 12, 12])
first_full_array=np.full(5,10)
first_full_array
array([10, 10, 10, 10, 10])
second_full_array=np.full((4,5),8)
second_full_array
array([[8, 8, 8, 8, 8], [8, 8, 8, 8, 8], [8, 8, 8, 8, 8], [8, 8, 8, 8, 8]])
first_arr=np.arange(20)
first_arr
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
second_arr=np.linspace((1,2),(10,20),10)
second_arr
array([[ 1., 2.], [ 2., 4.], [ 3., 6.], [ 4., 8.], [ 5., 10.], [ 6., 12.], [ 7., 14.], [ 8., 16.], [ 9., 18.], [10., 20.]])
third_arr=np.full((2,2,2),10)
third_arr
array([[[10, 10], [10, 10]], [[10, 10], [10, 10]]])
# shape( ) gives the number of elements in each dimension
np.shape(first_arr)
(20,)
np.shape(second_arr)
(10, 2)
np.shape(third_arr)
(2, 2, 2)
# size gives the total number of elements in the array
np.size(first_arr)
20
np.size(second_arr)
20
np.size(third_arr)
8
first_arr=np.array([1, 2, 3, 5])
first_arr
array([1, 2, 3, 5])
# insert 4 at index 3
new_first_arr=np.insert(first_arr,3,4)
new_first_arr
array([1, 2, 3, 4, 5])
second_arr=np.array([1,2,3,4])
second_arr
array([1, 2, 3, 4])
new_second_arr=np.append(second_arr,5)
new_second_arr
array([1, 2, 3, 4, 5])
third_arr=np.array([1,2,3,4,5])
third_arr
array([1, 2, 3, 4, 5])
# delete the 5th element
del_arr=np.delete(third_arr,4)
del_arr
array([1, 2, 3, 4])
integers_arr=np.random.randint(0,20,20)
integers_arr
array([ 3, 9, 8, 2, 18, 1, 0, 8, 11, 13, 17, 7, 11, 0, 7, 8, 1, 5, 14, 11])
print(np.sort(integers_arr))
[ 0 0 1 1 2 3 5 7 7 8 8 8 9 11 11 11 13 14 17 18]
integers_2dim_arr=np.array([[3, 2, 5,7, 4], [5, 0, 8,3, 1]])
integers_2dim_arr
array([[3, 2, 5, 7, 4], [5, 0, 8, 3, 1]])
print(np.sort(integers_2dim_arr))
[[2 3 4 5 7] [0 1 3 5 8]]
colors=np.array(['orange','green','yellow','white','black','pink','blue','red'])
colors
array(['orange', 'green', 'yellow', 'white', 'black', 'pink', 'blue', 'red'], dtype='<U6')
print(np.sort(colors))
['black' 'blue' 'green' 'orange' 'pink' 'red' 'white' 'yellow']
A copy is a new array that is stored in a different memory location. View provides a different view of the original array.
students_ids_number=np.array([1111,1212,1313,1414,1515,1616,1717,1818])
students_ids_number
array([1111, 1212, 1313, 1414, 1515, 1616, 1717, 1818])
# students_ids_number_reg is a reference to the students_ids_number
students_ids_number_reg=students_ids_number
# id() gives the memory location of the array
print("id of students_ids_number",id(students_ids_number))
print("id of students_ids_number_reg",id(students_ids_number_reg))
id of students_ids_number 140470532651792 id of students_ids_number_reg 140470532651792
students_ids_number_reg[1]=2222
print(students_ids_number)
print(students_ids_number_reg)
[1111 2222 1313 1414 1515 1616 1717 1818] [1111 2222 1313 1414 1515 1616 1717 1818]
# make a copy. This is a deep copy.
students_ids_number_cp=students_ids_number.copy()
print(students_ids_number_cp)
[1111 2222 1313 1414 1515 1616 1717 1818]
print(students_ids_number_cp==students_ids_number)
[ True True True True True True True True]
print ("id of students_ids_number",id(students_ids_number))
print("id of students_ids_number_cp",id(students_ids_number_cp))
id of students_ids_number 140470532651792 id of students_ids_number_cp 140469998751152
students_ids_number[0]=1000
print ("original: ", students_ids_number)
print("copy: ",students_ids_number_cp)
original: [1000 2222 1313 1414 1515 1616 1717 1818] copy: [1111 2222 1313 1414 1515 1616 1717 1818]
students_ids_number_v=students_ids_number.view()
students_ids_number_v[0]=2000
print("original: ", students_ids_number)
print("view:",students_ids_number_v)
original: [2000 2222 1313 1414 1515 1616 1717 1818] view: [2000 2222 1313 1414 1515 1616 1717 1818]
# use the base attribute
# https://numpy.org/doc/stable/reference/generated/numpy.ndarray.base.html
print(students_ids_number_cp.base)
print(students_ids_number_v.base)
None [2000 2222 1313 1414 1515 1616 1717 1818]
first_arr=np.arange(1,13)
first_arr
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
second_arr=np.reshape(first_arr,(3,4))
second_arr
array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]])
third_arr=np.reshape(first_arr,(6,2))
third_arr
array([[ 1, 2], [ 3, 4], [ 5, 6], [ 7, 8], [ 9, 10], [11, 12]])
fifth_arr=np.reshape(first_arr,(3,2,2))
print(fifth_arr)
print("Dimensions of fifth_arr is ",fifth_arr.ndim)
[[[ 1 2] [ 3 4]] [[ 5 6] [ 7 8]] [[ 9 10] [11 12]]] Dimensions of fifth_arr is 3
sixth_arr=np.array([[1,2],[3,4],[5,6]])
sixth_arr
array([[1, 2], [3, 4], [5, 6]])
# flattening
seventh_arr_flat=np.reshape(sixth_arr,-1)
seventh_arr_flat
array([1, 2, 3, 4, 5, 6])
# flatten() creates a new copy
eighth_arr_flat=sixth_arr.flatten()
print("eighth_arr_flat:",eighth_arr_flat)
# ravel() creates a view
ninth_arr_rav=sixth_arr.ravel()
print("ninth_arr_rav:",ninth_arr_rav)
eighth_arr_flat: [1 2 3 4 5 6] ninth_arr_rav: [1 2 3 4 5 6]
eighth_arr_flat[0]=100
ninth_arr_rav[0]=200
print("eighth_arr_flat:",eighth_arr_flat)
print("ninth_arr_rav:",ninth_arr_rav)
print("sixth_arr:",sixth_arr)
eighth_arr_flat: [100 2 3 4 5 6] ninth_arr_rav: [200 2 3 4 5 6] sixth_arr: [[200 2] [ 3 4] [ 5 6]]
twodim_arr=np.reshape(np.arange(12),(3,4))
twodim_arr
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
twodim_arr[1,1]
5
# get the second row
twodim_arr[1]
array([4, 5, 6, 7])
threedim_arr=np.reshape(np.arange(3*4*5),(3,4,5))
threedim_arr
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, 24], [25, 26, 27, 28, 29], [30, 31, 32, 33, 34], [35, 36, 37, 38, 39]], [[40, 41, 42, 43, 44], [45, 46, 47, 48, 49], [50, 51, 52, 53, 54], [55, 56, 57, 58, 59]]])
threedim_arr[0,2,3]
13
# negative indexing accesses the array from the end
threedim_arr[2,-1,-1]
59
onedim_arr=np.arange(10)
onedim_arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# slicing
# start:stop:step size
onedim_arr[2:6]
array([2, 3, 4, 5])
onedim_arr[:5]
array([0, 1, 2, 3, 4])
onedim_arr[-3:]
array([7, 8, 9])
# step size is 2
onedim_arr[::2]
array([0, 2, 4, 6, 8])
twodim_arr
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
# slice 2D array
twodim_arr[1:,1:]
array([[ 5, 6, 7], [ 9, 10, 11]])
twodim_arr[1,:]
array([4, 5, 6, 7])
twodim_arr[:,2]
array([ 2, 6, 10])
first_arr=np.arange(1,11)
second_arr=np.arange(11,21)
print("first_arr",first_arr)
print("second_arr",second_arr)
first_arr [ 1 2 3 4 5 6 7 8 9 10] second_arr [11 12 13 14 15 16 17 18 19 20]
con_arr=np.concatenate((first_arr,second_arr))
con_arr
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
third_2darr=np.array([[1,2,3,4,5], [6,7,8,9,10]])
third_2darr
array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]])
fourth_2darr=np.array([[11,12,13,14,15], [16,17,18,19,20]])
fourth_2darr
array([[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]])
# join two arrays along rows
con2d_arr = np.concatenate((third_2darr,fourth_2darr),axis=1)
con2d_arr
array([[ 1, 2, 3, 4, 5, 11, 12, 13, 14, 15], [ 6, 7, 8, 9, 10, 16, 17, 18, 19, 20]])
st_arr = np.stack((first_arr,second_arr))
st_arr
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]])
# horizontal stacking
hst_arr=np.hstack((first_arr,second_arr))
hst_arr
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
# vertical stacking
vst_arr=np.vstack((first_arr,second_arr))
vst_arr
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]])
fifth_arr=np.arange(1,13)
fifth_arr
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
sp_arr=np.array_split(fifth_arr,4)
sp_arr
[array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9]), array([10, 11, 12])]
print(sp_arr[1])
[4 5 6]
sp_arr=np.array_split(fifth_arr,8)
sp_arr
[array([1, 2]), array([3, 4]), array([5, 6]), array([7, 8]), array([9]), array([10]), array([11]), array([12])]
np.hsplit(third_2darr,5)
[array([[1], [6]]), array([[2], [7]]), array([[3], [8]]), array([[4], [9]]), array([[ 5], [10]])]
vs_arr=np.vsplit(third_2darr,2)
vs_arr
[array([[1, 2, 3, 4, 5]]), array([[ 6, 7, 8, 9, 10]])]
arr1 = np.array([2,4,6,8,10])
arr2 = np.array([1,3,5,7,9])
arr3 = np.concatenate((arr1, arr2), axis=0)
arr3
array([ 2, 4, 6, 8, 10, 1, 3, 5, 7, 9])
a=np.arange(1,11)
b=np.arange(21,31)
print("a",a)
print("b",b)
a [ 1 2 3 4 5 6 7 8 9 10] b [21 22 23 24 25 26 27 28 29 30]
a+b
array([22, 24, 26, 28, 30, 32, 34, 36, 38, 40])
b-a
array([20, 20, 20, 20, 20, 20, 20, 20, 20, 20])
a*b
array([ 21, 44, 69, 96, 125, 156, 189, 224, 261, 300])
b/a
array([21. , 11. , 7.66666667, 6. , 5. , 4.33333333, 3.85714286, 3.5 , 3.22222222, 3. ])
a**b
array([ 1, 4194304, 94143178827, 281474976710656, 298023223876953125, 4561031516192243712, 5059972980857283351, 0, 5756027422282277481, 5076944270305263616])
a*2
array([ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20])
np.add(a,b)
array([22, 24, 26, 28, 30, 32, 34, 36, 38, 40])
np.subtract(b,a)
array([20, 20, 20, 20, 20, 20, 20, 20, 20, 20])
np.multiply(a,b)
array([ 21, 44, 69, 96, 125, 156, 189, 224, 261, 300])
np.divide(b,a)
array([21. , 11. , 7.66666667, 6. , 5. , 4.33333333, 3.85714286, 3.5 , 3.22222222, 3. ])
np.mod(b,a)
array([0, 0, 2, 0, 0, 2, 6, 4, 2, 0])
np.power(a,b)
array([ 1, 4194304, 94143178827, 281474976710656, 298023223876953125, 4561031516192243712, 5059972980857283351, 0, 5756027422282277481, 5076944270305263616])
np.sqrt(a)
array([1. , 1.41421356, 1.73205081, 2. , 2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. , 3.16227766])
a=np.arange(1,10).reshape(3,3)
a
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b=np.arange(1,4)
b
array([1, 2, 3])
a+b
array([[ 2, 4, 6], [ 5, 7, 9], [ 8, 10, 12]])
c=np.arange(1,3)
c
array([1, 2])
d=np.arange(24).reshape(2,3,4)
d
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]]])
e=np.arange(4)
e
array([0, 1, 2, 3])
d-e
array([[[ 0, 0, 0, 0], [ 4, 4, 4, 4], [ 8, 8, 8, 8]], [[12, 12, 12, 12], [16, 16, 16, 16], [20, 20, 20, 20]]])
first_arr=np.arange(10,110,10)
first_arr
array([ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
second_arr=np.arange(10,100,10).reshape(3,3)
second_arr
array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
third_arr=np.arange(10,110,10).reshape(2,5)
third_arr
array([[ 10, 20, 30, 40, 50], [ 60, 70, 80, 90, 100]])
first_arr.sum()
550
second_arr.sum()
450
third_arr.sum()
550
# sum of each column
second_arr.sum(axis=0)
array([120, 150, 180])
# sum of each row
second_arr.sum(axis=1)
array([ 60, 150, 240])
# product of all element
first_arr.prod()
36288000000000000
second_arr.prod()
362880000000000
third_arr.prod()
36288000000000000
third_arr.prod(axis=0)
array([ 600, 1400, 2400, 3600, 5000])
np.average(first_arr)
55.0
np.average(second_arr)
50.0
np.average(third_arr)
55.0
np.min(first_arr)
10
np.max(first_arr)
100
np.mean(first_arr)
55.0
np.std(first_arr)
28.722813232690143
first_arr=np.array([1,2,3,4,5,6,1,2,7,2,1,10,7,8])
np.unique(first_arr)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 10])
second_arr=np.array([[1, 1, 2,1] ,[ 3, 1, 2,1] , [1, 1, 2, 1], [ 7, 1, 1, 1]])
second_arr
array([[1, 1, 2, 1], [3, 1, 2, 1], [1, 1, 2, 1], [7, 1, 1, 1]])
np.unique(second_arr)
array([1, 2, 3, 7])
# get unique rows
np.unique(second_arr,axis=0)
array([[1, 1, 2, 1], [3, 1, 2, 1], [7, 1, 1, 1]])
# get unique columns
np.unique(second_arr,axis=1)
array([[1, 1, 2], [1, 3, 2], [1, 1, 2], [1, 7, 1]])
np.unique(first_arr,return_index= True)
(array([ 1, 2, 3, 4, 5, 6, 7, 8, 10]), array([ 0, 1, 2, 3, 4, 5, 8, 13, 11]))
np.unique(second_arr,return_counts=True)
(array([1, 2, 3, 7]), array([11, 3, 1, 1]))
Reshape remakes the array by the inputs, while transpose swaps rows and columns of an array.
first_2dimarr = np.arange(12).reshape((3,4))
first_2dimarr
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
np.transpose(first_2dimarr)
array([[ 0, 4, 8], [ 1, 5, 9], [ 2, 6, 10], [ 3, 7, 11]])
second_2dimarr=np.arange(6).reshape(3,2)
second_2dimarr
array([[0, 1], [2, 3], [4, 5]])
np.transpose(second_2dimarr,(1,0))
array([[0, 2, 4], [1, 3, 5]])
first_3dimarr=np.arange(24).reshape(2,3,4)
first_3dimarr
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.moveaxis(first_3dimarr,0,-1)
array([[[ 0, 12], [ 1, 13], [ 2, 14], [ 3, 15]], [[ 4, 16], [ 5, 17], [ 6, 18], [ 7, 19]], [[ 8, 20], [ 9, 21], [10, 22], [11, 23]]])
np.swapaxes(first_3dimarr,0,2)
array([[[ 0, 12], [ 4, 16], [ 8, 20]], [[ 1, 13], [ 5, 17], [ 9, 21]], [[ 2, 14], [ 6, 18], [10, 22]], [[ 3, 15], [ 7, 19], [11, 23]]])
arr_1dim=[10,1,9,2,8,3,7,4,6,5]
arr_1dim
[10, 1, 9, 2, 8, 3, 7, 4, 6, 5]
arr_1dim[::-1]
[5, 6, 4, 7, 3, 8, 2, 9, 1, 10]
np.flip(arr_1dim)
array([ 5, 6, 4, 7, 3, 8, 2, 9, 1, 10])
arr_2dim=np.arange(9).reshape(3,3)
arr_2dim
array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
np.flip(arr_2dim)
array([[8, 7, 6], [5, 4, 3], [2, 1, 0]])
# only flip each row
np.flip(arr_2dim,1)
array([[2, 1, 0], [5, 4, 3], [8, 7, 6]])
arr_3dim=np.arange(24).reshape(2,3,4)
arr_3dim
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]]])
# flip the 2nd dimension of the array
np.flip(arr_3dim,1)
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]]])
np.flip(arr_3dim,2)
array([[[ 3, 2, 1, 0], [ 7, 6, 5, 4], [11, 10, 9, 8]], [[15, 14, 13, 12], [19, 18, 17, 16], [23, 22, 21, 20]]])