Create array
import numpy as np arr1=np.array([0.3,0.5,4.2])#Create a one-dimensional array arr2=np.array([[3,4,5],[4,2,1]])#Create a two-dimensional array print(arr1) print(arr2) print(type(arr1))
[0.3 0.5 4.2] [[3 4 5] [4 2 1]] <class 'numpy.ndarray'>
#View the basic properties of the array print(arr1.shape)#returns a tuple print(arr1.ndim) print(arr1.dtype)#data type print(arr2.shape) print(arr2.ndim)#Return the dimension of the array print(arr2.dtype)
(3,) 1 float64 (twenty three) 2 int32
#First introduction to the characteristics of arrays list1=[0.3,0.5,4.2] arr1=np.array([0.3,0.5,4.2]) print(list1) print(arr1) #list1**2 print([i**2 for i in list1]) arr1**2# square term print(arr1**2)
[0.3, 0.5, 4.2] [0.3 0.5 4.2] [0.09, 0.25, 17.64] [0.09 0.25 17.64]
arr3=np.arange(0,10)# arr4=np.arange(10) #The default starting point is equal to 0 arr5=np.arange(0,1,0.1)#start stop step print(arr3) print(arr4) print(arr5)
[0 1 2 3 4 5 6 7 8 9] [0 1 2 3 4 5 6 7 8 9] [0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
#All 0 array
arr7=np.zeros([3,4,3])#Three rows and four columns three-dimensional array
print(arr7) [[[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.]]]
#Data type of array arr8=np.array([3,4,5],dtype=np.float32)#Declare its data type when creating an array print(arr8) print(arr8.dtype) arr8[0]=1.2 print(arr8) print(np.int32(arr8))#Convert the array type of the array #Generate random numbers print(np.random.random(10))#Random number under unconstrained conditions print(np.random.rand(3,4))#Generate uniformly distributed random numbers of the specified shape#That is, three rows and four columns print(np.random.randn(2,2,2))#Generate normal distributed random numbers of specified shape#Three-dimensional structure
[3. 4. 5.] float32 [1.2 4. 5. ] [1 4 5][0.17106092 0.29044013 0.79911209 0.45078865 0.46598821 0.43567392 0.58008144 0.95826806 0.9241146 0.19584847] [[0.09034558 0.69409671 0.2933771 0.95821404] [0.60125074 0.54150608 0.99578956 0.64812296] [0.60906182 0.10291584 0.63418118 0.54207162]] [[[-0.45568266 1.11536442] [-1.24810256 -1.33604114]] [[-1.51907174 0.24281556] [ 0.70003938 0.10203376]]]
random module commonly used random number generation function
seed determines the seed of the random number generator permutation returns a random permutation or range of a sequence shuffle randomly sorts a sequence
arr9=np.array([1,5,6,8,4])
print(np.random.shuffle(arr9))
array index
arr1=np.array([0.3,0.78,0.24,5,3.2])
print(arr1)
print(arr1[0])
print(arr1[-5])
#Multiple element indexes of one-dimensional arrays (slicing)
print(arr1[1:3])#Left closed and right opened
print(arr1[-4:-2])res1=arr1[3]
res2=arr1[3:4]#Retain the original structure
print(res1,res1.shape)#0-dimensional array is a scalar
print(res2,res2.shape)#One-dimensional array, which is a vector [0.3 0.78 0.24 5. 3.2] 0.3 0.3 [0.78 0.24] [0.78 0.24] 5.0 () [5.] (1,)
Logical index
arr2=np.array([2.3,1.8,4.5])
print(arr2)
print(arr2[[False,False,True]])
arr2>2
index=arr2>2
print(index)
print(arr2[index])[2.3 1.8 4.5] [4.5] [True False True] [2.3 4.5]
Index of multi-dimensional array
arr3=np.arange(1,13).reshape([3,4])
print(arr3)
print(arr3[2,3])
print(arr3[2,0:])
print(arr3[:,0])
print(arr3[0:2,0])
print(arr3[2:,:])
print(arr3[arr3[:,0]>4,:])[[ 1 2 3 4] [5 6 7 8] [9 10 11 12]] 12 [9 10 11 12] [1 5 9] [1 5] [[ 9 10 11 12]] [[5 6 7 8] [ 9 10 11 12]]
Modify elements in array
arr3=np.arange(1,13).reshape([3,4])
print(arr3)
arr3[0,0]=15
print(arr3)[[ 1 2 3 4] [5 6 7 8] [9 10 11 12]] [[15 2 3 4] [5 6 7 8] [ 9 10 11 12]]
Solve for the distance matrix
n=10
x=np.linspace(1,100,n)#arithmetic array#sample abscissa
y=np.linspace(1,100,n)#The ordinate of the sample
dist=np.zeros([n,n])
for i in range(n):
for j in range(n):
dist[i,j]=np.sqrt((x[i]-x[j])**2 + (y[i]-y[j])**2) #Calculate Euclidean distance
print(x)
print(y)
print(dist)[ 1. 12. 23. 34. 45. 56. 67. 78. 89. 100.] [1. 12. 23. 34. 45. 56. 67. 78. 89. 100.] [[ 0. 15.55634919 31.11269837 46.66904756 62.22539674 77.78174593 93.33809512 108.8944443 124.45079349 140.00714267] [ 15.55634919 0. 15.55634919 31.11269837 46.66904756 62.22539674 77.78174593 93.33809512 108.8944443 124.45079349] [ 31.11269837 15.55634919 0. 15.55634919 31.11269837 46.66904756 62.22539674 77.78174593 93.33809512 108.8944443 ] [46.66904756 31.11269837 15.55634919 0. 15.55634919 31.11269837 46.66904756 62.22539674 77.78174593 93.33809512] [ 62.22539674 46.66904756 31.11269837 15.55634919 0. 15.55634919 31.11269837 46.66904756 62.22539674 77.78174593] [77.78174593 62.22539674 46.66904756 31.11269837 15.55634919 0. 15.55634919 31.11269837 46.66904756 62.22539674] [93.33809512 77.78174593 62.22539674 46.66904756 31.11269837 15.55634919 0. 15.55634919 31.11269837 46.66904756] [108.8944443 93.33809512 77.78174593 62.22539674 46.66904756 31.11269837 15.55634919 0. 15.55634919 31.11269837] [124.45079349 108.8944443 93.33809512 77.78174593 62.22539674 46.66904756 31.11269837 15.55634919 0. 15.55634919] [140.00714267 124.45079349 108.8944443 93.33809512 77.78174593 62.22539674 46.66904756 31.11269837 15.55634919 0. ]]