numpy
numpy
Introduction
• Stands for numerical python.
• Fundamental Package for numerical Computations.
• Supports N dimensional array objects that can be used for processing
multi-dimensional data.
• Supports different data types.
Introduction
• Using Numpy we can perform
• Mathematical and logical operations on arrays.
• Fourier Transform
• Linear Algebra Operations
• Random Number generations
Crate an array
• Array is the ordered collection of basic data types of given length
• Syntax: numpy.array(object)
Array
• Numpy can handle different categorical entries
Generate Array using linspace
• Return equaly spaced numbers within the given range.
• numpy.linspace(start, stop, num, dtype, retstep, dtype)
• Start : start of interval range
• Stop: The end value of the interval range
• Num: number of samples to be generated
• dtype: type of output array
• restep: return the samples, step value
Generate Array using linspace
• Generate an array b with start value 1 and stop value 5.
Generate array using np.arange
• np.arange returns equally spaced numbers within the given range
based on step size.
• Syntax: np.arange(start, stop, stepsize)
• Start : start of interval range
• Stop: The end value of the interval range
• Stepsize: stepsize in the interval
Generate array using np.arange
• Generate an array with start value = 1, stop value = 10 and stepsize =
2
Generate array using ones
• Numpy.ones returns an array of given shape and type filled with ones.
• Syntax: numpy.ones(shape, dtype)
• Shape: integer or sequence of integers
• Dtype: data type (default: float)
Generate array using zeros
• Numpy.zeros returns an array of given shape and type filled with
zeros.
• Syntax: numpy.zeros(shape, dtype)
• Shape: integer or sequence of integers
• Dtype: data type (default: float)
Generate array using random.rand()
• Numpy.random.rand() returns an array of given shape filled with
random values.
• Syntax: numpy.random.rand(shape)
Advantages of numpy
• Numpy supports vectorized operations.
• Array operations are carried out in c and hence universal functions in
numpy are faster than operations carried out in Python Lists.
Advanages of numpy: Consumes less storage
space
Reshape an array
• Recast an array to new shape without changing its data
Array Dimensions
• Shape Returns the dimensions of an array
Numpy addition
• Numpy.add() returns the element wise addition between two arrays.
Numpy Multiplication
• Numpy.multiply(): returns element wise multiplication between two
arrays.
Other numpy functions
Accessing Components of an array