0% found this document useful (0 votes)
10 views3 pages

PYTHON UNIT-5 Part-B

NumPy is a Python library for data analysis and scientific computing that provides a multidimensional array object called ndarray, along with various functions for creating and manipulating these arrays. It highlights the differences between Python lists and NumPy arrays, such as data type uniformity and memory efficiency, and includes methods for creating, reshaping, and performing arithmetic operations on arrays. Additionally, it covers attributes and functions for array manipulation, including indexing, slicing, and statistical operations.

Uploaded by

diagnosant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

PYTHON UNIT-5 Part-B

NumPy is a Python library for data analysis and scientific computing that provides a multidimensional array object called ndarray, along with various functions for creating and manipulating these arrays. It highlights the differences between Python lists and NumPy arrays, such as data type uniformity and memory efficiency, and includes methods for creating, reshaping, and performing arithmetic operations on arrays. Additionally, it covers attributes and functions for array manipulation, including indexing, slicing, and statistical operations.

Uploaded by

diagnosant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

NumPy: NumPy stands for ‘Numerical Python’.

It is a package for data analysis and scientific


computing with Python. NumPy uses a multidimensional array object, and has functions and
tools for working with these arrays.
NumPy arrays are used to store lists of numerical data, vectors and matrices. The NumPy library
has a large set of built-in functions for creating, manipulating, and transforming NumPy arrays.
The NumPy array is officially called ndarray.
Difference Between List and Array:
List Array
List can have elements of different data types All elements of an array are of same data type
for example, [1, 3.4, ‘hello’, ‘a@’] for example, an array of floats may be:
[1.2, 5.4, 2.7, 6.5]
Elements of a list are not stored contiguously Array elements are stored in contiguous
in memory. memory locations. This makes operations on
arrays faster than lists.
Lists do not support element wise operations, Arrays support element wise operations. For
for example, addition, multiplication, etc. example, if A1 is an array, it is possible to say
because elements may not be of same type A1/3 to divide each element of the array by 3.
Python store the type information for every NumPy array takes up less space in memory as
element along with its element value. Thus compared to a list because arrays do not
lists take more space in memory and are less require to store datatype of each element
efficient. separately.
List is a part of core Python. Array (ndarray) is a part of NumPy library.
Creating array using numpy: To create an array and to use its methods, first we need
to import the NumPy library.
import numpy as np
a1 = np.array([10,20,30])
a4 = np.array( [ [1,2], [3,4] ], dtype=float)
>>> a1 # array([10, 20, 30])
>>>a4 #array( [[1., 2.], output on interactive mode
[3., 4.]])
We can create a two dimensional (2-D) arrays by passing nested lists to the array() function
a2 = np.array([[2.4,3], [4.91,7],[0,-1]])
Attributes & Functions of NumPy Array:
 ndarray.ndim: gives the number of dimensions of the array as an integer value. Arrays
can be 1-D, 2-D or n-D. we shall focus on 1-D and 2-D arrays only. NumPy calls the
dimensions as axes (plural of axis). Thus, a 2-D array has two axes. The row-axis is called
axis-0 and the column-axis is called axis-1. The number of axes is also called the array’s
rank e.g. >>> a1.ndim #1
 ndarray.shape: It gives the sequence of integers indicating the size of the array for each
dimension. e.g. >>> a1.shape # (3,)
a3 = np.array([[2.4,3], [4.91,7],[0,-1]])
>>> a3.shape # (3, 2) means row 3 and column 2
 ndarray.size: It gives the total number of elements of the array. This is equal to the
product of the elements of shape. e.g. >>> a1.sze # 3
 ndarray.dtype: It is the data type of the elements of the array. All the elements of an
array are of same data type. Common data types are int32, int64, float32, float64, U32,
etc. e.g. >>> a1.dtype # dtype('int32') OR int32 on print(a1.dtype)
 ndarray.itemsize: It specifies
ecifies the size in bytes of each element of the array.
e.g. >>> a3.itemsize # 8
 np.arange(start,stop,step): This function is used to create an array with a range of
values. e.g. arr = np.arange(1, 6) >>>arr #array(1,2,3,4,5)
print(arr) #[1 2 3 4 5] output on script mode
 np.empty(): empty() function can be used to create empty array or an unintialized
(garbage value) array of specified shape and dtype
syntax: numpy.empty(Shape,[dtype=<datatype>,] [ order = ‘C’ or ‘F’]
e.g. arr = np.empty([2,3],dtype=np.int64, order='C')
 np.reshape(row,column): this function can create another array by changing order of
array e.g.
import numpy as np
arr = np.arange(10)
print(arr)
a=arr.reshape(5,2)
print(a)
 np.zeros(): This function is used to create an array filled with zeros or initialized by 0.
Default type will be float. e.g. arr = np.zeros(3
np.zeros(3) >>>arr #array(0.,0.,0.)
arr=np.zeros(
arr=np.zeros((2,3)) >>>arr #array([[0.,0.,0.],[0.,0.,0.]])
arr=np.zeros((2
arr=np.zeros((2, 3), dtype=float)
 np.ones(): This function is used to create an array filled with ones or initialized by 1. 1
Default type will be float. e.g. arr = np.one
np.ones(3) >>>arr #array(1.,1.,1.)
arr=np.ones()(2,3) >>>arr #array([[1.,1.,1.],[1.,1.,1
,[1.,1.,1.]])
 Indexing: NumPy arrays can be indexed in form array_name[i][j] OR array_name[I,j]
e.g. >>>a3[0][1] #3.0
 Slicing: Array can be sliced as array_name[start:stop:step] e.g.
>>> arr = np.array([[ -7, 7, 0, 10, 20], [ -5, 1, 40, 200], [ -1, 1, 4, 30]])
# access all the elements in the 3rd column
>>> arr[0:3,2] # array([10,
([10, 40, 4])
# access elements ts of 2nd and 3rd row from 1st and 2nd column
>>> arr[1:3,0:2] #array([[-5, 5, 1], [-1, 1]])
If row indices are not specified, it means al all the rows are to be considered
>>>arr[:,2] #array([10,
array([10, 40, 4])
 Arithmetic operations on arrays arrays:
i) Addition <ndarray1> + <n>|<ndarray2>
ii) Subtraction <ndarray1> - <n>|<ndarray2>
iii) Multiply <ndarray1> * <n>|<ndarray2>
iv) Divide <ndarray1> / <n>|<ndarray2>
v) Mod <ndarray1> % <n>|<ndarray2>
 Arithmetic Functions on arrays:
i) numpy.add(<ndarray1>,<n>|<ndarray2>)
ii) numpy.subtract(<ndarray1>,<n>|<ndarray2>)
iii) numpy.multiply(<ndarray1>,<n>|<ndarray2>)
iv) numpy.divide(<ndarray1>,<n>|<ndarray2>)
v) numpy.mod(<ndarray1>,<n>|<ndarray2>)
vi) numpy.remainder(<ndarray1>,<n>|<ndarray2>)
 other functions on arrays:
i) sort(axis=0|1): It sort elements in ascending order 0 means sort column wise and
1 means sort row wise e.g.
import numpy as np
a = np.array([8,2,9,4,1])
a.sort() >>>a # array([1, 2, 4, 8, 9])
ii) np.linspace(start,stop,n): returns n element of array of evenly spaced
values within the specified interval. e.g. a=np.linspace(1,5,10)
iii) np.identity(n, dtype ): Return a identity matrix(nxn) i.e. a square matrix with ones
on the main diagonal. e.g. a=np.identity(3) >>>a
# array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
iv) transpose(): generate transpose of matrix e.g a = np.array([[1,2,3],[4,5,6]])
>>>a.transpose()
v) max(), min(), mean(), std(): finds the maximum, minimum, mean and standard
deviation of element from an array. e.g.
A = np.array([1,0,2,-3,6,8,4,7]) B = np.array([[3,6],[4,2]])
max element form the whole 1-D array >>> A.max() # 8
max element form the whole 2-D array >>> B.max() # 6
if axis=1, it gives column wise maximum >>> B.max(axis=1) #array([6, 4])
if axis=0, it gives row wise maximum >>> B.max(axis=0) #array([4, 6])
vi) np.random.randint(start,stop,(shape)): generates random number staring from
start to stop-1 with optional shape(order). e.g. A = np.random.randint(1,7,(2,3))

You might also like