Basic Array Operations in Python Numpy

Numpy Basic Operations

import numpy as np
import pandas as pd

Properties

array=np.array([[1,2,3],[2,3,4]])
print(array)
print('number of dim:',array.ndim)#dimension 
print('shape',array.shape)#ranks 
print('size',array.size)#number of elements 
"""
[[1 2 3]
[2 3 4]]
number of dim: 2
shape (2, 3)
size 6
"""
#data type 
array0=np.array([[1,2,3],[2,3,4]],dtype=np.float16)#np.int64
print(array0.dtype)
# float16

Understanding arrays

zero=np.zeros((3,4))#double parentheses are not written by default because of the type 
one=np.ones((3,4),dtype=np.int64)#all are 1 
print(zero)
print(one)
a=np.arange(10,20,2)#vector (start, end, step size) 
print(a)
a[-1]=21
a=np.delete(a,0)#delete the first element 
print(a)#stay a based on this, modify the last value to 21 
b=np.arange(12,dtype=float)#0-11 floating-point type 
print(b)
c=np.arange(12).reshape((3,4))#set rows and rows 
print(c)
d=np.linspace(1,10,6).reshape((2,3))#line segments (first, last, number) 
print(d)
e=np.random.randint(1,10,(2,5))#2 * 5 random matrix between 1-10 
print(e,'n')
print("#array dimension")
#output one-dimensional arrays as rows, two-dimensional arrays as matrices, and three-dimensional arrays as matrix lists 
c1=np.array([1,2,3])#one dimensional array, one bracket 
c2=np.array([[1,2],[3,4]])#two dimensional array, two parentheses 
c3= np.array([[[1,2],[3,4],[5,6]],[[1,2],[3,4],[5,6]]])#3d array 
print(c3, c3.shape,c3.ndim)
"""
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
[10 12 14 16 18]
[12 14 16 21]
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11.]
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]
[[ 1.   2.8  4.6]
[ 6.4  8.2 10. ]]
[[6 1 5 2 9]
[6 6 9 8 4]] 

#array dimension 
[[[1 2]
[3 4]
[5 6]]
[[1 2]
[3 4]
[5 6]]] (2, 3, 2) 3
"""

Basic operation 1

e=np.arange(4)
f=e**2#square binary star 
g=10*np.sin(np.pi/2)#np.pi radian 
h=10*np.sin(f)
print(f,g,h)
print(e<2)#compare boolean 
# np.exp
#matrix calculation 
a0=np.array([[1,2],[2,3]])
b0=np.arange(4).reshape((2,2))
c0=a0*b0#dot multiplication 
c_dot=np.dot(a0,b0)#cross product 
c_dot1=a0.dot(b0)#cross product 
print(c0)
print(c_dot)
print(c_dot1,'n')
rand=np.random.random((2,4))#the first one random it is a module. second random it is a randomly generated function from 0 to 1 
print(rand)
print(np.sum(rand))
print(np.min(rand,axis=1))#minimum value for each row 
print(np.min(rand,axis=0))#maximum value of each column 
print("        ")
#minimum position index [0] start 
print(np.argmin(rand))
print(np.average(rand,axis=1))#take the average mean and average
print(rand.mean())
#median median std (1) standard deviation 
"""
[0 1 4 9] 10.0 [ 0.          8.41470985 -7.56802495  4.12118485]
[ True  True False False]
[[0 2]
[4 9]]
[[ 4  7]
[ 6 11]]
[[ 4  7]
[ 6 11]] 
[[0.64530567 0.6523024  0.37349276 0.0186477 ]
[0.66123502 0.03198241 0.4926486  0.81234177]]
3.687956337947351
[0.0186477  0.03198241]
[0.64530567 0.03198241 0.37349276 0.0186477 ]
  
3
[0.42243714 0.49955195]
0.46099454224341885
"""

Basic Operations 2

A=np.arange(14,2,-1).reshape((3,4))
print(np.mean(A,axis=0))#calculate the average value of the column 
print(np.sort(A))#sort sort 
print(np.transpose(A))#transposition 
print(A.T)#transposition 
print("        ")
#clip (matrix, less than this value becomes this value, greater than this value becomes this value) the middle remains unchanged 
print(np.clip(A,5,9))
"""
[10.  9.  8.  7.]
[[11 12 13 14]
[ 7  8  9 10]
[ 3  4  5  6]]
[[14 10  6]
[13  9  5]
[12  8  4]
[11  7  3]]
[[14 10  6]
[13  9  5]
[12  8  4]
[11  7  3]]
  
[[9 9 9 9]
[9 9 8 7]
[6 5 5 5]]
"""

Index usage

B=np.arange(3,15).reshape((3,4))
print(B)
print(B[2])#third line 
print(B[2:,])
print(B[2,1:4])#column starts from 1 
print(B[0][0])#first number 
print(B[0,0])
print("        ")
print("#circular travel")   
for row in B:
print(row)
print("        ")
print("#loop delisting")
for col in B.T:
print(col)
print("#loop out each item")
print(B.flatten())#expand a vector 
for item in B.flat:#flat it's an iterator 
print(item)
print("delete",'n')
B=np.delete(B,1,axis=0)#delete second line element 
print(B)
"""
[[ 3  4  5  6]
[ 7  8  9 10]
[11 12 13 14]]
[11 12 13 14]
[[11 12 13 14]]
[12 13 14]
3
3
  
#circular travel 
[3 4 5 6]
[ 7  8  9 10]
[11 12 13 14]
  
#loop delisting 
[ 3  7 11]
[ 4  8 12]
[ 5  9 13]
[ 6 10 14]
  
#loop out each item 
[ 3  4  5  6  7  8  9 10 11 12 13 14]
3
4
5
6
7
8
9
10
11
12
13
14
 delete 
[[ 3  4  5  6]
[11 12 13 14]]
"""

Array merging

C=np.array([[1,1,1]])
D=np.array([[2,2,2]]) 
C1=np.array([1,1,1])#a [] it is a one-dimensional array, [[]] it is a two-dimensional array row and column, please pay attention to the difference when referencing 
print(np.vstack((C,D)))#vertical stack pay attention to double brackets 
E=np.hstack((C,D))#horizontal stack
print(E)
print(C.shape,E.shape,C1.shape)
print(C.T)#become 3 * 1 
print(C1)
print(C1.T)#C 1 and C1.T no change 
print("C.T.shape,C1.T.shape")
print("        ")
print("#C1[np.newaxis,:] and C1[:,np.newaxis] the difference between C 1 is (, N)")
print(C1[np.newaxis,:])#added a dimension to become a two-dimensional array, with row vectors (1, N)[[1 1 1]]
print(C1[:,np.newaxis])#add dimension and column( N 1) column vectors 
print(C1.reshape(3,1))#again reshape dimensions can also be added 
print("        ")
print("# concatenate merge")
H=np.vstack((C,C,D))
H0=np.concatenate((C,C,D),axis=0)#vertical merge as above 
print(H)
print(H0)
"""
[[1 1 1]
[2 2 2]]
[[1 1 1 2 2 2]]
(1, 3) (1, 6) (3,)
[[1]
[1]
[1]]
[1 1 1]
[1 1 1]
C.T.shape,C1.T.shape
  
#C1[np.newaxis,:] and C1[:,np.newaxis] the difference between C 1 is (, N)
[[1 1 1]]
[[1]
[1]
[1]]
[[1]
[1]
[1]]
  
# concatenate merge 
[[1 1 1]
[1 1 1]
[2 2 2]]
[[1 1 1]
[1 1 1]
[2 2 2]]
"""

Array segmentation

#split
S=np.arange(12).reshape((3,4))
print(S)
print(np.split(S,3,axis=0))#perform top-down execution on each column, divided into 3 points 
print(np.vsplit(S,3))#divide vertically into 3 blocks 
print("               ")
print(np.split(S,2,axis=1))##perform top-down execution on each row, divided into 2 points 
print(np.hsplit(S,2))#divide horizontally into 3 pieces 
print("        ")
print("#above is equal segmentation, below is unequal segmentation")
print(np.array_split(S,3,axis=1))##perform top-down execution on each row, divided into 3 points 
​"""
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]
         
[array([[0, 1],
 [4, 5],
 [8, 9]]), array([[ 2,  3],
 [ 6,  7],
 [10, 11]])]
[array([[0, 1],
 [4, 5],
 [8, 9]]), array([[ 2,  3],
 [ 6,  7],
 [10, 11]])]
  
#above is equal segmentation, below is unequal segmentation 
[array([[0, 1],
 [4, 5],
 [8, 9]]), array([[ 2],
 [ 6],
 [10]]), array([[ 3],
 [ 7],
 [11]])]
"""

Copy

l=np.array([0,1,2,3])
m=l
n=l
m=n
l[0]=5
print(l,m,n)#still the same 
print(m is n)
print(" ")
print("unrelated")
p=l.copy()# deep copy
print(p is l)
"""
[5 1 2 3] [5 1 2 3] [5 1 2 3]
True

 unrelated 
False
"""

Reference learning Reference learning.