开发工具:
文件大小: 549kb
下载次数: 0
上传时间: 2019-08-18
详细说明:INTRODUCTION
1.1WhatisNumPy?
NumPyisthefundamentalpackageforscientificcomputinginPython.ItisaPythonlibrarythatprovidesamultidi-
mensionalarrayobject,variousderivedobjects(suchasmaskedarraysandmatrices),andanassortmentofroutinesfor
fastoperationsonarrays,includingmathematical,logical,shapemanipulatioCONTENTS
1 Introduction
1.1 What is NumPy?
1. 2 Building and installing NumP
1.3 How to find documentation
umpy basics
2.1 Data types
2. 2 Array creation
2.3 I/O with Numpy
.,13
2.4 Indexing
20
2.5 Broadcasting
26
2.6 Byte-swapping
29
2.7 Structured arrays(aka“ Record arrays”).
31
2.8 Subclassing ndarray
Performance
4 Miscellaneous
45
4.1 IEEE 754 Floating Point Special values
45
4.2 Examples:··
46
4.3 Interfacing to C
46
4.4 Interfacing to Fortran
48
4.5 Interfacing to C++
48
4.6 Methods vs functions
5 Using Numpy C-API
51
5.1 How to extend NumPy
51
5.2 USing Python as glue
8
5.3 Beyond the basic
Python Module Index
Index
NumPy User Guide, Release 2.0.0.dev
This guide is intended as an introductory overview of NumPy and explains how to install and make use of the most
important features of NumPy. For detailed reference documentation of the functions and classes contained in the
package, see the reference.
Warning: This User Guide"is still a work in progress; some of the material is not organized, and several aspects
of NumPy are not yet covered sufficient detail. We are an open source community continually working to improve
the documentation and eagerly encourage interested parties to contribute. For information on how to do so, please
visit the nunPy doc wiki.
More documentation for Num Py can be found on the numpy. org website
Thanks!
CONTENTS
NumPy User Guide, Release 2.0.0.dev
CONTENTS
CHAPTER
ONE
INTRODUCTION
1.1 What is NumPy?
NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides a multidi-
mensional array object, various derived objects(such as masked arrays and matrices), and an assortment of routines for
fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, 1/O, discrete Fourier
transforms, basic linear algebra, basic statistical operations, random simulation and much more
At the core of the NumPy package, is the ndarray object. This encapsulates n-dimensional arrays of homogeneous
data types, with many operations being performed in compiled code for performance. There are several important
differences between NumPy arrays and the standard Python sequences:
NumPy arrays have a fixed size at creation, unlike Python lists(which can grow dynamically ) Changing the
size of an ndarray will create a new array and delete the original
The elements in a NumPy array are all required to be of the same data type, and thus will be the same size in
memory. The exception: one can have arrays of(Python, including NumPy)objects, thereby allowing for arrays
of different sized elements
Num Py arrays facilitate advanced mathematical and other types of operations on large numbers of data. Typi
cally, such operations are executed more efficiently and with less code than is possible using Python's built-in
sequences.
a growing plethora of scientific and mathematical Python-based packages are using NumPy arrays; though
these typically support Python-sequence input, they convert such input to NunPy arrays prior to processing,
and they often output num py arrays. In other words, in order to efficiently use much(perhaps even most
of todays scientific/mathematical Python-based software, just knowing how to use Pythons built-in sequence
types is insufficient-one also needs to know how to use NumPy arrays.
The points about sequence size and speed are particularly important in scientific computing. As a simple example
consider the case of multiplying each element in a 1-D sequence with the corresponding element in another sequence
of the same length. If the data are stored in two Python lists, a and b, we could iterate over each element
for i in range (len(a)):
append(a[i]b[
This produces the correct answer, but if a and b each contain millions of numbers, we will pay the price for the
inefficiencies of looping in Python. We could accomplish the same task much more quickly in C by writing(for clarity
we neglect variable declarations and initializations, memory allocation, etc.
for (i=C
rows i++):
[i]=a[i]*b[i1;
3
NumPy User Guide, Release 2.0.0.dev
This saves all the overhead involved in interpreting the Python code and manipulating Python objects, but at the
expense of the benefits gained from coding in Python. Furthermore, the coding work required increases with the
dimensionality of our data. In the case of a 2-D array, for example, the C code(abridged as before)expands to
Cor(
s rows i++):I
for (1=0;1< columns; j++):i
C「i1「1
「i1「j*b「i1「j1;
NumPy gives us the best of both worlds: element-by-element operations are the"default mode when an ndarray is
involved, but the element-by-element operation is speedily executed by pre-compiled C code. In NumPy
does what the earlier examples do, at near-C speeds, but with the code simplicity we expect from something based on
Python(indeed, the NumPy idiom is even simpler! ) This last example illustrates two of NumPy's features which are
the basis of much of its power: vectorization and broadcasting
Vectorization describes the absence of any explicit looping, indexing, etc, in the code- these things are taking place
of course, just"behind the scenes(in optimized, pre-compiled C code). Vectorized code has many advantages, among
which are
vectorized code is more concise and easier to read
fewer lines of code generally means fewer bugs
the code more closely resembles standard mathematical notation(making it easier, typically, to correctly code
mathematical constructs
vectorization results in more"Pythonic"code(without vectorization, our code would still be littered with inef
ficient and difficult to read for loops
Broadcasting is the term used to describe the implicit element-by-element behavior of operations; generally speaking
n NumPy all operations (i.e, not just arithmetic operations, but logical, bit-wise, functional, etc. behave in this
implicit element-by-element fashion, i.e, they broadcast. Moreover, in the example above, a and b could be multidi
mensional arrays of the same shape, or a scalar and an array, or even two arrays of with different shapes. Provided that
the smaller array is"expandable " to the shape of the larger in such a way that the resulting broadcast is unambiguous
(for detailed"rules"of broadcasting see numpy. doc. broadcasting)
NumPy fully supports an object-oriented approach, starting once again, with ndarray For example, ndarray is a
class, possessing numerous methods and attributes. Many, of it's nethods mirror functions in the outer-most NumPy
namespace, giving the programmer has complete freedom to code in whichever paradigm she prefers and/or which
seems most appropriate to the task at hand
1.2 Building and installing Num Py
1.2.1 Binary installers
In most use cases the best way to install NumPy on your system is by using an installable binary package for your
operating system.
Windows
Good solutions for Windows are, The Enthought Python Distribution(EPD)(which provides binary installers for
Windows, OS X and Redhat)and Python(x, y). Both of these packages include Python, NumPy and many additional
p
ackages
Chapter 1. Introduction
NumPy User Guide, Release 2.0.0.dev
AlightweightalternativeistodownloadthePythoninstallerfromwww.python.organdtheNumpyinstallerforyour
Python version from the Sourceforge download site
The NumPy installer includes binaries for different CPUs(without SSe instructions, with SSe2 or with SSe3)and
installs the correct one automatically. If needed, this can be bypassed from the command line with
numpy-<1. y z>-superpack-win32 exe /arch nose
or‘sse2’or‘sse3’ instead of nose
nux
Most of the major distributions provide packages for NumPy, but these can lag behind the most recent NumPy release.
Pre-built binary packages for Ubuntu are available on the scipy ppa Redhat binaries are available in the ePd
Mac OS X
A universal binary installer for NumPy is available from the download site. The EPD provides NumPy binaries
1.2.2 Building from source
A general overview of building NumPy from source is given here, with detailed instructions for specific platforms
given seperately
Prerequisites
Building NumPy requires the following software installed
1. Python 2. 4.x, 2.5. or 2.6.x
On Debian and derivative (Ubuntu): python, python-dev
OnWindowstheofficialpythoninstalleratwww.python.orgisenough
Make sure that the Python package distutils is installed before continuing. For example, in Debian gnu/linux
distutils is included in the python-dev package
Python must also be compiled with the zlib module enabled
2. Compilers
77 libraries, so you'll also need a FORTRAN 77 compiler installe r Various Num Py modules use FORTRAN
To build any extension modules for Python, youll need a C compile
Note that NumPy is developed mainly using gnu compilers. Compilers from other vendors such as Intel
Absoft, Sun, NAG, Compaq, Vast, Porland, Lahey, HP, IBM, Microsoft are only supported in the form of
community feedback, and may not work out of the box GCC 3.x(and later)compilers are recommended
3. Linear Algebra libraries
NumPy does not require any external linear algebra libraries to be installed. However, if these are available,
NumPy's setup script can detect them and use them for building. a number of different LaPaCK library setups
can be used including optimized lapacK libraries such as ATLAS, MKL or the accelerate/vecLib framework
on osX
1.2. Building and installing NumPy
NumPy User Guide, Release 2.0.0.dev
FORTRAN ABI mismatch
The two most popular open source fortran compilers are g77 and gfortran. Unfortunately, they are not ABI compatible,
which means that concretely you should avoid mixing libraries built with one with another. In particular, if your
blas/lapack/atlas is built with g77, you must use g77 when building numpy and scipy; on the contrary, if your atlas
is built with gfortran, you must build numpy/scipy with gfortran. This applies for most other cases where different
FORTRAN compilers might have been used
Choosing the fortran compiler
To build with g77:
python setup. py build --fcompiler-gnu
To build with gfortran
python setup. py build --fcompiler-gnu95
For more information see
python setup. py build--help- compiler
How to check the ABl of blas/lapack/atlas
One relatively simple and reliable way to check for the compiler used to build a library is to use ldd on the library. If
libg 2c. so is a dependency, this means that g77 has been used. If libgfortran so is aa dependency, gfortran has been
used. If both are dependencies, this means both have been used, which is almost al ways a very bad idea
Disabling ATLAS and other accelerater libraries
Usage of ATLAS and other accelerated libraries in Numpy can be disabled via
BLAS=None LAPACK=None ATLAS=None python setup. py build
Building with ATLAS support
Ubuntu 8.10(Intrepid)and 9.04(Jaunty
You can install the necessary packages for optimized ATLAS with this command
sudo apt-get install libatlas-base-cev
If you have a recent CPU with SIMD Suppport (SSE, SsE2, etc.), you should also install the corresponding package
for optimal performances. For example, for SsE2
sudo apL- ceL insua111ibaL1as3g⊥-ss∈2
This package is not available on amd64 platforms
NOTE: Ubuntu changed its default fortran compiler from g77 in Hardy to gfortran in Intrepid. If you are buildin
ATLAS from source and are upgrading from Hardy to Intrepid or later versions, you should rebuild everything from
scratch, including lapack
Ubuntu 8.04 and lower
You can install the necessary packages for optimized ATLAS with this command
sudo apt-get install atlas3-base-dev
Chapter 1. Introduction
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.