NumPy - Tutorialspoint

Transcription

NumPyAbout the TutorialNumPy, which stands for Numerical Python, is a library consisting of multidimensionalarray objects and a collection of routines for processing those arrays. Using NumPy,mathematical and logical operations on arrays can be performed.This tutorial explains the basics of NumPy such as its architecture and environment. It alsodiscusses the various array functions, types of indexing, etc. An introduction to Matplotlibis also provided. All this is explained with the help of examples for better understanding.AudienceThis tutorial has been prepared for those who want to learn about the basics and variousfunctions of NumPy. It is specifically useful for algorithm developers. After completing thistutorial, you will find yourself at a moderate level of expertise from where you can takeyourself to higher levels of expertise.PrerequisitesYou should have a basic understanding of computer programming terminologies. A basicunderstanding of Python and any of the programming languages is a plus.Disclaimer & Copyright Copyright 2016 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I)Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republishany contents or a part of contents of this e-book in any manner without written consentof the publisher.We strive to update the contents of our website and tutorials as timely and as precisely aspossible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of ourwebsite or its contents including this tutorial. If you discover any errors on our website orin this tutorial, please notify us at contact@tutorialspoint.com.i

NumPyTable of ContentsAbout the Tutorial . iAudience . iPrerequisites . iDisclaimer & Copyright. iTable of Contents . ii1.NUMPY INTRODUCTION . 12.NUMPY ENVIRONMENT . 23.NUMPY NDARRAY OBJECT . 44.NUMPY DATA TYPES . 7Data Type Objects (dtype) . 85.NUMPY ARRAY ATTRIBUTES. 12ndarray.shape . 12ndarray.ndim . 13numpy.itemsize . 14numpy.flags . 146.NUMPY ARRAY CREATION ROUTINES . 16numpy.empty. 16numpy.zeros . 17numpy.ones . 187.NUMPY ARRAY FROM EXISTING DATA . 19numpy.asarray . 19numpy.frombuffer . 20ii

NumPynumpy.fromiter. 218.NUMPY ARRAY FROM NUMERICAL RANGES . 23numpy.arange . 23numpy.linspace . 24numpy.logspace . 259.NUMPY INDEXING & SLICING. 2710. NUMPY ADVANCED INDEXING. 31Integer Indexing . 31Boolean Array Indexing . 3311. NUMPY BROADCASTING . 3512. NUMPY ITERATING OVER ARRAY . 38Iteration Order . 39Modifying Array Values . 42External Loop . 42Broadcasting Iteration . 4313. NUMPY – ARRAY MANIPULATION. 45numpy.reshape . 47numpy.ndarray.flat . 48numpy.ndarray.flatten . 48numpy.ravel . 49numpy.transpose . 50numpy.ndarray.T. 51numpy.swapaxes . 52numpy.rollaxis . 53numpy.broadcast . 54iii

NumPynumpy.broadcast to . 56numpy.expand dims . 57numpy.squeeze . 59numpy.concatenate . 60numpy.stack . 61numpy.hstack and numpy.vstack . 63numpy.split . 64numpy.hsplit and numpy.vsplit . 65numpy.resize . 66numpy.append . 68numpy.insert . 69numpy.delete . 71numpy.unique . 7214. NUMPY – BINARY OPERATORS . 75bitwise and . 75bitwise or . 76numpy.invert() . 77left shift . 78right shift . 7915. NUMPY STRING FUNCTIONS . 8016. NUMPY MATHEMATICAL FUNCTIONS . 85Trigonometric Functions . 85Functions for Rounding . 8817. NUMPY ARITHMETIC OPERATIONS . 91numpy.reciprocal() . 93iv

NumPynumpy.power() . 94numpy.mod() . 9518. NUMPY STATISTICAL FUNCTIONS . 98numpy.amin() and numpy.amax() . 98numpy.ptp() . 99numpy.percentile() . 100numpy.median(). 102numpy.mean() . 103numpy.average() . 104Standard Deviation . 106Variance . 10619. NUMPY SORT, SEARCH & COUNTING FUNCTIONS . 107numpy.sort() . 107numpy.argsort() . 109numpy.lexsort(). 110numpy.argmax() and numpy.argmin() . 110numpy.nonzero() . 112numpy.where() . 113numpy.extract() . 11420. NUMPY BYTE SWAPPING . 116numpy.ndarray.byteswap() . 11621. NUMPY COPIES & VIEWS . 117No Copy . 117View or Shallow Copy. 118Deep Copy . 120v

NumPy22. NUMPY MATRIX LIBRARY . 123matlib.empty() . 123numpy.matlib.zeros() . 123numpy.matlib.ones() . 124numpy.matlib.eye() . 124numpy.matlib.identity() . 125numpy.matlib.rand() . 12523. NUMPY LINEAR ALGEBRA . 127numpy.dot() . 127numpy.vdot() . 127numpy.inner() . 128numpy.matmul() . 129Determinant. 130numpy.linalg.solve() . 13124. NUMPY MATPLOTLIB . 134Sine Wave Plot . 137subplot() . 138bar() . 14025. NUMPY – HISTOGRAM USING MATPLOTLIB . 141numpy.histogram() . 141plt() . 14126. NUMPY I/O WITH NUMPY . 143numpy.save() . 143savetxt() . 144vi

1. NUMPY INTRODUCTIONNumPyNumPy is a Python package. It stands for 'Numerical Python'. It is a library consisting ofmultidimensional array objects and a collection of routines for processing of array.Numeric, the ancestor of NumPy, was developed by Jim Hugunin. Another package Numarraywas also developed, having some additional functionalities. In 2005, Travis Oliphant createdNumPy package by incorporating the features of Numarray into Numeric package. There aremany contributors to this open source project.Operations using NumPyUsing NumPy, a developer can perform the following operations: Mathematical and logical operations on arrays. Fourier transforms and routines for shape manipulation. Operations related to linear algebra. NumPy has in-built functions for linear algebraand random number generation.NumPy – A Replacement for MatLabNumPy is often used along with packages like SciPy (Scientific Python) and Mat plotlib(plotting library). This combination is widely used as a replacement for MatLab, a popularplatform for technical computing. However, Python alternative to MatLab is now seen as amore modern and complete programming language.It is open source, which is an added advantage of NumPy.7

2. NUMPY ENVIRONMENTNumPyStandard Python distribution doesn't come bundled with NumPy module. A lightweightalternative is to install NumPy using popular Python package installer, pip.pip install numpyThe best way to enable NumPy is to use an installable binary package specific to youroperating system. These binaries contain full SciPy stack (inclusive of NumPy, SciPy,matplotlib, IPython, SymPy and nose packages along with core Python).WindowsAnaconda (from https://www.continuum.io) is a free Python distribution for SciPy stack. It isalso available for Linux and Mac.Canopy (https://www.enthought.com/products/canopy/) is available as free as well ascommercial distribution with full SciPy stack for Windows, Linux and Mac.Python (x,y): It is a free Python distribution with SciPy stack and Spyder IDE for WindowsOS. (Downloadable from http://python-xy.github.io/)LinuxPackage managers of respective Linux distributions are used to install one or more packagesin SciPy stack.For Ubuntusudo apt-get install python-numpy python-scipy python-matplotlibipythonipythonnotebook python-pandas python-sympy python-noseFor Fedorasudo yum install numpyscipy python-matplotlibipython python-pandas sympy pythonnose atlas-develBuilding from SourceCore Python (2.6.x, 2.7.x and 3.2.x onwards) must be installed with distutils and zlib moduleshould be enabled.GNU gcc (4.2 and above) C compiler must be available.8

NumPyTo install NumPy, run the following command.Python setup.py installTo test whether NumPy module is properly installed, try to import it from Python prompt.import numpyIf it is not installed, the following error message will be displayed.Traceback (most recent call last):File " pyshell#0 ", line 1, in module import numpyImportError: No module named 'numpy'Alternatively, NumPy package is imported using the following syntax:import numpy as np9

3. NUMPY NDARRAY OBJECTNumPyThe most important object defined in NumPy is an N-dimensional array type called ndarray.It describes the collection of items of the same type. Items in the collection can be accessedusing a zero-based index.Every item in an ndarray takes the same size of block in the memory. Each element in ndarrayis an object of data-type object (called dtype).Any item extracted from ndarray object (by slicing) is represented by a Python object of oneof array scalar types. The following diagram shows a relationship between ndarray, data typeobject (dtype) and array scalar type:An instance of ndarray class can be constructed by different array creation routines describedlater in the tutorial. The basic ndarray is created using an array function in NumPy as follows:numpy.arrayIt creates an ndarray from any object exposing array interface, or from any method thatreturns an array.numpy.array(object, dtype None, copy True, order None, subok False, ndmin 0)10

NumPyThe above constructor takes the following parameters:objectAny object exposing the array interface method returns an array, or any(nested) sequencedtypeDesired data type of array, optionalcopyOptional. By default (true), the object is copiedorderC (row major) or F (column major) or A (any) (default)subokBy default, returned array forced to be a base class array. If true, sub-classespassed throughndiminSpecifies minimum dimensions of resultant arrayTake a look at the following examples to understand better.Example 1import numpy as npa np.array([1,2,3])print aThe output is as follows:[1, 2, 3]Example 2# more than one dimensionsimport numpy as np11

NumPya np.array([[1, 2], [3, 4]])print aThe output is as follows:[[1, 2][3, 4]]Example 3# minimum dimensionsimport numpy as npa np.array([1, 2, 3,4,5], ndmin 2)print aThe output is as follows:[[1, 2, 3, 4, 5]]Example 4# dtype parameterimport numpy as npa np.array([1, 2, 3], dtype complex)print aThe output is as follows:[ 1. 0.j,2. 0.j,3. 0.j]The ndarray object consists of contiguous one-dimensional segment of computer memory,combined with an indexing scheme that maps each item to a location in the memory block.The memory block holds the elements in a row-major order (C style) or a column-major order(FORTRAN or MatLab style).12

4. NUMPY DATA TYPESNumPyNumPy supports a much greater variety of numerical types than Python does. The followingtable shows different scalar data types defined in NumPy.Data TypesboolDescriptionBoolean (True or False) stored as a byteintDefault integer type (same as C long; normally either int64 or int32)intcIdentical to C int (normally int32 or int64)intpInteger used for indexing (same as C ssize t; normally either int32 or int64)int8Byte (-128 to 127)int16Integer (-32768 to 32767)int32Integer (-2147483648 to 2147483647)int64Integer (-9223372036854775808 to 9223372036854775807)uint8Unsigned integer (0 to 255)uint16Unsigned integer (0 to 65535)uint32Unsigned integer (0 to 4294967295)uint64Unsigned integer (0 to 18446744073709551615)floatShorthand for float64float16Half precision float: sign bit, 5 bits exponent, 10 bits mantissafloat32Single precision float: sign bit, 8 bits exponent, 23 bits mantissafloat64Double precision float: sign bit, 11 bits exponent, 52 bits mantissacomplexShorthand for complex128complex64Complex number, represented by two 32-bit floats (real and imaginarycomponents)complex128Complex number, represented by two 64-bit floats (real and imaginarycomponents)13

NumPyNumPy numerical types are instances of dtype (data-type) objects, each having uniquecharacteristics. The dtypes are available as np.bool , np.float32, etc.Data Type Objects (dtype)A data type object describes interpretation of fixed block of memory corresponding to anarray, depending on the following aspects: Type of data (integer, float or Python object) Size of data Byte order (little-endian or big-endian) In case of structured type, the names of fields, data type of each field and part of thememory block taken by each field If data type is a subarray, its shape and data typeThe byte order is decided by prefixing ' ' or ' ' to data type. ' ' means that encoding is littleendian (least significant is stored in smallest address). ' ' means that encoding is big-endian(most significant byte is stored in smallest address).A dtype object is constructed using the following syntax:numpy.dtype(object, align, copy)The parameters are: Object: To be converted to data type object Align: If true, adds padding to the field to make it similar to C-struct Copy: Makes a new copy of dtype object. If false, the result is reference to built-indata type objectExample 1# using array-scalar typeimport numpy as npdt np.dtype(np.int32)print dtThe output is as follows:int3214

NumPyExample 2#int8, int16, int32, int64 can be replaced by equivalent string 'i1', 'i2','i4',etc.import numpy as npdt np.dtype('i4')print dtThe output is as follows:int32Example 3# using endian notationimport numpy as npdt np.dtype(' i4')print dtThe output is as follows: i4The following examples show the use of structured data type. Here, the field name and thecorresponding scalar data type is to be declared.Example 4# first create structured data typeimport numpy as npdt np.dtype([('age',np.int8)])print dtThe output is as follows:[('age', 'i1')]Example 5# now apply it to ndarray object15

NumPyimport numpy as npdt np.dtype([('age',np.int8)])a np.array([(10,),(20,),(30,)], dtype dt)print aThe output is as follows:[(10,) (20,) (30,)]Example 6# file name can be used to access content of age columnimport numpy as npdt np.dtype([('age',np.int8)])a np.array([(10,),(20,),(30,)], dtype dt)print a['age']The output is as follows:[10 20 30]Example 7The following examples define a structured data type called student with a string field 'name',an integer field 'age' and a float field 'marks'. This dtype is applied to ndarray object.import numpy as npstudent np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])print studentThe output is as follows:[('name', 'S20'), ('age', 'i1'), ('marks', ' f4')])Example 8import numpy as npstudent np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])a np.array([('abc', 21, 50),('xyz', 18, 75)], dtype student)16

NumPyprint aThe output is as follows:[('abc', 21, 50.0), ('xyz', 18, 75.0)]Each built-in data type has a character code that uniquely identifies it. 'b': boolean 'i': (signed) integer 'u': unsigned integer 'f': floating-point 'c': complex-floating point 'm': timedelta 'M': datetime 'O': (Python) objects 'S', 'a': (byte-)string 'U': Unicode 'V': raw data (void)17

5. NUMPY ARRAY ATTRIBUTESNumPyIn this chapter, we will discuss the various array attributes of NumPy.ndarray.shapeThis array attribute returns a tuple consisting of array dimensions. It can also be used toresize the array.Example 1import numpy as npa np.array([[1,2,3],[4,5,6]])print a.shapeThe output is as follows:(2, 3)Example 2# this resizes the ndarrayimport numpy as npa np.array([[1,2,3],[4,5,6]])a.shape (3,2)print aThe output is as follows:[[12][34][56]]Example 3NumPy also provides a reshape function to resize an array.import numpy as np18

NumPya np.array([[1,2,3],[4,5,6]])b a.reshape(3,2)print bThe output is as follows:[[12][34][56]]ndarray.ndimThis array attribute returns the number of array dimensions.Example 4# an array of evenly spaced numbersimport numpy as npa np.arange(24)print aThe output is as follows:[0 12345678910111213141516 17 18 19 20 21 22 23]Example 5# this is one dimensional arrayimport numpy as npa np.arange(24)a.ndim# now reshap

sudo apt-get install python-numpy python-scipy python-matplotlibipythonipython-notebook python-pandas python-sympy python-nose For Fedora sudo yum install numpyscipy python-matplotlibipython python-pandas sympy python-nose atlas-devel Building from Source Core Python (2.6.x, 2.7.x and 3.2.x