NUMPYA03
NUMPYA03
Python Library:-
Numpy
By ;
Shyam Ramani Id no.-23CP001,
Rudra Chauhan Id no.-23CP010,
Sujal Patel Id no.-23CP018,
Contents:
•
Introduction to Numpy
•
Creating Arrays
•
Array Slicing
•
Data Types
•
Copy Vs View
•
Array Shape
•
Array Reshape
•
Array Iterating
•
Array Join
•
Array Split
•
Array Search
•
Array Sort
•
Array Filter
What is Numpy ?
●
Numpy is an array-processing package which is most foundational
package for numerical computing in Python.
●
NumPy is 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 is short for "Numerical Python".
Why to use Numpy ?
•By now, you are familiar with Python lists and how
incredibly useful they are.
•So, you may be asking yourself:
2 Ipsum 4 Sit
Lorem ipsum dolor sit Lorem ipsum dolor sit
amet, consectetuer amet, consectetuer
adipiscing elit. adipiscing elit.
1 Lorem 3 Dolor
Lorem ipsum dolor sit Lorem ipsum dolor sit
amet, consectetuer amet, consectetuer
adipiscing elit. adipiscing elit.
Installation:
●
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
Importing numpy:
●
Once NumPy is installed, import it in your applications by
adding the import keyword:
import numpy as np
*NumPy is usually imported under the np alias for which Create an alias with
the as keyword while importing
(Note: In Python alias are an alternate name for referring to the same thing.)
Checking NumPy Version
●
type(): This built-in Python function tells us the type of the object
passed to it. Like in above code it shows that arr is numpy.ndarray type.
●
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 Array:
●
A dimension in arrays is one level of array depth (nested arrays).
●
For example ,0-D array,1-D array,2-D array,3-D array,higher dimensional
arrays.
●
e.g. for 1-D array,
Using ndim function:
Example:
Output:
Output:
Array Indexing:
●
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.
●
Examples:
Output: Output:
Negative indexing:
●
Example:
Output:
Slicing of 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].
●
If we don't pass start its considered 0
●
If we don't pass end its considered length of array in that
dimension
Examples of slicing:
Output:
Output:
Copy vs 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.
To check if array owns data:
●
i.e. copy or view;
●
Every NumPy array has the attribute base that returns None if
the array owns the data.
Output:
Continued:
Output: Output:
Data Types in Numpy:
●
NumPy has some extra data types then default datatypes ,
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 data-type:
Output:
Output:
Reshaping of Array:
● 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.
Output:
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.
Output: Output:
●
The function nditer() is a helping function that can be used from very basic to very
advanced iterations. It solves some basic issues which we face in iteration, l.e. In
basic for loops, iterating through each scalar of an array we need to use n for loops which
Continued:
Output:
Joining Arrays:
● Joining means putting contents of two or more arrays in a single array.
Output:
Splitting Array:
● 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.
Output: Output:
Searching in 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.
Output:
Output:
SearchSorted:
● 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.
● The searchsorted() method is assumed to be used on sorted arrays.
Output:
Output:
Sorting Arrays:
● Sorting means putting elements in an ordered sequence.
● 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.
Output:
Filtering Array:
● Getting some elements out of an existing array and creating a
new array out of them is called filtering.
● In NumPy, you 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 value at an index is True that element is contained in the
filtered array, if the value at that index is False that element is
excluded from the filtered array.
Output:
Output:
REFERNCES :
●
https://numpy.org/
●
Youtube- WsCube Tech
●
https://www.w3schools.com/
Thank You !