Two: Shape and reshaping of Numpy arrays&& Three: Indexing and slicing of Numpy arrays (one-dimensional, two-dimensional, three-dimensional)&& Four: Stacking and cascading of Numpy arrays&& Five: Numpy array broadcast&& Six: Calculation with Numpy arrays

Two: Shape and reshaping of Numpy arrays

Below are some inline code snippets.

// A code block
var foo = 'bar';
// An highlighted block
2.1 View the dimensions of a NumPy array array.ndim
input:a = np.array([[5,10,15],[20,25,20]])
print('Array :','\\
',a)
print('Dimensions :',a.ndim)
output:Array :
[[ 5 10 15]
[20 25 20]]
Dimensions: 2
2.2 Check the shape of NumPy array ndarray.shape,ndayyary.shape[0],ndayyary.shape[0]
input:a = np.array([[1,2,3],[4,5,6]])
print('Array :','\\
',a)
print('Shape :',a.shape)
print('Rows = ',a.shape[0])
print('Columns = ',a.shape[1])
output:Array :
[[1 2 3]
[4 5 6]]
Shape: (2, 3)
Rows = 2
Columns = 3
2.3 Reshape the dimensions of Numpy arrays (1 dimension - multi-dimensional, multi-dimensional - multi-dimensional, multi-dimensional - 1 dimension) np.reshape()
#1 Dimension - Multidimensional (manually set shapes)
input:a = np.array([3,6,9,12])
np.reshape(a,(2,2))
output:[[3 6]
        [9 12]]
#1 dimension - multi-dimensional (automatically calculate shape), numpy will automatically calculate the shape when it sees -1, only one of the rows and columns can be -1, the other needs to be specified
input:a = np.array([3,6,9,12,15,18])
print('Three rows :',np.reshape(a,(3,-1)))
print('Three columns :',np.reshape(a,(-1,3)))
ouput:Three rows:
      [[3 6]
       [9 12]
       [18 24]]
      Three columns:
      [[3 6 9]
       [12 18 24]]
#Multidimensional-1 dimension (expand Numpy array) Does not change the original array: flatten() Changes the original array: ravel()
input: a=np.zeros((3,3),dtype=np.int32)
b=a.flatten()
c=a.ravel()
print("a:",a)
print("b:",b)
print("c:",c)
b[0]=1
print("a1:",a)
c[0]=1
print("a2:",a)
output: a: [[0 0 0]
[0 0 0]
[0 0 0]]
b: [0 0 0 0 0 0 0 0 0]
       c: [0 0 0 0 0 0 0 0 0]
       a1: [[0 0 0]
 [0 0 0]
            [0 0 0]]
a2: [[1 0 0]
 [0 0 0]
 [0 0 0]]
#多dimensional-multidimensional
input:a1=np.ones((4,4),dtype=np.int32)
a2=a1.reshape((2,8)) #a2=np.reshape(a1,(2,8))
print("a2:",a2)
output:a1: [[1 1 1 1]
[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
a2: [[1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1]]
2.4 Transposition of Numpy array np.trasnspose(ndarrary) /ndarrary.transpose()
input:a = a = np.array([[1,2,3],
[4,5,6]])
b = a.transpose()
print('Original shape and array:',a.shape,'\\
',a)
print(' Original shape and arrary After transpose:',b.shape,'\\
',b)
output:Original shape and array: (2, 3)
[[1 2 3]
[4 5 6]]
 Original shape and array After transpose: (3, 2)
 [[1 4]
 [2 5]
 [3 6]]
var foo = 'bar';

Three: Indexing and slicing of Numpy arrays (one-dimensional, two-dimensional, three-dimensional)

// A code block
var foo = 'bar';
// An highlighted block
**3.1 Slicing of Numpy arrays
3.1.1 Slice of one-dimensional array** ndarrary[start:end:step-size] Index range: [start,end)
input:a = np.array([1,2,3,4,5,6])
print(a[1:5:2])
print(a[:6:2]) #When start is not specified, start defaults to 0
print(a[1::2]) #When end is not specified, end defaults to the array size
 print(a[1:6:]) #The default is 1 when the step size is not specified
output:[2 4]
[1 3 5]
[2 4 6]
[2 3 4 5 6]
\t   
**3.2 Two-dimensional array slicing
<1>Retrieve elements from a two-dimensional array**
input:a = np.array([[1,2,3],
[4,5,6]])
print(a[0,0]) #Equivalent to print(a[0][0])
print(a[0][0])
print(a[1,2])
print(a[1,0])
output:1
\t   1
6
       4
       
**<2>Two-dimensional array slicing**
input:a = np.array([[1,2,3],[4,5,6]])
#Print the first row of values
print('The value of the first line:',a[0:1,:])
# Has the step size of the column
print('The values of the first row, second column and first row and third column:',a[0:1,::2])
print('Second column values :',a[:,1::2])
print('Arbitrary values :',a[0:1,1:3])


output: value of the first line: [[1 2 3]]
       The values of the first row, second column and the first row and third column: [[1 3]]
Second column values: [[2] [5]]
Arbitrary values: [[2 3]]
**3.3 Three-dimensional array slicing**
input:a = np.array([[[1,2],[3,4],[5,6]],# first axis
[[7,8],[9,10],[11,12]],# second axis
[[13,14],[15,16],[17,18]]])# The third axis
print(a)
output:[ [[1 2] [3 4] [5 6]]
         [[7 8] [9 10] [11 12]]
  [[13 14] [15 16] [17 18]]
  ]
input:print('First array, first row, first column value :','\\
',a[0,0,0])
print('First array last column:','\\
',a[0,:,1])
print('First two rows for second and third arrays :','\\
',a[1:,0:2,0:2])

output:First array, first row, first column value: 1
First array last column: [2 4 6]
First two rows for second and third arrays : [[[7 8]
                                           [9 10]]
 [[13 14]
  [15 16]]]
**3.4 Negative Slicing of Numpy Arrays**
input:a = np.array([[1,2,3,4,5],
[6,7,8,9,10]])
print(a[:,-1]) #The last number of all lines

output:[5 10]

input:print(a[-1::-1,-1::-2]) #The first row from the bottom - the first row, step size: -1, the first column from the bottom - the first column, the step size: -2

output:[[10 8 6] #Second row-First row & amp; & amp;(fifth column, third column, first column)
 [5 3 1]]
#Use negative slicing to reverse the array ndarrary.[::-1,::-1]
input:print(a[::-1,::-1])
output:[[10 9 8 7 6]
[5 4 3 2 1]]
The #flip(ndarrary,axis=0/1) method reverses the array. When axis=0, the columns are reversed, and when axis=1, the rows are reversed.
input:print("a array row reverse:",np.flip(a,axis=1))
print("a array column reverse:",np.flip(a,axis=0))
output:a array row inversion: [[5 4 3 2 1]
[10 9 8 7 6]]
       a array column reverse: [[6 7 8 9 10]
[1 2 3 4 5]]
**Four: Stacking and cascading of Numpy arrays (splicing of arrays)**
**4.1 Stacking Numpy arrays** Vertical stacking: vstack(a,b), that is, stacking along rows. Horizontal stacking: hstack(a,b), that is, stacking along columns and stacking by depth: dstack((a,b)) Note: When using the vstack and hstack methods, the two numpy arrays a and b must have exactly the same axis.
input:a = np.arange(1,6)
b = np.arange(6,11)
print('a:','\\
',a)
print('b:','\\
',b)
print('Vertically stacked, that is, stacked along the line:',np.vstack((a,b)))
print('Horizontal stacking, that is, stacking along columns:',np.hstack((a,b)))
a1=[[1,2],[3,4]]
b1=[[5,6],[7,8,]]
print('Stack by depth:',np.dstack(a1,b1))

output:a:[1 2 3 4 5]
b:[6 7 8 9 10]
Stack vertically, that is, stack along rows: [[1 2 3 4 5]
[6 7 8 9 10]
}
Stack horizontally, that is, stack along columns: [1 2 3 4 5 6 7 8 9 10]
Stack by depth:[ [[1 5] [2 6]]
[[3 7] [4 8]] ]
4.2 Concatenated Numpy array concatenate((a,b),axis=0/1)
append(a,value,axis=0/1): directly add new elements to the end of the a array
Note: Using the np.append() method does not change the value of the original array, but adds elements to the copy.
\t\t\t\t 
#np.concatenate((a,b),axis=0/1) When axis=0, cascade by column. When axis=0, cascade by row.
input:a = np.arange(1,6).reshape(1,5)
b = np.arange(6,11).reshape(1,5)
print('a:','\\
',a)
print('b:','\\
',b)
print('Concatenate by columns:','\\
',np.concatenate((a,b),axis=0))
print('Concatenation by row:','\\
',np.concatenate((a,b),axis=1))
output:a:[[1 2 3 4 5]]
b:[[6 7 8 9 10]]
Cascade by column:[ [1 2 3 4 5]
[6 7 8 9 10]
]
Concatenate by row: [ [1 2 3 4 5 6 7 8 9 10] ]
\t   \t\t\t 
#np.append()
input:c = np.array([[1 2]
         [3 4]])
c1=np.append(c,[[5,6]], axis=0)
      print('c1:',c1,'\\
')
print('c:',c)
output:c1: [[1 2]
 [3 4]
 [5 6]]

c: [[1 2]
 [3 4]]
**5: Numpy array broadcast**
Operations can be performed between numpy arrays of different shapes or between numpy arrays and numbers. Broadcasting basically stretches the smaller numpy array such that the smaller numpy array retains the same shape as the larger numpy array
Note: Broadcasting can only be used in two situations 1. The two arrays have the same shape 2. One of them is 1-dimensional
input:a = np.arange(1,11,2) #[1 3 5 7 9]
b = np.array([[2],[2]])
print('a + b=:','\\
',a + b)
print('a*2=:',a*2)
output:a + b:[[3 5 7 9 11]
[3 5 7 9 11]]
a*2=[2 6 10 14 18]
Six: Calculation using Numpy arrays
6.1 Basic operations on Numpy arrays: addition, subtraction, multiplication, division, remainder
input:a=np.arange(6,11)
print('a=',a)
print('a + 5=:',a + 5)
print('a-5=:',a-5)
print('a*5=:',a*5)
print('a/5=:',a/5)
print('a*a=:',a**2)
print('a%5=:',a%5)
output:a=[6 7 8 9 10]
a-5=[1 2 3 4 5]
a + 5=[ 11 12 13 14 15]
a*5=[30 35 40 45 50]
a/5=[1.2 1.4 1.6 1.8 2.]
a*a=[36 79 64 81 100]
a%5=[1 2 3 4 0]
6.2 Mean, median, standard deviation np.mean() np.std() np.median()
input:a = np.arange(1,11,2)
print('Mean:',np.mean(a))
print('Standard deviation:',np.std(a))
print('Median:',np.median(a))

output: average: 5.0
Standard deviation: 2.8284271247461903
       Median: 5.0

\t  
   \t\t     \t   \t\t\t 
\t   

    \t\t\t 
 \t\t
  \t                 \t\t\t\t\t\t\t\t 
  \t   
  \t 
\t 










var foo = 'bar';