NumPy(NumericalPython) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。
NumPy 的前身 Numeric 最早是由 Jim Hugunin 与其它协作者共同开发,2005 年,Travis Oliphant 在 Numeric 中结合了另一个同性质的程序库 Numarray 的特色,并加入了其它扩展而开发了 NumPy。NumPy 为开放源代码并且由许多协作者共同维护开发。
NumPy 是一个运行速度非常快的数学库,主要用于数组计算,包含:
- 一个强大的N维数组对象 ndarray
- 广播功能函数
- 整合 C/C++/Fortran 代码的工具
- 线性代数、傅里叶变换、随机数生成等功能
相关链接
NumPy 官网 http://www.numpy.org/
NumPy 源代码:https://github.com/numpy/numpy
SciPy 官网:https://www.scipy.org/
SciPy 源代码:https://github.com/scipy/scipy
Matplotlib 官网:https://matplotlib.org/
Matplotlib 源代码:https://github.com/matplotlib/matplotlib
# coding:utf-8 import numpy asnp
1产生数组
defcreate_array(): a = np.empty([3,2], dtype = int) print("随机整数数组:\n",a) #随机整数数组: #[[ 81060032 32765] #[ 81064576 32765] #[-67197712 32764]] a = np.zeros([2,2], dtype = float) print("全0数组:\n",a) #全0数组: #[[0. 0.] #[0. 0.]] a = np.ones([2,2], dtype = int) print("全1数组:\n",a) #全1数组: #[[1 1] #[1 1]] a =np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]) print("创建数组:\n",a) #创建数组: #[[ 1 2 3 4] #[ 5 6 7 8] #[ 9 10 11 12] #[13 14 15 16]] x = [(1,2,3,4),(5,6,7,8)] a = np.asarray(x) print("从原来的数组或元组创建数组:\n",a) #从原来的数组或元组创建数组: #[[1 2 3 4] #[5 6 7 8]] a = np.random.random([2,3]) print("产生随机数组:\n",a) #产生0-1随机数组: #[[0.67427601 0.11907 0.99459619] #[0.60160757 0.86183888 0.73418104]] a = np.linspace(start=0,stop=20,num=11) print("产生一维等差数组:\n",a) #产生一维等差数组: #[ 0. 2. 4. 6. 8.10. 12. 14. 16. 18. 20.] a =np.logspace(start=1,stop=3,num=3,endpoint=True,base=2) print("产生[base^start,base^stop] 个数组:\n",a) #产生[base^start,base^stop] 个数组: #[2. 4. 8.]
2 获得属性
defget_attribute(): a =np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]) print("a的行列数:\n",a.shape) print("a的数据类型:\n",a.dtype) print("a的秩:\n",a.ndim) print("a的总个数:\n",a.size)
a的行列数:
(4, 4)
a的数据类型:
int32
a的秩:
2
a的总个数:
16
3 操作
def np_operator(): a =np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) print("a:\n",a) #[[ 1 2 3] #[ 4 5 6] #[ 7 8 9] #[10 11 12]] print("a数组调整:\n",a.reshape(3,4)) #a数组调整: #[[ 1 2 3 4] #[ 5 6 7 8] #[ 9 10 11 12]] print("a前两2行:\n",a[0:2]) #a前两行: #[[1 2 3] #[4 5 6]] print("a第3列:\n",a[:,2]) #a第3列: #[ 3 6 9 12] print("a每个元素+1:\n",a+1) #a每个元素+1: #[[ 2 3 4] #[ 5 6 7] #[ 8 9 10] #[11 12 13]] print("a每个元素平方:\n",a**2) #a每个元素平方: #[[ 1 4 9] #[ 16 25 36] #[ 49 64 81] #[100 121 144]] print("判断a每个元素是否等于5:\n",a==5) #判断a每个元素是否等于5: #[[False False False] #[False True False] #[False False False] #[False False False]] print("a列求和:\n",a.sum(axis=0)) #a列求和: #[22 26 30] print("a行求和:\n",a.sum(axis=1)) #a行求和: #[6 15 24 33] A = np.array([[1,1],[2,1]]) B = np.array([[1,2],[3,4]]) print("A:\n",A) #A: #[[1 1] #[2 1]] print("B:\n",B) #B: #[[1 2] #[3 4]] print("A*B:\n",A*B) #A*B: #[[1 2] #[6 4]] print("矩阵内积:\n",A.dot(B)) #矩阵内积: #[[4 6] #[5 8]] print("矩阵乘法:\n",np.dot(A,B)) #矩阵乘法: # [[4 6] # [5 8]] print("A的扩展:\n",np.tile(A,(2,3))) #A的扩展: # [[1 1 1 1 1 1] # [2 1 2 1 2 1] # [1 1 1 1 1 1] # [2 1 2 1 2 1]] print("纵向量与横向量内积:\n",X.dot(Y)) print("纵向量与横向量乘法:\n",np.dot(X,Y)) print("横向量与纵向量内积:\n",Y.dot(X)) print("横向量与纵向量内积:\n",np.dot(Y,X)) #X = [1,2] #Y = [3, # 4] #横向量与纵向量内积: #[[3 4] #[6 8]] #横向量与纵向量乘法: #[[3 4] #[6 8]] #纵向量与横向量内积: #[[11]] #纵向量与横向量内积: #[[11]]
4 排序和索引
def ord_index(): a =np.array([[1,3,2],[6,5,4],[7,8,9],[11,10,12]]) print("a:\n",a) #a: #[[ 1 3 2] #[ 6 5 4] #[ 7 8 9] #[11 10 12]] index = a.argmax(axis=0) print("a中每一列最大值索引:\n",index) #a中每一列最大值索引: #[3 3 3] print("a中每一列最大值:\n",a[index,range(a.shape[1])]) #a中每一列最大值: #[11 10 12] print("a中每行元素从小到大排序:\n",np.sort(a,axis=1)) #a中每行元素从小到大排序: #[[ 1 2 3] #[ 4 5 6] # [ 7 8 9] #[10 11 12]] print("返回数组从小到大的索引值:\n",np.argsort(a)) #返回数组从小到大的索引值: #[[0 2 1] #[2 1 0] #[0 1 2] #[1 0 2]]
5 读取文件
def load_file(): a = range(10) np.save('testfile',a) b = np.load('testfile.npy') print(b) #[0 1 2 3 4 5 6 7 8 9] a = range(15) np.savetxt('mydata.txt',a) b = np.loadtxt('mydata.txt') print(b) #[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.]
if __name__=="__main__": create_array() get_attribute() np_operator() ord_index() load_file()
————————————————————
软件安全测试
https://study.163.com/course/courseMain.htm?courseId=1209779852&share=2&shareId=480000002205486
接口自动化测试
https://study.163.com/course/courseMain.htm?courseId=1209794815&share=2&shareId=480000002205486
DevOps 和Jenkins之DevOps
https://study.163.com/course/courseMain.htm?courseId=1209817844&share=2&shareId=480000002205486
DevOps与Jenkins 2.0之Jenkins
https://study.163.com/course/courseMain.htm?courseId=1209819843&share=2&shareId=480000002205486
Selenium自动化测试
https://study.163.com/course/courseMain.htm?courseId=1209835807&share=2&shareId=480000002205486
性能测试第1季:性能测试基础知识
https://study.163.com/course/courseMain.htm?courseId=1209852815&share=2&shareId=480000002205486
性能测试第2季:LoadRunner12使用
https://study.163.com/course/courseMain.htm?courseId=1209980013&share=2&shareId=480000002205486
性能测试第3季:JMeter工具使用
https://study.163.com/course/courseMain.htm?courseId=1209903814&share=2&shareId=480000002205486
性能测试第4季:监控与调优
https://study.163.com/course/courseMain.htm?courseId=1209959801&share=2&shareId=480000002205486
Django入门
https://study.163.com/course/courseMain.htm?courseId=1210020806&share=2&shareId=480000002205486
啄木鸟顾老师漫谈软件测试
https://study.163.com/course/courseMain.htm?courseId=1209958326&share=2&shareId=480000002205486