`
xpenxpen
  • 浏览: 703688 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

NumPy安装

阅读更多
1. 简介
NumPy是一个基础科学的计算包,包含:
  • 一个强大的N维数组对象
  • 复杂(广播)函数
  • 集成 C/C++/Fortran 的工具
  • 有用的线性代数、傅立叶转换和随机数生成函数


2. 下载安装
本人亲测通过的版本:
Python2.7
NumPy1.9.1

先去python.org/download/下载python-2.7.8.msi
去http://sourceforge.net/projects/numpy/files/NumPy/下载numpy-1.9.1-win32-superpack-python2.7.exe

然后安装,windows上很简单没啥可说的。
python装到C:\Python27就行,而numpy则会自动查找安装好的python,随后会装到C:\Python27\Lib\site-packages下

3. 加C:\Python27加入到环境变量PATH
如果在cygwin里也装过python的话,注意调整PATH的先后顺序。
然后确认一下python路径
where python
C:\Python27\python.exe


4. 测试代码
代码出自于《NumPy Beginners Guide(第二版)》一书。
代码有一些更改,原书代码在32位python下会报错,这里是修正后不报错的代码。
修正地方:dtype=np.int64

import sys
from datetime import datetime
import numpy as np

def numpysum(n):
   a = np.arange(n, dtype=np.int64) ** 2
   b = np.arange(n, dtype=np.int64) ** 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)
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)
delta = datetime.now() - start
print "The last 2 elements of the sum", c[-2:]
print "NumPySum elapsed time in microseconds", delta.microseconds



运行,如果结果没问题代表numpy安装成功。
$ python vectorsum.py 3
The last 2 elements of the sum [2, 12]
PythonSum elapsed time in microseconds 0
The last 2 elements of the sum [ 2 12]
NumPySum elapsed time in microseconds 0

$ python vectorsum.py 30000
The last 2 elements of the sum [26995500239996L, 26998200030000L]
PythonSum elapsed time in microseconds 50000
The last 2 elements of the sum [26995500239996 26998200030000]
NumPySum elapsed time in microseconds 10000

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics