[go: up one dir, main page]

0% found this document useful (0 votes)
8 views21 pages

Unit 2

NumPy is a Python library for working with arrays, offering functionalities for linear algebra, Fourier transforms, and matrices, and is significantly faster than traditional Python lists. It provides an array object called ndarray, which can be created from various array-like objects and supports multiple dimensions. The document also covers installation, data types, array operations, and mathematical functions available in NumPy.

Uploaded by

disecek477
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)
8 views21 pages

Unit 2

NumPy is a Python library for working with arrays, offering functionalities for linear algebra, Fourier transforms, and matrices, and is significantly faster than traditional Python lists. It provides an array object called ndarray, which can be created from various array-like objects and supports multiple dimensions. The document also covers installation, data types, array operations, and mathematical functions available in NumPy.

Uploaded by

disecek477
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/ 21

UNIT-2 NUMPY

What is NumPy?
NumPy is a Python library used for working with arrays.

It also has functions for working in domain of linear algebra, fourier transform,
and matrices.

NumPy was created in 2005 by Travis Oliphant. It is an open source project and
you can use it freely.

NumPy stands for Numerical Python.

Why Use NumPy?


In Python we have lists that serve the purpose of arrays, but they are slow to
process.

NumPy aims to provide an array object that is up to 50x faster than traditional
Python lists.

The array object in NumPy is called ndarray, it provides a lot of supporting


functions that make working with ndarray very easy.

Arrays are very frequently used in data science, where speed and resources are
very important.

Why is NumPy Faster Than Lists?


NumPy arrays are stored at one continuous place in memory unlike lists, so
processes can access and manipulate them very efficiently.

This behavior is called locality of reference in computer science.

This is the main reason why NumPy is faster than lists. Also it is optimized to
work with latest CPU architectures.
UNIT-2 NUMPY

Which Language is NumPy written in?


NumPy is a Python library and is written partially in Python, but most of the
parts that require fast computation are written in C or C++.

Installation of NumPy
If you have Python and PIP already installed on a system, then installation of
NumPy is very easy.

Install it using this command:

C:\Users\Your Name>pip install numpy

Import NumPy
Once NumPy is installed, import it in your applications by adding
the import keyword:

import numpy

Checking NumPy Version


The version string is stored under __version__ attribute.

Create a NumPy ndarray Object


NumPy is used to work with arrays. The array object in NumPy is
called ndarray.

We can create a NumPy ndarray object by using the array() function.

To create an ndarray, we can pass a list, tuple or any array-like object into
the array() method, and it will be converted into an ndarray:

Dimensions in Arrays
A dimension in arrays is one level of array depth (nested arrays).
UNIT-2 NUMPY

0-D Arrays
0-D arrays, or Scalars, are the elements in an array. Each value in an array is a
0-D array.

1-D Arrays
An array that has 0-D arrays as its elements is called uni-dimensional or 1-D
array.

2-D Arrays
An array that has 1-D arrays as its elements is called a 2-D array.

These are often used to represent matrix or 2nd order tensors.

3-D arrays
An array that has 2-D arrays (matrices) as its elements is called 3-D array.

These are often used to represent a 3rd order tensor.

Higher Dimensional Arrays


An array can have any number of dimensions.

When the array is created, you can define the number of dimensions by using
the ndmin argument.

Access Array Elements


Array indexing is the same as accessing an array element.

You can access an array element by referring to its index number.

The indexes in NumPy arrays start with 0, meaning that the first element has
index 0, and the second has index 1 etc.
UNIT-2 NUMPY

Access 2-D Arrays


To access elements from 2-D arrays we can use comma separated integers
representing the dimension and the index of the element.

Think of 2-D arrays like a table with rows and columns, where the dimension
represents the row and the index represents the column.

Access 3-D Arrays


To access elements from 3-D arrays we can use comma separated integers
representing the dimensions and the index of the element.

Slicing arrays
Slicing in python means taking elements from one given index to another given
index.

We pass slice instead of index like this: [start:end].

We can also define the step, like this: [start:end:step].

If we don't pass start its considered 0

If we don't pass end its considered length of array in that dimension

If we don't pass step its considered 1

STEP
Use the step value to determine the step of the slicing:

Data Types in Python


By default Python have these data types:

 strings - used to represent text data, the text is given under quote
marks. e.g. "ABCD"
 integer - used to represent integer numbers. e.g. -1, -2, -3
UNIT-2 NUMPY

 float - used to represent real numbers. e.g. 1.2, 42.42


 boolean - used to represent True or False.
 complex - used to represent complex numbers. e.g. 1.0 + 2.0j, 1.5 + 2.5j

Data Types in NumPy


NumPy has some extra data types, and refer to data types with one character,
like i for integers, u for unsigned integers etc.

Below is a list of all data types in NumPy and the characters used to represent
them.

 i - integer
 b - boolean
 u - unsigned integer
 f - float
 c - complex float
 m - timedelta
 M - datetime
 O - object
 S - string
 U - unicode string
 V - fixed chunk of memory for other type ( void )

Checking the Data Type of an Array


The NumPy array object has a property called dtype that returns the data type
of the array:

Creating Arrays With a Defined Data Type


We use the array() function to create arrays, this function can take an optional
argument: dtype that allows us to define the expected data type of the array
elements:
UNIT-2 NUMPY

Converting Data Type on Existing Arrays


The best way to change the data type of an existing array, is to make a copy of
the array with the astype() method.

The astype() function creates a copy of the array, and allows you to specify the
data type as a parameter.

The data type can be specified using a string, like 'f' for float, 'i' for integer
etc. or you can use the data type directly like float for float and int for
integer.

The Difference Between Copy and View


The main difference between a copy and a view of an array is that the copy is a
new array, and the view is just a view of the original array.

The copy owns the data and any changes made to the copy will not affect
original array, and any changes made to the original array will not affect the
copy.

The view does not own the data and any changes made to the view will affect
the original array, and any changes made to the original array will affect the
view.

COPY:
Example
import numpy as np

arr = np.array([1, 2, 3, 4, 5])


x = arr.copy()
arr[0] = 42

print(arr)
print(x)

VIEW:
UNIT-2 NUMPY

Example
import numpy as np

arr = np.array([1, 2, 3, 4, 5])


x = arr.view()
arr[0] = 42

print(arr)
print(x)

Check if Array Owns its Data


As mentioned above, copies owns the data, and views does not own the data,
but how can we check this?

Every NumPy array has the attribute base that returns None if the array owns the
data.

Otherwise, the base attribute refers to the original object.

Shape of an Array
The shape of an array is the number of elements in each dimension.

Get the Shape of an Array


NumPy arrays have an attribute called shape that returns a tuple with each
index having the number of corresponding elements.

What does the shape tuple represent?


Integers at every index tells about the number of elements the corresponding
dimension has.

Reshaping arrays
UNIT-2 NUMPY

Reshaping means changing the shape of an array.

The shape of an array is the number of elements in each dimension.

By reshaping we can add or remove dimensions or change number of elements


in each dimension.

Unknown Dimension
You are allowed to have one "unknown" dimension.

Meaning that you do not have to specify an exact number for one of the
dimensions in the reshape method.

Pass -1 as the value, and NumPy will calculate this number for you.

Flattening the arrays


Flattening array means converting a multidimensional array into a 1D array.

We can use reshape(-1) to do this.

Iterating Arrays
Iterating means going through elements one by one.

As we deal with multi-dimensional arrays in numpy, we can do this using


basic for loop of python.

If we iterate on a 1-D array it will go through each element one by one.

Joining NumPy Arrays


Joining means putting contents of two or more arrays in a single array.

In SQL we join tables based on a key, whereas in NumPy we join arrays by


axes.
UNIT-2 NUMPY

We pass a sequence of arrays that we want to join to


the concatenate() function, along with the axis. If axis is not explicitly passed,
it is taken as 0.

Joining Arrays Using Stack Functions


Stacking is same as concatenation, the only difference is that stacking is done
along a new axis.

We can concatenate two 1-D arrays along the second axis which would result in
putting them one over the other, ie. stacking.

We pass a sequence of arrays that we want to join to the stack() method along
with the axis. If axis is not explicitly passed it is taken as 0.

Stacking Along Rows


NumPy provides a helper function: hstack() to stack along rows.

Stacking Along Columns


NumPy provides a helper function: vstack() to stack along columns.

Stacking Along Height (depth)


NumPy provides a helper function: dstack() to stack along height, which is the
same as depth.

Splitting NumPy Arrays


Splitting is reverse operation of Joining.

Joining merges multiple arrays into one and Splitting breaks one array into
multiple.

We use array_split() for splitting arrays, we pass it the array we want to split
and the number of splits.
UNIT-2 NUMPY

Split Into Arrays


The return value of the array_split() method is an array containing each of
the split as an array.

If you split an array into 3 arrays, you can access them from the result just like
any array element:

Splitting 2-D Arrays


Use the same syntax when splitting 2-D arrays.

Use the array_split() method, pass in the array you want to split and the
number of splits you want to do.

Searching Arrays
You can search an array for a certain value, and return the indexes that get a
match.

To search an array, use the where() method.

Search Sorted
There is a method called searchsorted() which performs a binary search in the
array, and returns the index where the specified value would be inserted to
maintain the search order.

Search From the Right Side


By default the left most index is returned, but we can give side='right' to
return the right most index instead.

Sorting Arrays
Sorting means putting elements in an ordered sequence.
UNIT-2 NUMPY

Ordered sequence is any sequence that has an order corresponding to


elements, like numeric or alphabetical, ascending or descending.

The NumPy ndarray object has a function called sort(), that will sort a specified
array.

NumPy String Functions


It is used to concatenate
1 add() the corresponding array
elements (strings).

It returns the multiple


copies of the specified
string, i.e., if a string 'hello'
2 multiply()
is multiplied by 3 then, a
string 'hello hello' is
returned.

It returns the copy of the


string where the original
string is centered with the
3 center()
left and right padding
filled with the specified
number of fill characters.

It returns a copy of the


original string in which the
4 capitalize() first letter of the original
string is converted to the
Upper Case.

It returns the title cased


version of the string, i.e.,
the first letter of each
5 title()
word of the string is
converted into the upper
case.
UNIT-2 NUMPY

It returns a copy of the


string in which all the
6 lower()
letters are converted into
the lower case.

It returns a copy of the


string in which all the
7 upper()
letters are converted into
the upper case.

It returns a list of words in


9 split()
the string.

It returns the list of lines in


9 splitlines() the string, breaking at line
boundaries.

Returns a copy of the


string with the leading and
10 strip()
trailing white spaces
removed.

It returns a string which is


the concatenation of all
11 join()
the strings specified in the
given sequence.

It returns a copy of the


string by replacing all
12 replace() occurrences of a particular
substring with the
specified one.

13 decode() It is used to decode the


specified string element-
UNIT-2 NUMPY

wise using the specified


codec.

It is used to encode the


14 encode() decoded string element-
wise.

NumPy Bitwise Operators


It is used to calculate
the bitwise and
1 bitwise_and operation between the
corresponding array
elements.

It is used to calculate
the bitwise or
2 bitwise_or operation between the
corresponding array
elements.

It is used to calculate
the bitwise not the
3 invert
operation of the array
elements.

It is used to shift the


bits of the binary
4 left_shift
representation of the
elements to the left.

It is used to shift the


bits of the binary
5 right_shift
representation of the
elements to the right.

bitwise_and Operation
UNIT-2 NUMPY

The NumPy provides the bitwise_and() function which is used to calculate the bitwise_and
operation of the two operands.

The bitwise and operation is performed on the corresponding bits of the binary representation
of the operands. If both the corresponding bit in the operands is set to 1, then only the
resultant bit in the AND result will be set to 1 otherwise it will be set to 0.

bitwise_or Operator
The NumPy provides the bitwise_or() function which is used to calculate the bitwise or
operation of the two operands.

The bitwise or operation is performed on the corresponding bits of the binary representation
of the operands. If one of the corresponding bit in the operands is set to 1 then the resultant
bit in the OR result will be set to 1; otherwise it will be set to 0.

Invert operation
It is used to calculate the bitwise not the operation of the given operand. The 2's complement
is returned if the signed integer is passed in the function.

It shifts the bits in the binary representation of the operand to the left by the specified
position. An equal number of 0s are appended from the right.

Right Shift Operation


It shifts the bits in the binary representation of the operand to the right by the specified
position. An equal number of 0s are appended from the left.

NumPy Mathematical Functions


Numpy contains a large number of mathematical functions which can be used to perform
various mathematical operations. The mathematical functions include trigonometric functions,
arithmetic functions, and functions for handling complex numbers. Let's discuss the
mathematical functions.

Trigonometric functions
Numpy contains the trigonometric functions which are used to calculate the sine, cosine, and
tangent of the different angles in radian.

The sin, cos, and tan functions return the trigonometric ratio for the specified angles.
UNIT-2 NUMPY

On the other hand, arcsin(), arccos(), and arctan() functions return the trigonometric inverse
of the specified angles.

The numpy.degrees() function can be used to verify the result of these trigonometric
functions.

Rounding Functions
The numpy provides various functions that can be used to truncate the value of a decimal
float number rounded to a particular precision of decimal numbers. Let's discuss the rounding
functions.

The numpy.around() function


This function returns a decimal value rounded to a desired position of the decimal. The syntax
of the function is given below.

numpy.around(num, decimals)

Parameter List
1 num It is the input number.

It is the number of
decimals which to
which the number is to
be rounded. The
2 decimals
default value is 0. If
this value is negative,
then the decimal will
be moved to the left.

The numpy.floor() function


This function is used to return the floor value of the input data which is the largest integer not
greater than the input value.

The numpy.ceil() function


This function is used to return the ceiling value of the array values which is the smallest
integer value greater than the array element.
UNIT-2 NUMPY

Numpy statistical functions


Numpy provides various statistical functions which are used to perform some statistical data
analysis. In this section of the tutorial, we will discuss the statistical functions provided by the
numpy.

Finding the minimum and maximum elements from the array


The numpy.amin() and numpy.amax() functions are used to find the minimum and maximum
of the array elements along the specified axis respectively.

numpy.ptp() function
The name of the function numpy.ptp() is derived from the name peak-to-peak. It is used to
return the range of values along an axis.

numpy.percentile() function
The syntax to use the function is given below.

numpy.percentile(input, q, axis)

It accepts the following parameters.

1. input: It is the input array.


2. q: It is the percentile (1-100) which is calculated of the array element.
3. axis: It is the axis along which the percentile is to be calculated.

Calculating median, mean, and average of array items


The numpy.median() function:
Median is defined as the value that is used to separate the higher range of data sample with
a lower range of data sample. The function numpy.median() is used to calculate the median
of the multi-dimensional or one-dimensional arrays.

The numpy.mean() function:


The mean can be calculated by adding all the items of the arrays dividing by the number of
array elements. We can also mention the axis along which the mean can be calculated.

The numpy.average() function:


UNIT-2 NUMPY

The numpy.average() function is used to find the weighted average along the axis of the multi-
dimensional arrays where their weights are given in another array.

NumPy Matrix Library


NumPy contains a matrix library, i.e. numpy.matlib which is used to configure matrices instead
of ndarray objects.

numpy.matlib.empty() function
This function is used to return a new matrix with the uninitialized entries. The syntax to use
this function is given below.

1. numpy.matlib.empty(shape, dtype, order)


It accepts the following parameter.

1. shape: It is the tuple defining the shape of the matrix.


2. dtype: It is the data type of the matrix.
3. order: It is the insertion order of the matrix, i.e. C or F.

numpy.matlib.zeros() function
This function is used to create the matrix where the entries are initialized to zero.

numpy.matlib.ones() function
This function returns a matrix with all the elements initialized to 1.

numpy.matlib.eye() function
This function returns a matrix with the diagonal elements initialized to 1 and zero elsewhere.
The syntax to use this function is given below.

1. numpy.matlib.eye(n, m, k, dtype)
It accepts the following parameters.

1. n: It represents the number of rows in the resulting matrix.


2. m: It represents the number of columns, defaults to n.
3. k: It is the index of diagonal.
4. dtype: It is the data type of the output

numpy.matlib.identity() function
This function is used to return an identity matrix of the given size. An identity matrix is the one
with diagonal elements initializes to 1 and all other elements to zero.

numpy.matlib.rand() function
This function is used to generate a matrix where all the entries are initialized with random
values.
UNIT-2 NUMPY

NumPy Linear Algebra


Numpy provides the following functions to perform the different algebraic calculations on the
input data.

It is used to calculate
1 dot() the dot product of two
arrays.

It is used to calculate
2 vdot() the dot product of two
vectors.

It is used to calculate
3 inner() the inner product of
two arrays.

It is used to calculate
the matrix
4 matmul()
multiplication of two
arrays.

It is used to calculate
5 det() the determinant of a
matrix.

It is used to solve the


6 solve()
linear matrix equation.

It is used to calculate
7 inv() the multiplicative
inverse of the matrix.

numpy.dot() function
This function is used to return the dot product of the two matrices. It is similar to the matrix
multiplication.
UNIT-2 NUMPY

umpy.vdot() function
This function is used to calculate the dot product of two vectors. It can be defined as the sum
of the product of corresponding elements of multi-dimensional arrays.

numpy.inner() function
This function returns the sum of the product of inner elements of the one-dimensional array.
For n-dimensional arrays, it returns the sum of the product of elements over the last axis.

numpy.matmul() function
It is used to return the multiplication of the two matrices. It gives an error if the shape of both
matrices is not aligned for multiplication.

numpy determinant
The determinant of the matrix can be calculated using the diagonal elements. The
determinant of following 2 X 2 matrix

A B
C D

can be calculated as AD - BC.

The numpy.linalg.det() function is used to calculate the determinant of the matrix.

numpy.linalg.solve() function
This function is used to solve a quadratic equation where values can be given in the form of
the matrix.

numpy.linalg.inv() function
This function is used to calculate the multiplicative inverse of the input matrix.

NumPy Broadcasting
Broadcasting in NumPy refers to the ability of performing operations on arrays
with different shapes by automatically expanding the smaller array's shape to
match the larger array's shape. This is useful when performing arithmetic
operations or applying functions to arrays of different dimensions.
UNIT-2 NUMPY

When performing arithmetic operations, NumPy operates on corresponding


elements of the arrays. If the arrays have the same shape, operations are are
smoothly performed. However, if the arrays have different shapes, NumPy uses
broadcasting to align them, allowing element-wise operations to be conducted
easily.

Rules of Broadcasting
For broadcasting to work, the following rules must be satisfied −

 If arrays have a different number of dimensions, the shape of the smaller-


dimensional array is padded with ones on the left side until both shapes have the
same length.
 The size of each dimension must either be the same or one of them must be one.
 Broadcasting is applied from the last dimension to the first dimension.

For instance, consider two arrays with shapes (3, 4) and (4,). The broadcasting
rules will align the shapes as follows −

 Pad the smaller array's shape: The smaller array shape (4,) is padded to (1, 4).
 Align dimensions: The shapes (3, 4) and (1, 4) are aligned as (3, 4) and (3, 4)
respectively.
 Perform element-wise operation: The operation is applied to the aligned shapes.

Adding a Scalar to an Array


When adding a scalar to an array, NumPy uses broadcasting to apply the scalar
to each element of the array. Broadcasting expands the scalar to match the
shape of the array, enabling element-wise operations.

Adding Arrays of Different Shapes


When adding arrays of different shapes, NumPy applies broadcasting rules to
make their shapes compatible. Broadcasting works by stretching the smaller
array across the larger one, so that both arrays have the same shape for
element-wise addition.

This process eliminates the need for manually reshaping arrays before
performing operations.
UNIT-2 NUMPY

Broadcasting with Multi-Dimensional Arrays


When performing operations between multi-dimensional arrays with different
shapes, broadcasting rules align their dimensions so that they can be operated
on element-wise.

This process involves stretching the smaller array to match the shape of the
larger one, enabling operations to be performed smoothly.

Applying Functions with Broadcasting


Broadcasting not only simplifies arithmetic operations between arrays of
different shapes but also allows functions to be applied across arrays. These
functions can include −

 Mathematical Functions: Functions that perform mathematical operations, such as


addition, subtraction, multiplication, and division.
 Statistical Functions: Functions that compute statistical properties, like mean,
median, variance, and standard deviation.
 Reduction Functions: Functions that reduce the dimensions of an array by
performing operations such as sum, product, or maximum.
 Logical Operations: Functions that perform logical operations, such as comparisons
and logical operations (e.g., AND, OR, NOT).

When applying functions to arrays with different shapes, broadcasting ensures


that the function is applied element-wise.

You might also like