1. np.log10()
计算以10为底的对数值
import numpy as np
np.log10(x)
>>>np.log10(100)
2.0
2. np. log()
计算以e为底的对数值
import numpy as np
np.log(x)
>>>np.log(np.e)
1.0
>>>np.log(10)
2.3025850929940459
3. np.log2()
计算以2为底的对数值,直接把2放在log和()之间
import numpy as np
np.log2(x)
>>>np.log2(4)
2.0
4. np.random.shuffle(x)
将给定的数组的内容进行重新排序(类似于洗牌,打乱顺序)
arr=np.arange(10)
np.random.shuffle(arr)
>>>arr
array([5, 2, 7, 0, 6, 3, 4, 1, 8, 9])
#多维
arr=np.arange(12).reshape(3,4)
>>>arr
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
np.random.shuffle(arr)
>>>arr
array([[ 4, 5, 6, 7],
[ 0, 1, 2, 3],
[ 8, 9, 10, 11]])
5. np.mean()
功能:求平均值
通式:numpy.mean(a,axis,dtype,out,keepdims)
常用的参数:axis
eg: m*n的矩阵
- 不设置axis:对m*n个值求平均
- axis=0:对每一列求平均值
- axis=1:对每一行求平均值
>>>import numpy as np
>>>a=np.array([[1,2],[3,4])
>>>a
array([[1,2],
[3,4]])
>>>np.mean(a)
2.5
>>>np.mean(a,axis=0)
array([2.0,3.0])
>>>np.mean(a,axis=1)
array([1.5,3.5])
>>>import numpy as np
>>>a=array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
>>>a
array([
[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]])
>>>b=np.mat(a)
>>>b
matrix([
[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]])
>>>np.mean(b)
6.5
>>>np.mean(b,0)
matrix[[5.5,6.5,7.5]]
>>>np.mean(b,1)
matrix[[2.0],
[5.0],
[8.0],
[11.0]]
6. np.random. choice()
从一个int数字或1维array里随机选取内容
通式:np.random.choice(a, size=None, replace=True, p=None)
>>>import numpy as np
>>>np.random.choice(5,3)#从0-4五个数中任取三个
array([0,3,4])
>>>np.random.choice(5,3,p=[0.1,0,0.3,0.6,0])#从0-4五个数中以概率p随机取三个
array=([3,3,0])
>>>np.random.choice(5,3,replace=False,p=[0.1,0,0.3,0.6,0])#从0-4五个数中不重复的以概率p随机取三个
array([2,3,0])
7. np.ceil()和np.floor()
向上取整和向下取整
8. np.clip()
通式:numpy.clip(a, a_min, a_max, out=None)[source]
其中a是一个数组,后面两个参数分别表示最小和最大值,将数组中大于最大值的值,改为最大值,小于最小值的值,改为最小值,其余不变。eg:
import numpy as np
x=np.array([1,2,3,5,6,7,8,9])
np.clip(x,3,8)
输出:array([3,3,3,5,6,7,8,8])
高维数组道理一样:
import numpy as np
x=np.array([[1,2,3,5,6,7,8,9],[1,2,3,5,6,7,8,9]])
np.clip(x,3,8)
输出:
array([[3, 3, 3, 5, 6, 7, 8, 8],
[3, 3, 3, 5, 6, 7, 8, 8]])