NumPy – Array Creation Routines ”; Previous Next A new ndarray object can be constructed by any of the following array creation routines or using a low-level ndarray constructor. numpy.empty It creates an uninitialized array of specified shape and dtype. It uses the following constructor − numpy.empty(shape, dtype = float, order = ”C”) The constructor takes the following parameters. Sr.No. Parameter & Description 1 Shape Shape of an empty array in int or tuple of int 2 Dtype Desired output data type. Optional 3 Order ”C” for C-style row-major array, ”F” for FORTRAN style column-major array Example The following code shows an example of an empty array. Live Demo import numpy as np x = np.empty([3,2], dtype = int) print x The output is as follows − [[22649312 1701344351] [1818321759 1885959276] [16779776 156368896]] Note − The elements in an array show random values as they are not initialized. numpy.zeros Returns a new array of specified size, filled with zeros. numpy.zeros(shape, dtype = float, order = ”C”) The constructor takes the following parameters. Sr.No. Parameter & Description 1 Shape Shape of an empty array in int or sequence of int 2 Dtype Desired output data type. Optional 3 Order ”C” for C-style row-major array, ”F” for FORTRAN style column-major array Example 1 Live Demo # array of five zeros. Default dtype is float import numpy as np x = np.zeros(5) print x The output is as follows − [ 0. 0. 0. 0. 0.] Example 2 Live Demo import numpy as np x = np.zeros((5,), dtype = np.int) print x Now, the output would be as follows − [0 0 0 0 0] Example 3 Live Demo # custom type import numpy as np x = np.zeros((2,2), dtype = [(”x”, ”i4”), (”y”, ”i4”)]) print x It should produce the following output − [[(0,0)(0,0)] [(0,0)(0,0)]] numpy.ones Returns a new array of specified size and type, filled with ones. numpy.ones(shape, dtype = None, order = ”C”) The constructor takes the following parameters. Sr.No. Parameter & Description 1 Shape Shape of an empty array in int or tuple of int 2 Dtype Desired output data type. Optional 3 Order ”C” for C-style row-major array, ”F” for FORTRAN style column-major array Example 1 Live Demo # array of five ones. Default dtype is float import numpy as np x = np.ones(5) print x The output is as follows − [ 1. 1. 1. 1. 1.] Example 2 Live Demo import numpy as np x = np.ones([2,2], dtype = int) print x Now, the output would be as follows − [[1 1] [1 1]] Print Page Previous Next Advertisements ”;
Category: numpy
NumPy – Advanced Indexing
NumPy – Advanced Indexing ”; Previous Next It is possible to make a selection from ndarray that is a non-tuple sequence, ndarray object of integer or Boolean data type, or a tuple with at least one item being a sequence object. Advanced indexing always returns a copy of the data. As against this, the slicing only presents a view. There are two types of advanced indexing − Integer and Boolean. Integer Indexing This mechanism helps in selecting any arbitrary item in an array based on its Ndimensional index. Each integer array represents the number of indexes into that dimension. When the index consists of as many integer arrays as the dimensions of the target ndarray, it becomes straightforward. In the following example, one element of specified column from each row of ndarray object is selected. Hence, the row index contains all row numbers, and the column index specifies the element to be selected. Example 1 Live Demo import numpy as np x = np.array([[1, 2], [3, 4], [5, 6]]) y = x[[0,1,2], [0,1,0]] print y Its output would be as follows − [1 4 5] The selection includes elements at (0,0), (1,1) and (2,0) from the first array. In the following example, elements placed at corners of a 4X3 array are selected. The row indices of selection are [0, 0] and [3,3] whereas the column indices are [0,2] and [0,2]. Example 2 Live Demo import numpy as np x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]]) print ”Our array is:” print x print ”n” rows = np.array([[0,0],[3,3]]) cols = np.array([[0,2],[0,2]]) y = x[rows,cols] print ”The corner elements of this array are:” print y The output of this program is as follows − Our array is: [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] The corner elements of this array are: [[ 0 2] [ 9 11]] The resultant selection is an ndarray object containing corner elements. Advanced and basic indexing can be combined by using one slice (:) or ellipsis (…) with an index array. The following example uses slice for row and advanced index for column. The result is the same when slice is used for both. But advanced index results in copy and may have different memory layout. Example 3 Live Demo import numpy as np x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]]) print ”Our array is:” print x print ”n” # slicing z = x[1:4,1:3] print ”After slicing, our array becomes:” print z print ”n” # using advanced index for column y = x[1:4,[1,2]] print ”Slicing using advanced index for column:” print y The output of this program would be as follows − Our array is: [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] After slicing, our array becomes: [[ 4 5] [ 7 8] [10 11]] Slicing using advanced index for column: [[ 4 5] [ 7 8] [10 11]] Boolean Array Indexing This type of advanced indexing is used when the resultant object is meant to be the result of Boolean operations, such as comparison operators. Example 1 In this example, items greater than 5 are returned as a result of Boolean indexing. Live Demo import numpy as np x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]]) print ”Our array is:” print x print ”n” # Now we will print the items greater than 5 print ”The items greater than 5 are:” print x[x > 5] The output of this program would be − Our array is: [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] The items greater than 5 are: [ 6 7 8 9 10 11] Example 2 In this example, NaN (Not a Number) elements are omitted by using ~ (complement operator). Live Demo import numpy as np a = np.array([np.nan, 1,2,np.nan,3,4,5]) print a[~np.isnan(a)] Its output would be − [ 1. 2. 3. 4. 5.] Example 3 The following example shows how to filter out the non-complex elements from an array. Live Demo import numpy as np a = np.array([1, 2+6j, 5, 3.5+5j]) print a[np.iscomplex(a)] Here, the output is as follows − [2.0+6.j 3.5+5.j] Print Page Previous Next Advertisements ”;
NumPy – Array From Existing Data ”; Previous Next In this chapter, we will discuss how to create an array from existing data. numpy.asarray This function is similar to numpy.array except for the fact that it has fewer parameters. This routine is useful for converting Python sequence into ndarray. numpy.asarray(a, dtype = None, order = None) The constructor takes the following parameters. Sr.No. Parameter & Description 1 a Input data in any form such as list, list of tuples, tuples, tuple of tuples or tuple of lists 2 dtype By default, the data type of input data is applied to the resultant ndarray 3 order C (row major) or F (column major). C is default The following examples show how you can use the asarray function. Example 1 Live Demo # convert list to ndarray import numpy as np x = [1,2,3] a = np.asarray(x) print a Its output would be as follows − [1 2 3] Example 2 Live Demo # dtype is set import numpy as np x = [1,2,3] a = np.asarray(x, dtype = float) print a Now, the output would be as follows − [ 1. 2. 3.] Example 3 Live Demo # ndarray from tuple import numpy as np x = (1,2,3) a = np.asarray(x) print a Its output would be − [1 2 3] Example 4 Live Demo # ndarray from list of tuples import numpy as np x = [(1,2,3),(4,5)] a = np.asarray(x) print a Here, the output would be as follows − [(1, 2, 3) (4, 5)] numpy.frombuffer This function interprets a buffer as one-dimensional array. Any object that exposes the buffer interface is used as parameter to return an ndarray. numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0) The constructor takes the following parameters. Sr.No. Parameter & Description 1 buffer Any object that exposes buffer interface 2 dtype Data type of returned ndarray. Defaults to float 3 count The number of items to read, default -1 means all data 4 offset The starting position to read from. Default is 0 Example The following examples demonstrate the use of frombuffer function. Live Demo import numpy as np s = ”Hello World” a = np.frombuffer(s, dtype = ”S1”) print a Here is its output − [”H” ”e” ”l” ”l” ”o” ” ” ”W” ”o” ”r” ”l” ”d”] numpy.fromiter This function builds an ndarray object from any iterable object. A new one-dimensional array is returned by this function. numpy.fromiter(iterable, dtype, count = -1) Here, the constructor takes the following parameters. Sr.No. Parameter & Description 1 iterable Any iterable object 2 dtype Data type of resultant array 3 count The number of items to be read from iterator. Default is -1 which means all data to be read The following examples show how to use the built-in range() function to return a list object. An iterator of this list is used to form an ndarray object. Example 1 Live Demo # create list object using range function import numpy as np list = range(5) print list Its output is as follows − [0, 1, 2, 3, 4] Example 2 Live Demo # obtain iterator object from list import numpy as np list = range(5) it = iter(list) # use iterator to create ndarray x = np.fromiter(it, dtype = float) print x Now, the output would be as follows − [0. 1. 2. 3. 4.] Print Page Previous Next Advertisements ”;
NumPy – Introduction
NumPy – Introduction ”; Previous Next NumPy is a Python package. It stands for ”Numerical Python”. It is a library consisting of multidimensional array objects and a collection of routines for processing of array. Numeric, the ancestor of NumPy, was developed by Jim Hugunin. Another package Numarray was also developed, having some additional functionalities. In 2005, Travis Oliphant created NumPy package by incorporating the features of Numarray into Numeric package. There are many contributors to this open source project. Operations using NumPy Using 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 algebra and random number generation. NumPy – A Replacement for MatLab NumPy 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 popular platform for technical computing. However, Python alternative to MatLab is now seen as a more modern and complete programming language. It is open source, which is an added advantage of NumPy. Print Page Previous Next Advertisements ”;
NumPy – Array Attributes
NumPy – Array Attributes ”; Previous Next In this chapter, we will discuss the various array attributes of NumPy. ndarray.shape This array attribute returns a tuple consisting of array dimensions. It can also be used to resize the array. Example 1 Live Demo import numpy as np a = np.array([[1,2,3],[4,5,6]]) print a.shape The output is as follows − (2, 3) Example 2 Live Demo # this resizes the ndarray import numpy as np a = np.array([[1,2,3],[4,5,6]]) a.shape = (3,2) print a The output is as follows − [[1, 2] [3, 4] [5, 6]] Example 3 NumPy also provides a reshape function to resize an array. Live Demo import numpy as np a = np.array([[1,2,3],[4,5,6]]) b = a.reshape(3,2) print b The output is as follows − [[1, 2] [3, 4] [5, 6]] ndarray.ndim This array attribute returns the number of array dimensions. Example 1 Live Demo # an array of evenly spaced numbers import numpy as np a = np.arange(24) print a The output is as follows − [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23] Example 2 Live Demo # this is one dimensional array import numpy as np a = np.arange(24) a.ndim # now reshape it b = a.reshape(2,4,3) print b # b is having three dimensions The output is as follows − [[[ 0, 1, 2] [ 3, 4, 5] [ 6, 7, 8] [ 9, 10, 11]] [[12, 13, 14] [15, 16, 17] [18, 19, 20] [21, 22, 23]]] numpy.itemsize This array attribute returns the length of each element of array in bytes. Example 1 Live Demo # dtype of array is int8 (1 byte) import numpy as np x = np.array([1,2,3,4,5], dtype = np.int8) print x.itemsize The output is as follows − 1 Example 2 Live Demo # dtype of array is now float32 (4 bytes) import numpy as np x = np.array([1,2,3,4,5], dtype = np.float32) print x.itemsize The output is as follows − 4 numpy.flags The ndarray object has the following attributes. Its current values are returned by this function. Sr.No. Attribute & Description 1 C_CONTIGUOUS (C) The data is in a single, C-style contiguous segment 2 F_CONTIGUOUS (F) The data is in a single, Fortran-style contiguous segment 3 OWNDATA (O) The array owns the memory it uses or borrows it from another object 4 WRITEABLE (W) The data area can be written to. Setting this to False locks the data, making it read-only 5 ALIGNED (A) The data and all elements are aligned appropriately for the hardware 6 UPDATEIFCOPY (U) This array is a copy of some other array. When this array is deallocated, the base array will be updated with the contents of this array Example The following example shows the current values of flags. Live Demo import numpy as np x = np.array([1,2,3,4,5]) print x.flags The output is as follows − C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : True WRITEABLE : True ALIGNED : True UPDATEIFCOPY : False Print Page Previous Next Advertisements ”;
NumPy – Ndarray Object
NumPy – Ndarray Object ”; Previous Next The 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 accessed using a zero-based index. Every item in an ndarray takes the same size of block in the memory. Each element in ndarray is an object of data-type object (called dtype). Any item extracted from ndarray object (by slicing) is represented by a Python object of one of array scalar types. The following diagram shows a relationship between ndarray, data type object (dtype) and array scalar type − An instance of ndarray class can be constructed by different array creation routines described later in the tutorial. The basic ndarray is created using an array function in NumPy as follows − numpy.array It creates an ndarray from any object exposing array interface, or from any method that returns an array. numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0) The above constructor takes the following parameters − Sr.No. Parameter & Description 1 object Any object exposing the array interface method returns an array, or any (nested) sequence. 2 dtype Desired data type of array, optional 3 copy Optional. By default (true), the object is copied 4 order C (row major) or F (column major) or A (any) (default) 5 subok By default, returned array forced to be a base class array. If true, sub-classes passed through 6 ndmin Specifies minimum dimensions of resultant array Take a look at the following examples to understand better. Example 1 Live Demo import numpy as np a = np.array([1,2,3]) print a The output is as follows − [1, 2, 3] Example 2 Live Demo # more than one dimensions import numpy as np a = np.array([[1, 2], [3, 4]]) print a The output is as follows − [[1, 2] [3, 4]] Example 3 Live Demo # minimum dimensions import numpy as np a = np.array([1, 2, 3,4,5], ndmin = 2) print a The output is as follows − [[1, 2, 3, 4, 5]] Example 4 Live Demo # dtype parameter import numpy as np a = np.array([1, 2, 3], dtype = complex) print a The 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). Print Page Previous Next Advertisements ”;
Array From Numerical Ranges
NumPy – Array From Numerical Ranges ”; Previous Next In this chapter, we will see how to create an array from numerical ranges. numpy.arange This function returns an ndarray object containing evenly spaced values within a given range. The format of the function is as follows − numpy.arange(start, stop, step, dtype) The constructor takes the following parameters. Sr.No. Parameter & Description 1 start The start of an interval. If omitted, defaults to 0 2 stop The end of an interval (not including this number) 3 step Spacing between values, default is 1 4 dtype Data type of resulting ndarray. If not given, data type of input is used The following examples show how you can use this function. Example 1 Live Demo import numpy as np x = np.arange(5) print x Its output would be as follows − [0 1 2 3 4] Example 2 Live Demo import numpy as np # dtype set x = np.arange(5, dtype = float) print x Here, the output would be − [0. 1. 2. 3. 4.] Example 3 Live Demo # start and stop parameters set import numpy as np x = np.arange(10,20,2) print x Its output is as follows − [10 12 14 16 18] numpy.linspace This function is similar to arange() function. In this function, instead of step size, the number of evenly spaced values between the interval is specified. The usage of this function is as follows − numpy.linspace(start, stop, num, endpoint, retstep, dtype) The constructor takes the following parameters. Sr.No. Parameter & Description 1 start The starting value of the sequence 2 stop The end value of the sequence, included in the sequence if endpoint set to true 3 num The number of evenly spaced samples to be generated. Default is 50 4 endpoint True by default, hence the stop value is included in the sequence. If false, it is not included 5 retstep If true, returns samples and step between the consecutive numbers 6 dtype Data type of output ndarray The following examples demonstrate the use linspace function. Example 1 Live Demo import numpy as np x = np.linspace(10,20,5) print x Its output would be − [10. 12.5 15. 17.5 20.] Example 2 Live Demo # endpoint set to false import numpy as np x = np.linspace(10,20, 5, endpoint = False) print x The output would be − [10. 12. 14. 16. 18.] Example 3 Live Demo # find retstep value import numpy as np x = np.linspace(1,2,5, retstep = True) print x # retstep here is 0.25 Now, the output would be − (array([ 1. , 1.25, 1.5 , 1.75, 2. ]), 0.25) numpy.logspace This function returns an ndarray object that contains the numbers that are evenly spaced on a log scale. Start and stop endpoints of the scale are indices of the base, usually 10. numpy.logspace(start, stop, num, endpoint, base, dtype) Following parameters determine the output of logspace function. Sr.No. Parameter & Description 1 start The starting point of the sequence is basestart 2 stop The final value of sequence is basestop 3 num The number of values between the range. Default is 50 4 endpoint If true, stop is the last value in the range 5 base Base of log space, default is 10 6 dtype Data type of output array. If not given, it depends upon other input arguments The following examples will help you understand the logspace function. Example 1 Live Demo import numpy as np # default base is 10 a = np.logspace(1.0, 2.0, num = 10) print a Its output would be as follows − [ 10. 12.91549665 16.68100537 21.5443469 27.82559402 35.93813664 46.41588834 59.94842503 77.42636827 100. ] Example 2 Live Demo # set base of log space to 2 import numpy as np a = np.logspace(1,10,num = 10, base = 2) print a Now, the output would be − [ 2. 4. 8. 16. 32. 64. 128. 256. 512. 1024.] Print Page Previous Next Advertisements ”;
NumPy – Matrix Library
NumPy – Matrix Library ”; Previous Next NumPy package contains a Matrix library numpy.matlib. This module has functions that return matrices instead of ndarray objects. matlib.empty() The matlib.empty() function returns a new matrix without initializing the entries. The function takes the following parameters. numpy.matlib.empty(shape, dtype, order) Where, Sr.No. Parameter & Description 1 shape int or tuple of int defining the shape of the new matrix 2 Dtype Optional. Data type of the output 3 order C or F Example Live Demo import numpy.matlib import numpy as np print np.matlib.empty((2,2)) # filled with random data It will produce the following output − [[ 2.12199579e-314, 4.24399158e-314] [ 4.24399158e-314, 2.12199579e-314]] numpy.matlib.zeros() This function returns the matrix filled with zeros. Live Demo import numpy.matlib import numpy as np print np.matlib.zeros((2,2)) It will produce the following output − [[ 0. 0.] [ 0. 0.]] numpy.matlib.ones() This function returns the matrix filled with 1s. Live Demo import numpy.matlib import numpy as np print np.matlib.ones((2,2)) It will produce the following output − [[ 1. 1.] [ 1. 1.]] numpy.matlib.eye() This function returns a matrix with 1 along the diagonal elements and the zeros elsewhere. The function takes the following parameters. numpy.matlib.eye(n, M,k, dtype) Where, Sr.No. Parameter & Description 1 n The number of rows in the resulting matrix 2 M The number of columns, defaults to n 3 k Index of diagonal 4 dtype Data type of the output Example Live Demo import numpy.matlib import numpy as np print np.matlib.eye(n = 3, M = 4, k = 0, dtype = float) It will produce the following output − [[ 1. 0. 0. 0.] [ 0. 1. 0. 0.] [ 0. 0. 1. 0.]] numpy.matlib.identity() The numpy.matlib.identity() function returns the Identity matrix of the given size. An identity matrix is a square matrix with all diagonal elements as 1. Live Demo import numpy.matlib import numpy as np print np.matlib.identity(5, dtype = float) It will produce the following output − [[ 1. 0. 0. 0. 0.] [ 0. 1. 0. 0. 0.] [ 0. 0. 1. 0. 0.] [ 0. 0. 0. 1. 0.] [ 0. 0. 0. 0. 1.]] numpy.matlib.rand() The numpy.matlib.rand() function returns a matrix of the given size filled with random values. Example Live Demo import numpy.matlib import numpy as np print np.matlib.rand(3,3) It will produce the following output − [[ 0.82674464 0.57206837 0.15497519] [ 0.33857374 0.35742401 0.90895076] [ 0.03968467 0.13962089 0.39665201]] Note that a matrix is always two-dimensional, whereas ndarray is an n-dimensional array. Both the objects are inter-convertible. Example Live Demo import numpy.matlib import numpy as np i = np.matrix(”1,2;3,4”) print i It will produce the following output − [[1 2] [3 4]] Example import numpy.matlib import numpy as np j = np.asarray(i) print j It will produce the following output − [[1 2] [3 4]] Example import numpy.matlib import numpy as np k = np.asmatrix (j) print k It will produce the following output − [[1 2] [3 4]] Print Page Previous Next Advertisements ”;
NumPy – Statistical Functions ”; Previous Next NumPy has quite a few useful statistical functions for finding minimum, maximum, percentile standard deviation and variance, etc. from the given elements in the array. The functions are explained as follows − numpy.amin() and numpy.amax() These functions return the minimum and the maximum from the elements in the given array along the specified axis. Example Live Demo import numpy as np a = np.array([[3,7,5],[8,4,3],[2,4,9]]) print ”Our array is:” print a print ”n” print ”Applying amin() function:” print np.amin(a,1) print ”n” print ”Applying amin() function again:” print np.amin(a,0) print ”n” print ”Applying amax() function:” print np.amax(a) print ”n” print ”Applying amax() function again:” print np.amax(a, axis = 0) It will produce the following output − Our array is: [[3 7 5] [8 4 3] [2 4 9]] Applying amin() function: [3 3 2] Applying amin() function again: [2 4 3] Applying amax() function: 9 Applying amax() function again: [8 7 9] numpy.ptp() The numpy.ptp() function returns the range (maximum-minimum) of values along an axis. Live Demo import numpy as np a = np.array([[3,7,5],[8,4,3],[2,4,9]]) print ”Our array is:” print a print ”n” print ”Applying ptp() function:” print np.ptp(a) print ”n” print ”Applying ptp() function along axis 1:” print np.ptp(a, axis = 1) print ”n” print ”Applying ptp() function along axis 0:” print np.ptp(a, axis = 0) It will produce the following output − Our array is: [[3 7 5] [8 4 3] [2 4 9]] Applying ptp() function: 7 Applying ptp() function along axis 1: [4 5 7] Applying ptp() function along axis 0: [6 3 6] numpy.percentile() Percentile (or a centile) is a measure used in statistics indicating the value below which a given percentage of observations in a group of observations fall. The function numpy.percentile() takes the following arguments. numpy.percentile(a, q, axis) Where, Sr.No. Argument & Description 1 a Input array 2 q The percentile to compute must be between 0-100 3 axis The axis along which the percentile is to be calculated Example Live Demo import numpy as np a = np.array([[30,40,70],[80,20,10],[50,90,60]]) print ”Our array is:” print a print ”n” print ”Applying percentile() function:” print np.percentile(a,50) print ”n” print ”Applying percentile() function along axis 1:” print np.percentile(a,50, axis = 1) print ”n” print ”Applying percentile() function along axis 0:” print np.percentile(a,50, axis = 0) It will produce the following output − Our array is: [[30 40 70] [80 20 10] [50 90 60]] Applying percentile() function: 50.0 Applying percentile() function along axis 1: [ 40. 20. 60.] Applying percentile() function along axis 0: [ 50. 40. 60.] numpy.median() Median is defined as the value separating the higher half of a data sample from the lower half. The numpy.median() function is used as shown in the following program. Example Live Demo import numpy as np a = np.array([[30,65,70],[80,95,10],[50,90,60]]) print ”Our array is:” print a print ”n” print ”Applying median() function:” print np.median(a) print ”n” print ”Applying median() function along axis 0:” print np.median(a, axis = 0) print ”n” print ”Applying median() function along axis 1:” print np.median(a, axis = 1) It will produce the following output − Our array is: [[30 65 70] [80 95 10] [50 90 60]] Applying median() function: 65.0 Applying median() function along axis 0: [ 50. 90. 60.] Applying median() function along axis 1: [ 65. 80. 60.] numpy.mean() Arithmetic mean is the sum of elements along an axis divided by the number of elements. The numpy.mean() function returns the arithmetic mean of elements in the array. If the axis is mentioned, it is calculated along it. Example Live Demo import numpy as np a = np.array([[1,2,3],[3,4,5],[4,5,6]]) print ”Our array is:” print a print ”n” print ”Applying mean() function:” print np.mean(a) print ”n” print ”Applying mean() function along axis 0:” print np.mean(a, axis = 0) print ”n” print ”Applying mean() function along axis 1:” print np.mean(a, axis = 1) It will produce the following output − Our array is: [[1 2 3] [3 4 5] [4 5 6]] Applying mean() function: 3.66666666667 Applying mean() function along axis 0: [ 2.66666667 3.66666667 4.66666667] Applying mean() function along axis 1: [ 2. 4. 5.] numpy.average() Weighted average is an average resulting from the multiplication of each component by a factor reflecting its importance. The numpy.average() function computes the weighted average of elements in an array according to their respective weight given in another array. The function can have an axis parameter. If the axis is not specified, the array is flattened. Considering an array [1,2,3,4] and corresponding weights [4,3,2,1], the weighted average is calculated by adding the product of the corresponding elements and dividing the sum by the sum of weights. Weighted average = (1*4+2*3+3*2+4*1)/(4+3+2+1) Example Live Demo import numpy as np a = np.array([1,2,3,4]) print ”Our array is:” print a print ”n” print ”Applying average() function:” print np.average(a) print ”n” # this is same as mean when weight is not specified wts = np.array([4,3,2,1]) print ”Applying average() function again:” print np.average(a,weights = wts) print ”n” # Returns the sum of weights, if the returned parameter is set to True. print ”Sum of weights” print np.average([1,2,3, 4],weights = [4,3,2,1], returned = True) It will produce the following output − Our array is: [1 2 3 4] Applying average() function: 2.5 Applying average() function again: 2.0 Sum of weights (2.0, 10.0) In a multi-dimensional array, the axis for computation can be specified. Example Live Demo import numpy as np a = np.arange(6).reshape(3,2) print ”Our array is:” print a print ”n” print ”Modified array:” wt = np.array([3,5]) print np.average(a, axis = 1, weights = wt) print ”n” print ”Modified array:” print np.average(a, axis = 1, weights = wt, returned = True) It will produce the following output − Our array is: [[0 1] [2 3] [4 5]] Modified array: [ 0.625 2.625 4.625] Modified array: (array([ 0.625, 2.625, 4.625]), array([ 8., 8., 8.])) Standard Deviation Standard deviation is the square root of the average of squared deviations from mean. The formula for standard deviation is as follows −