0% found this document useful (0 votes)
25 views

Python Day 7

NumPy is a Python library that provides support for large, multi-dimensional arrays and matrices for scientific computing. It contains N-dimensional array objects, array slicing and reshaping methods, and numerical routines for linear algebra, Fourier transforms, and random number generation. NumPy arrays are similar to lists but more powerful and efficient for numerical data. NumPy's main object is the ndarray, which represents multi-dimensional arrays indexed by tuples of positive integers.

Uploaded by

Sagar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Python Day 7

NumPy is a Python library that provides support for large, multi-dimensional arrays and matrices for scientific computing. It contains N-dimensional array objects, array slicing and reshaping methods, and numerical routines for linear algebra, Fourier transforms, and random number generation. NumPy arrays are similar to lists but more powerful and efficient for numerical data. NumPy's main object is the ndarray, which represents multi-dimensional arrays indexed by tuples of positive integers.

Uploaded by

Sagar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Numpy

What is NumPy

NumPy is an extension to the Python programming language, adding support for large,
multi-dimensional arrays and matrix's, along with a large library of high-level
mathematical functions to operate on these arrays.

 NumPy is the fundamental library needed for scientific computing with Python.
 This contains:
 N-dimensional array object
 Array slicing methods
 Array reshaping methods
 Numerical routines in numpy:
 Linear algebra functions
 Fourier transform
 Random number capabilities
Why NumPy

 Arithmetic Operation can not applied directly on lists


Example:

 Hence we need efficient arrays with arithmetic and better multidimensional tools
 NumPy package provide arrays which are similar to lists, but much more
capable, except fixed size
NumPy - ndarray

 NumPy’s main object is ndarray (homogeneous multidimensional array).

 It is a table of elements (usually numbers), all of the same type, indexed by a


tuple of positive integers.
 Dimensions  usually called axes.
 Rank  number of axes.

 Examples of multidimensional arrays include vectors, matrices, images and


spreadsheets

[9, 1, -1]  An array of rank 1 i.e. A matrix with 1 row and columns

[ [ 10, 0.21, -30],  An array of rank 2 ( A matrix with 2 rows and 3 columns)
[ 1.9, 7.4, 1.9] ]
Numpy – ndarray Attributes
ndarray.ndim
 Gives dimensiona (ran of axes) of the array
ndarray.shape
 For a matrix with n rows and m columns, shape will be tuple (n,m). The length of the
shape tuple is therefore the rank, or number of dimensions, ndim.
ndarray.size
 the total number of elements of the array. This is equal to the product of the elements
of shape.
ndarray.dtype
 an object describing the type of the elements in the array. One can create or specify
dtype’s using standard Python types. Additionally NumPy provides types of its own.
numpy.int32, numpy.int16, and numpy.float64 are some examples.
ndarray.itemsize
 the size in bytes of each element of the array. For example, an array of elements of
type float64 has itemsize 8 (=64/8), while one of type complex32 has itemsize 4
(=32/8). It is equivalent to ndarray.dtype.itemsize.
ndarray.data
 the buffer containing the actual elements of the array. Normally, we won’t need to use
this attribute because we will access the elements in an array using indexing facilities.
Numpy - Array Creation

There are a couple of mechanisms for creating arrays in NumPy:

 A Python list or tuples

 Using functions that are dedicated to generating numpy arrays, such as arange,
ones, zeros linspace, etc.

 reading data from files


Numpy - Creating Array using list

In general, any numerical data that is stored in an array-like container can be converted
to an ndarray through use of the array() function. The most obvious examples are
sequence types like lists and tuples.

Creating 1-D Array Creating 2-D Array


Numpy - Creating Array using built-in function

There are a couple of built-in NumPy functions which will create arrays from scratch.

• zeros(shape) -- creates an array filled with 0 values with the specified shape. The
default dtype is float64.

• ones(shape) -- creates an array filled with 1 values.

• arange() -- creates arrays with regularly incrementing values.

• linspace() -- creates arrays with a specified number of elements, and spaced equally
between the specified beginning and end values.

• eye(): Return a 2-D array with ones on the diagonal and zeros elsewhere.
Numpy Methods

 sort()
 argsort()
 transpose()
 invert()
 dot()
 var()
 std()
THANK YOU!!

visit : www.rcplindia.in [email protected]

You might also like