标签:numpy
安装自行解决
##为什么使用NumPy
文件 vectorSumCompare.py
#!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = ‘teng‘ import sys from datetime import datetime import numpy as np def numpysum(n): a = np.arange(n)**2 b = np.arange(n)**3 c = a+b return c def pythonsum(n): a = range(n) b = range(n) c = [] for i in range(len(a)): a[i] = i**2 b[i] = i**3 c.append(a[i]+ b[i]) return c size = int(sys.argv[1]) start = datetime.now() c = pythonsum(size) print "pythonsum:", c delta = datetime.now() - start print "The last 2 elements of the sum", c[-2:] print "PythonSum elapsed time in microseconds", delta.microseconds start = datetime.now() c = numpysum(size) print "numpysum:", c delta = datetime.now() - start print "The last 2 elements of the sum", c[-2:] print "NumPySum elapsed time in microseconds", delta.microseconds
运行以上脚本 如python vectorSumCompare.py 10000
Numpy的优点
简单
数据量大的时候 速度快
##NumPy数组对象
调试方法shape 返回一个tuple 元组中的元素为NumPy数组每一个维度上的大小
arange 一维数组
In [15]: m = np.array([np.arange(2), np.arange(2)])
In [16]: m
Out[16]: array([[0, 1],[0, 1]])
In [17]: m.shape
Out[17]: (2, 2)
ndarray是一个多维数组对象:
分为两个部分 实际数据和描述这些数据的元数据
标签:numpy
原文地址:http://tengrommel.blog.51cto.com/608570/1741270