Unit V NumPy
Unit V NumPy
NumPy
NumPy Package
• NumPy, which stands for Numerical Python, is a library consisting of multidimensional
array objects and a collection of routines for processing those arrays.
• Using NumPy, mathematical and logical operations on arrays can be performed.
• 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.
• It is easy to integrate with C/C++ and Fortran.
• For any scientific project, NumPy is the tool to know. It has been built
to work with the N-dimensional array, linear algebra, random number,
Fourier transform, etc.
Operations using NumPy
• numpy.array
• It creates a ndarray from any object exposing an array interface, or
from any method that returns an array.
orderThe order parameter specifies the memory layout of the array. When the object is
not an array, the newly created array will be in C order (row head or row-major) unless 'F'
4 is specified. When F is specified, it will be in Fortran order (column head or column-
major)
subok By default, returned array forced to be a base class array. If true, sub-
5 classes passed through
stop End of interval. The interval does not include this Required
value, except in some cases where step is not an
integer and floating point round-off affects the
length of out.
step Spacing between values. For any output out, this Optional
is the distance between two adjacent values,
out[i+1] - out[i]. The default step size is 1. If step
is specified as a position argument, start must
also be given.
• The linspace() function returns evenly spaced numbers over a specified interval
[start, stop].The endpoint of the interval can optionally be excluded.
• Syntax:
• numpy.linspace(start, stop, num=50, endpoint=True, retstep=False,
dtype=None)
Name Description Required /
Optional
start The starting value of the sequence. Required
stop The end value of the sequence, unless endpoint is set to False. In that Required
case, the sequence consists of all but the last of num + 1 evenly spaced
samples, so that stop is excluded. Note that the step size changes when
endpoint is False.
endpoint If True, stop is the last sample. Otherwise, it is not included. Default is Optional
True.
retstep If True, return (samples, step), where step is the spacing between Optional
samples.
dtype The type of the output array. If dtype is not given, infer the data type Optional
from the other input arguments
• Return value:
• ndarray - There are num equally spaced samples in the closed interval [start, stop] or the half-open
interval [start, stop) (depending on whether endpoint is True or False).
• step : float, optional - Only returned if retstep is True
Size of spacing between samples.
• Example:-
import numpy as np
arr1 = np.linspace(3.0, 4.0, num=7)
print(arr1)
arr2 = np.linspace(3.0,4.0, num=7, endpoint=False)
print(arr2)
arr3 = np.linspace(3.0,4.0, num=7, retstep=True)
print(arr3)
• In simple terms arange returns values based on step size and linspace
relies on sample size.
• arange – Return values with in a range which has a space between
values (in other words the step).
• linspace – Return set of samples with in a given interval.
numpy.ndarray.flatten() function
• The flatten() function is used to get a copy of an given array collapsed
into one dimension.
• Syntax:
• ndarray.flatten(order='C')
Name Description Required /
Optional
order ‘C’ means to flatten in row-major Optional
(C-style) order. ‘F’ means to flatten
in column-major (Fortran- style)
order.
• Return value:
• ndarray - A copy of the input array, flattened to one dimension.
• Example:-
import numpy as np
y = np.array([[2,3], [4,5]])
print(y)
y.flatten()
numpy.ravel() function
• By the shape of the array, we mean the number of rows and columns
of a multi-dimensional array.
• However, the numpy module provides us the way to reshape the array
by changing the number of rows and columns of the multi-
dimensional array.
• The reshape() function associated with the ndarray object is used to
reshape the array. It accepts the two parameters indicating the row and
columns of the new shape of the array.
• Syntax: numpy.reshape(array, shape, order = 'C’)
• array : [array_like]Input array
• shape : [int or tuples of int] e.g. if we are aranging an array with 10
elements then shaping it like numpy.reshape(4, 8) is wrong; we can do
numpy.reshape(2, 5) or (5, 2)
• order : C order means that operating row-rise on the array will be
slightly quicker FORTRAN-contiguous order in memory (first index
varies the fastest). F order means that column-wise operations will be
faster. ‘A’ means to read / write the elements in Fortran-like index
order if, array is Fortran contiguous in memory, C-like order otherwise
• Example:-
#reshaping the array
import numpy as np
a = np.array([[1,2],[3,4],[5,6]])
print("printing the original array..")
print(a)
a=a.reshape(2,3)
print("printing the reshaped array..")
print(a)
Slicing in the Array
print(arr[1:5])
Statical functions in numpy
• Statistics is concerned with collecting and then analysing that data.
• It includes methods for collecting the samples, describing the data, and
then concluding that data.
• NumPy is the fundamental package for scientific calculations and
hence goes hand-in-hand for NumPy statistical Functions.
• NumPy contains various statistical functions that are used to perform
statistical data analysis.
• These statistical functions are useful when finding a maximum or
minimum of elements. It is also used to find basic statistical concepts
like standard deviation, variance, etc.
• 1. np.amin()- This function determines the minimum value of the
element along a specified axis.
2. np.amax()- This function determines the maximum value of the
element along a specified axis.
3. np.mean()- It determines the mean value of the data set.
4. np.median()- It determines the median value of the data set.
5. np.std()- It determines the standard deviation
6. np.var – It determines the variance.
7. np.ptp()- It returns a range of values along an axis.
8. np.average()- It determines the weighted average
9. np.percentile()- It determines the nth percentile of data along the
specified axis.
NumPy Array Axis
• A NumPy multi-dimensional array is represented by the axis where
axis-0 represents the columns and axis-1 represents the rows.
• We can mention the axis to perform row-level or column-level
calculations like the addition of row or column elements.
• To calculate the maximum element among each column, the minimum
element among each row, and the addition of all the row elements.
numpy.min()
• Numpy sum() function is used to sum the elements of Numpy array over
the provided axis.
• Syntax of Numpy sum()
np.sum(a, axis=None, dtype=None)
• Numpy average() is used to calculate the weighted average along the specified axis
• Syntax of Numpy average()
np.average(arr, axis=None, weights=None)
• Arithmetic operations are possible only if the array has the same
structure and dimensions.
• We carry out the operations following the rules of array manipulation.
• We have both functions and operators to perform these functions.
numpy.add()
• numpy.add() function is used when we want to compute the addition of two
array.
• It add arguments element-wise.
• Syntax : numpy.add(arr1, arr2, /, out=None, *, where=True, casting=’same_kind’,
order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘add’)
• Parameters :
arr1 : [array_like or scalar] Input array.
arr2 : [array_like or scalar] Input array.
out : [ndarray, optional] A location into which the result is stored.
-> If provided, it must have a shape that the inputs broadcast to.
-> If not provided or None, a freshly-allocated array is returned.
where : [array_like, optional] Values of True indicate to calculate the
ufunc at that position, values of False indicate to leave the value in the
output alone.
**kwargs :Allows to pass keyword variable length of argument to a
function. Used when we want to handle named argument in a function.
Return : [ndarray or scalar] The sum of arr1 and arr2, element-wise.
Returns a scalar if both arr1 and arr2 are scalars.