numpy学习笔记
1、
numpy求均值、方差、标准差
1 import numpy as np 2 3 arr = [1,2,3,4,5,6] 4 #求均值 5 arr_mean = np.mean(arr) 6 #求方差 7 arr_var = np.var(arr) 8 #求标准差 9 arr_std = np.std(arr,ddof=1)
2、
arange函数
np.arange([start, ]stop, [step, ]dtype=None) start:可忽略不写,默认从0开始;起始值 stop:结束值;生成的元素不包括结束值 step:可忽略不写,默认步长为1;步长 dtype:默认为None,设置显示元素的数据类型 举例: import numpy as np nd1 = np.arange(5)#array([0, 1, 2, 3, 4]) nd2 = np.arange(1,5)#array([1, 2, 3, 4]) nd3 = np.arange(1,5,2)#nd3 = np.arange(1,5,2)#array([1, 3])
3、
dot函数
1、如果处理的是一维数组,则得到的是两数组的內积。 import numpy as np x=np.array([0,1,2,3,4])#等价于:x=np.arange(0,5) y=x[::-1] print x print y print np.dot(x,y) 输出: [0 1 2 3 4] [4 3 2 1 0] 10 2、向量点积 import numpy as np x=np.arange(0,5) y=np.random.randint(0,10,5) print x print y print np.dot(x,y) 输出: [0 1 2 3 4] [5 1 0 9 2] 36 3、矩阵的乘法 import numpy as np x=np.arange(0,5) y=np.random.randint(0,10,size=(5,1)) print x print y print "x.shape:"+str(x.shape) print "y.shape"+str(y.shape) print np.dot(x,y) 输出: [0 1 2 3 4] [[3] [7] [2] [8] [1]] x.shape:(5,) y.shape(5, 1) [39] 4、reshape用法 import numpy as np x=np.arange(0,6).reshape(2,3) y=np.random.randint(0,10,size=(3,2)) print x print y print "x.shape:"+str(x.shape) print "y.shape"+str(y.shape) print np.dot(x,y) 结果: [[0 1 2] [3 4 5]] [[7 5] [0 7] [6 2]] x.shape:(2, 3) y.shape(3, 2) [[12 11] [51 53]]