0% found this document useful (0 votes)
9 views26 pages

PyDays Day-2 - Final

Uploaded by

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

PyDays Day-2 - Final

Uploaded by

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

PyDays Day

NUMP
-2

Y
An initiative by Learning
Nexus
What is
Numpy?
• Numpy is a python library that allows numerical
computing
• Adds support to multi dimensional arrays
• Has high level math functions
• Efficient Array Structure
What makes • Fixed sizes and types
Numpy better • Contiguous memory
than stock allocation
python? • Multithreaded
Efficient Array Structure and fixed
types
• Lists in python store “objects” instead of the actual element
• Each variable stores multiple pieces of information related to the
data.
32 bit system (in 64 bit system (in
Parameters Data type
bytes) bytes)

size int 4 4

Reference Count long 4 8

Object Type long 4 8

Object Value long 4 8

• Total size : 28 Bytes (in 64 Bit system for small integer value)

An Object in Python is like a container that holds data and


functions that work with that data. Think of it as a real-world
item that has certain characteristics and behaviors. (OOPs
concept)
Efficient Array Structure and fixed
types
• Numpy allows Python developers access to “arrays”
• Arrays are like lists but they are of fixed size in memory and can hold only one type
of data unlike lists
• Default size of int in Numpy is int32
• While declaration, you can declare to use another int sizes such as int8, int16,
int64 etc (cannot be altered later)
Contigious Memory Location
• Array memory is allocated contiguously, reducing the number of
memory look up’s

...

• Use of Advanced processing algorithms such as SIMD Vector


Processing
• Efficient Cache Utilization
Multithreaded
• Since NumPy is primarily written in C, it can execute operations outside the
constraints of the Python Global Interpreter Lock (GIL), enabling the use of multiple
CPU cores for improved performance

• Numpy uses libraries OpenBLAS (Basic linear algebra subprograms) and


LAPACK(Linear algebra package) for efficient low level implementation of standard
linear algebra algorithms.
Application
s of Numpy •

Data analysis
Machine learning
• Image processing
• Replacement for MATLAB
• Base for many other python
modules (like Pandas)
Creating an import numpy as np
Array using #One dimensional array

numpy a = np.array([1,2,3])
Creating an import numpy as np
#One dimensional array

Array using a = np.array([1,2,3])


#Two dimensional array

numpy b = np.array([[1,2,3],[4,5,6]])
import numpy as np
#One dimensional array

Creating an
a = np.array([1,2,3])
#Two dimensional array

Array using b = np.array([[1,2,3],[4,5,6]])


#Three dimensional Array

numpy c = np.array([[[01, 02, 03],


[04, 05, 06],[07, 08, 09]],
[[10, 11,12],[13, 14,15],
[16, 17,18]]])
• .array([array values], dtype = int16) ->
creates an array of int16 datatype

• .ndim -> Returns the dimension of the matrix

Basic • .shape -> returns the order of the matrix


attributes in • .size -> returns the number of elements
Numpy • .itemsize -> returns the size of each element
in memory

• .nbytes -> returns the size of matrix in


memory
• np.zeros(dimension) - > creates an array
with all values as 0 and of specified
dimensions, the dimensions can be a
tuple or a list

• np.ones(dimensions) -> creates an array

Basic with all values as 1

functions in
• np.full(dimension, value, dtype) ->
create an array of all values as specified

Numpy • np.full_like(already existing array,


values)

• np.random.randint(low, high, size, dtype)

• np.min(matrix)

• np.max(matrix)
• A.reshape(order of the new matrix)

The product of dimensions of the new

More matrix should be equal to the product of


the old matrix

functions in • Vertically Stacking Matrices


np.vstack([v1,v2,v3…])

Numpy • Horizontally Stacking Matrices


np.hstack([v1,v2,v3…])
Scalar Arithmetic of
Matrix • Performs the operation on
Matrix A : [[ each and every element in the
matrix
[1,2,3],
A +
[4,5,6],
[7,8,9]], 2
[[10,11,12], A -2
[13,14,15], A *
[16,17,18]] 2
A /2
A **
2
Linear Algebra in Numpy
• np.dot(A,B) -> returns the dot product of two matrices
• np.linalg.matmul(A,B) -> matrix product of two matrices
• np.linalg.det(A) -> returns the determinant of the matrix
• np.linalg.vecdot(A,B) -> returns the vector dot product of two
matrices
• np.linalg.tensordot(A,B) -> returns the tensor dot product of two
matrices
• np.linalg.eig(A) -> returns the eigenvalues and right eigenvectors
of the matrix
• np.linalg.norm(A) -> return the normal of the matrix
• np.linalg.trace(a) -> returns the trace of the matrix
PyDays Day

Panda
-2

s
An initiative by Learning
Nexus
What is Pandas?
Introduction :
Pandas is an open-source data analysis and
manipulation library for Python. It provides powerful,
flexible data structures that make it easy to work with
structured data, such as tables and time series. The
library is built on top of NumPy, enabling efficient
operations on large datasets.
Data Structures
Series:
A one-dimensional labeled array that can hold any data type
(similar to a list or a column in a spreadsheet).

DataFrame:
A two-dimensional labeled data structure with columns of
potentially different types (similar to a table or a
spreadsheet).
#Creating a Data Frame
import pandas and pd

Working Data = {
‘Name’ : [“Alan”, “Ian”, “Hammond”],
‘Age’ : [30, 39, 65], ‘Job’ :
with Pandas [“Paleontologist”, “Mathematician”,
“Entrepreneur”]
}
dataf = pd.DataFrame(Data)
Accessing data
• .head(number of rows) : returns the top n number
of rows
• .tail(number of rows) : returns the bottom n
number of rows
• .columns : returns the name of headers
• array[‘name of column’] : returns the data in
that column
• array.iloc[row_index, column_index]
• array.loc[array[‘column name’] == ‘value’]
More ways to use .loc() function

• You can give multiple conditions using logical operators


array.loc[(condition 1) | (condition 2) & (condition
3) …]

• You can change the values in a column


array.loc[a[‘column name’] = value , columnname] =
new_value
Sorting Data
• array.sort_values([‘column_name’],
ascending = 1)
Supports QuickSort, MergeSort,
HeapSort
Adding new Column
• When new columns is related to already
existing
• Naive ones
method :
array[‘newColumn’] = array[‘column 1’] + array[‘column 2’] +
array[‘column 3’]
• Optimal method :
array[‘newColumn’] = arra.iloc[:, start_index : end_index + 1].sum(axis =
• 1)
Adding columns to a desired position
cols = list(a.columns)
array = array[cols[0:index] + [ cols[ len(cols) - 1 ] ] + cols[index :
len(cols)-2]]

• Deleting a column
array.drop(columns =
[‘name_of_column’])
Working with CSV
• Reading CSV

• Entire file : array = pandas.read_csv(“file_address\file_name”)


• Limited amount at a time : array =
pandas.read_csv(“file address\file_name”, chunksize = number
of rows)

• Writing CSV
• array.to_csv =(“saving_location\
file_name”)
Thank you
for Joining!
@tnn_vitap

You might also like