Numpy常用函数汇总

import numpy as np 


#basic attribute
array = np.array([[1, 2, 3], [2, 3, 4]])
print(array)		#print the np.array
print(array.ndim)	#print the dimension of the np.array
print(array.shape)	#print the shape of the np.array
print(array.size)	#print the size of the np.array
print(array.dtype)	#print the data type of the np.array


#create array of numpy
a = np.array([2, 23, 4])				#create an array via list
a = np.array([2, 23, 4], dtype=np.int)	#specify the data type
a = np.array([[2, 23, 4],[3, 33, 5]])	#create an array in the form of matrix
a = np.zeros((3, 4))					#create a 3x4 matrix full of zero. Tag:If you don't specify the data type, it will set as float64
a = np.ones((3, 4), dtype=np.int16)		#create a 3x4 matrix full of one, which data type is np.int16	
a = np.empty((3, 4))					#create a 3x4 matrix whose numbers are tend to zero
a = np.arange(10, 20, 2)				#create an array of 10 to 20 steps of 2
a = np.arange(12).reshape((3, 4))		#reshape the array, or transform the array to a matrix
a = np.linspace(1, 10, 20)				#divide the range from 1 to 10 into 20 equal parts
a = np.random.random((2, 3))			#create a 2x3 matrix of random number
#The following are the basic data types commonly used
#int(int64)		int64 	int32	int16
#float(float64)	float64	float32


#basic operation
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
b = np.array([[1, 2, 9, 10], [5, 6, 7, 8]])
c = a + b 				#plus
c = a - b 				#minus
c = a * b 				#multiply
c = a / b 				#divide
c = a ** b				#power
c = np.sin(a)			#sin
c = np.cos(a)			#cos
c = np.tan(a)			#sin
c = a == b 				#return the boolean value of a==b
c = a < b 				#return the boolean value of a<b
c = a > b 				#return the boolean value of a>b


#matrix
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
b = np.array([[1, 2, 8, 2], [6, 9, 1, 2]])
c = a.T 				#give the transpose of a matrix
c = np.dot(a, b.T)		#matrix multiplication
c = a.dot(b.T)			#another form of matrix multiplication
c = np.clip(a, 5, 9)	#If there is a value under 5, then set it to 5. If there is a value larger than 9, then set it to 9.


#index
a = np.arange(2, 14).reshape((3, 4))
b = np.argmin(a)		#give the index of min number in matrix A
b = np.argmax(a)		#give the index of max number in matrix A


#algorithm
a = np.sort(a)			#sort the matrix in every row
c = np.sum(a)			#give the sum of matrix a
c = np.min(a)			#give the min of matrix a
c = np.max(a)			#give the max of matrix a
c = np.sum(a, axis=0)	#give the sum of matrix a in every row
c = np.sum(a, axis=1)	#give the sum of matrix a in every column
a = np.average(A)		#give the average of maxtrix A
a = np.median(A)		#give the median of matrix A
a = np.cumsum(A)		#give the prefix sum in the form of array
a = np.diff(A)			#give the prefix diff in the form of array

#merge
a = np.array([1, 1, 1])
b = np.array([2, 2, 2])
c = np.vstack((a, b))	#vertical stack, 	c:[[1, 1, 1], [2, 2, 2]]
c = np.hstack((a, b))	#horizontal stack, 	c:[1, 1, 1, 2, 2, 2]

#copy
a = b 					#a is b. If you change the value of a later, the value of b will be changed either.
a = b.copy() 			#deep copy. Only copy the value of b to a. If you change the value of a later, the value of b will not be changed.

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值