Numpy

import numpy as np

1. Create ndarray

#Specify every value
x = np.array([1, 2, 3, 4, 5])
y = np.array([[1,2,3],[4,5,6],[7,8,9], [10,11,12]]) # Create a rank 2 ndarray
x = np.array([1.5, 2.2, 3.7, 4.0, 5.9], dtype = np.int64) # Specify the dtype

np.save('my_array', x) #save into a file
y = np.load('my_array.npy') #load the file
#Full
X = np.zeros((3,4)) # Create 3×4 ndarray full of zero 
X = np.ones((3,4)) # Create 3×4 ndarray full of ones
X = np.full((3,4), 5)  # Create 3×4 ndarray full of specified values
#special
X = np.eye(5) ## 5 x 5 Identity matrix
X = np.diag([10,20,30,50]) # 4 x 4 diagonal matrix 
#Range
x = np.arange(10) # x = [0 1 2 3 4 5 6 7 8 9]
x = np.arange(1,14,3) # x = [ 1 4 7 10 13]
#random
X = np.random.random((3,3)) #random floats in the half-open interval [0.0, 1.0)
X = np.random.randint(4,15,size=(3,2)) 
X = np.random.normal(0, 0.1, size=(1000,1000)) # mean of zero and a standard deviation of 0.1
# Reshape
x = np.arange(20)
x = np.reshape(x, (4,5))
x = np.arange(20).reshape(4, 5) #does the same thing as above
#Copy
Z = np.copy(X[1:4,2:5])
W = X[1:4,2:5].copy()

2. Acessing and Changing

#Slicing
Z = X[1:4,2:5] 
#Change
x[3] = 20 # Mutability
#Delete
x = np.delete(x, [0,4]) #delete the first and fifth element of x
Y = np.array([[1,2,3],[4,5,6],[7,8,9]]) 
w = np.delete(Y, 0, axis=0) # delete the first row of Y
v = np.delete(Y, [0,2], axis=1) # delete the first and last column of Y
#Append
x = np.append(x, 6) # append the integer 6 to x
x = np.append(x, [7,8]) # append the integer 7 and 8 to x
v = np.append(Y, [[10,11,12]], axis=0) # append a new row containing 7,8,9 to y
q = np.append(Y,[[13],[14],[15]], axis=1) # append a new column containing 9 and 10 to y
#Insert
x = np.array([1, 2, 5, 6, 7])
Y = np.array([[1,2,3],[7,8,9]]) 
x = np.insert(x,2,[3,4]) # insert the integer 3 and 4 between 2 and 5
w = np.insert(Y,1,[4,5,6],axis=0) # insert a row between the first and last row 
v = np.insert(Y,1,5, axis=1) # insert a column full of 5 between the first and second column
#Stacking(join)
x = np.array([1,2])
Y = np.array([[3,4],[5,6]])
z = np.vstack((x,Y)) # [[1,2], [3,4], [5,6]] # stack ndarrays on top of each other(vertical stacking)
w = np.hstack((Y,x.reshape(2,1))) # [[3,4,1], [5,6,2]] (horizontal stacking)

#Boolean Indexing
Y = X[X > 10] #Conditional select
X[(X > 10) & (X < 17)] = -1 #Conditonal reassign
#Set Operations
np.intersect1d(x,y) #The elements that are both in x and y
np.setdiff1d(x,y) #The elements that are in x that are not in y
np.union1d(x,y) #All the elements of x and y
#Sort
s = np.sort(x) or x.sort()
s = np.sort(np.unique(x)) # sort the unique elements in x
s = np.sort(X, axis = 0) # sort the columns of X
s = np.sort(X, axis = 1) # sort the rows of X

np.argmax(),np.argmin()
np.where() # return conditional index

3. Math Function

#must have the same shape or be broadcastable
np.add(X,Y),np.subtract(X,Y),np.multiply(X,Y)
np.divide(X,Y),np.mod(X,Y)
# apply mathematical functions to all elements of an ndarray at once
np.round(x,decimals) #default 0
np.floor() ,np.ceil() #round down and round up
np.exp(x),np.sqrt(x),np.power(x,2)
#Statistical Functions
X.mean(),X.mean(axis=0),X.mean(axis=1),X.sum()
X.std() #Standard Deviation
np.median(X),X.max(),X.min()
#Broadcast
4*X,4+X,4-X,4/X #This allows us to operate 4 to each element of X

4. Linear Algebra

np.dot(a,b) 
np.matmul(a,b)
np.linalg.det(a)
np.linalg.inv(x) 
posted @ 2022-06-06 12:49  失控D大白兔  阅读(35)  评论(0编辑  收藏  举报