0% found this document useful (0 votes)
3 views6 pages

Introduction to NumPy

NumPy is a Python library designed for efficient array manipulation and operations in linear algebra, Fourier transforms, and matrices. It offers significant performance advantages over standard Python lists, with features like continuous memory storage, various data types, and functions for reshaping, joining, and filtering arrays. Additionally, NumPy supports advanced functionalities such as searching, sorting, and iterating through arrays, making it essential for data science applications.

Uploaded by

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

Introduction to NumPy

NumPy is a Python library designed for efficient array manipulation and operations in linear algebra, Fourier transforms, and matrices. It offers significant performance advantages over standard Python lists, with features like continuous memory storage, various data types, and functions for reshaping, joining, and filtering arrays. Additionally, NumPy supports advanced functionalities such as searching, sorting, and iterating through arrays, making it essential for data science applications.

Uploaded by

mohdamirak133
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Introduction to NumPy

 What is NumPy?
NumPy is a python library use for working with the arrays. It also has
functions for working in the domain of Linear algebra, Fourier Transform, and
matrices.

Note: NumPy stands for numerical python.

 Why use NumPy?


1. In Python we have lists instead of arrays but they are slow to process.
NumPy provides an array object that is 50times faster than normal
lists in the Python.
2. The array object is also called ndarray, it provides a lot of function
that makes working with ndarray easy.
3. Array are used in Data Science where the speed and resources are
very Important.
Note:
Continuous Stored: Assume an arr = [10, 20, 30, 40], these
elements are stored one by another like consecutively for
e.g.: for calculating the address of the arr[1] which is 20 then
it is calculated by
Address=starting element + (index* size of the array)
Address=1000(arr[0])+(1*4)=1004 which is the address
of the arr[1] hence proved they are saved one by another.
4. Unlike the lists array objects are stored in continuous place in the
memory. This make the processes to access and manipulate the data
easily, this behaviour is called locality reference in computer science.
 Language used to make NumPy?
NumPy is written partially in Python but the most parts where computation
speed requires to be faster is written in C++/C.
 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.
1. i - integer
2. b - boolean
3. u - unsigned integer
4. f - float
5. c - complex float
6. m - timedelta
7. M - datetime
8. O - object
9. S - string
10. U - unicode string
11. V - fixed chunk of memory for other type ( void ) using the dtype getting the type of
the variable in the array

 Copy and View


Copy: - A copy is a new array that is copied from the original array and any changes
made to the copy will not affect the original array.

View:- A view is a new array that looks at the same data of the original array and any
changes made to the view will affect the original array.

# TO CHECK OF THE DATA IS OWN BY THE COPY OR VIEW WITH THE PROOF
arr=[Link]([1,2,3,4,5])

x=[Link]()
y=[Link]()

print([Link]) # this will print None beacause x is a copy because it doesn't shares

___________________the dataa as array arr to x

 print([Link]) # this will print array as it shares the data as array arr to y

 Shape of Array:-
Tells you how many elements are there in the array in each dimension of the array
Eg. For the earlier example “[Link]()” .
 Reshaping of an Array:-
It literally means the changing the shape or dimensions of an array
e.g. changing a 1D array to 2D array using the reshape() keywords
for 1d to 2d
arr=[Link]([1,2,3,4,5,6,7,8,9,10,11,12])
newarr=[Link](4,3)
this converts the 1d to 2D into 4 arrays with 3 elements In each array
for 1d to 3d:-
arr=[Link]([1,2,3,4,5,6,7,8,9,10,11,12])
newarr=[Link](2,3,2)
this converts the 1D to 3D array with (2,3,2) 2blocks containing with 3 rows array and
each row array containing 2 elements.
Suppose if we don’t want to pass an argument in the {(2,3,2) of 12 elements in an
array of 1D} we can skip the any one

Note:- if we want to miss an argument in the above 1D to 3D u can use (-1) any of the
three arguments in “newarr=[Link](2,3,2)”

 Flattening the Arrays:-


It refers to the conversion of multidimensional array to 1D array .
e.g. newarr=[Link](-1)
Note: There are a lot of functions for changing the shapes of arrays in
numpy flatten, ravel and also for rearranging the elements rot90, flip, fliplr, flipud etc.
These fall under Intermediate to Advanced section of numpy.

Iterating Arrays:-
It means going through the elements one by one. As we deal in multiple dimensional
we can use :
1. For loop
2. [Link](array name) keyword
3. Enumerated iteration
Joining two arrays:-
Can be done by:
1. Using the [Link]((arr1,arr2),axis=(0 or 1 or 2)) function/method
2. Using the [Link]((arr1,arr2) ),axis=(0 or 1 or 2) function/method
Note:-
For stack arranging two arrays in rows
For stack arranging two arrays in columns

Splitting the arrays:-


Done using the array_split(array name to be splitted ,[Link] arrays to be splitted into)
keyword. Individual arrays that are output (of the array that’s splitted) can be printed
just by using index of the new array like this :- print(newarr[0])

e.g.
import numpy as np

arr = [Link]([1, 2, 3, 4, 5, 6])

newarr = np.array_split(arr, 3)

print(newarr[0])
print(newarr[1])
print(newarr[2])

Note:- For splitting the array along the column?


Use the hsplit()keyword which is the opposite of hstack()

Example:-
Ques-)For splitting the array along the column?
import numpy as np

arr = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15],
[16, 17, 18]])

newarr = np.array_split(arr, 3, axis=1)

print(newarr)

Searching Arrays:-
You can search the certain value of array and return the indexes that gets a match
and it is done by where(element to be search) keyword. Returns the indexes in the
form of an array.
e.g.
arr=[Link]([1,2,3,4,5,4,4])
x=[Link](arr==4)
print(x) # it prints the indexes where 4 is present in the array

Search Sorted Array:-


It actually means if I insert the number into a sorted array ,where should I put it so
that sorted array remain the sorted.
Easy Definition:- It performs a binary search in the array and returns the index for the
1st appearance of the element.

e.g.
import numpy as np

arr = [Link]([6, 7, 8, 9])

x = [Link](arr, 7)

print(x) output=1

Note:-
Always remember the definition of the searchsorted() :- It actually means if I insert
the number into a sorted array, where should I put it so that sorted array remains the
sorted.
Note:- We can also choose side from which we want to search the element
arr=[Link]([1,3,4,5,6,9])
x=[Link](arr,9,side='right') print(x) output=4 becoz 9 is the ending
element i.e. index =3 which is same as of the element we are searching for. Hence it
inserts the element 9 at the index 4 i.e. after the 4th element which situated at
index=3.

Note:- For searching for multiply values use the array to for the searching for
example :-
X=[Link](arr[2,4,6]) like this put the values to be searched in the array

Sorting the Array:-


Sorting just puts the elements in the sequential order, can done by using the sort()
keyword
Note:- This just returns the copy of the array after sorting the array, original array
remains unchanged.
Filtering Arrays:-

Getting some elements out of the existing array and creating a new array out of
them.
In NumPy, we can filter an array using a boolean index list.
A Boolean index list is a list of Booleans corresponding to indexes in the array.
If the index is True that element is contained in the filtered array and vice versa for
False.
e.g.
arr=[Link]([1,2,3,4,5,6])
x=[False,True,False,True,False,True]
newarr=arr[x]
print(newarr)

logic is same we are using the same Boolean index list wherever (at the index) the
value is true element would be taken out of the array then creating a new array out
of them that’s it.
And Rest is done in the file on the Vs code.

You might also like