Num Py
Num Py
To get the diagonal elements in a two dimensional array we use diag as a function.
5. randint:
This function is used to generate a random number between a given range.
The syntax is:
random.randint(min, max, total_value)
6. rand:
This functio is used to generate a random values between 0 to 1
The syntax is:
random.rand(number of values)
We may generate random numbers in 2D array format as well.
7. randn:
This function is used to generate a random values close to 0 (Zero).
This may return positive or negative numbers as well.
The syntax is:
random.randn(number of values)
Indexing & Slicing of NumPy
Array
Indexing in One Dimensional Array
It Supports Indexing from left to right as well right to left.
When left to right indexing starts:-
0,1,2,3,4…….
While right to left indexing starts:-
…….,-3,-2,-1
The indexing technique is much similar to the indexing technique of List.
Slicing in One Dimensional Array
Slicing is a technique used to extract a portion of a sequence, such as an array or a list.
Basic Syntax:
The basic syntax for slicing a one-dimensional array typically looks like this:
array_name[start : stop : step]
Start: The index of the first element you want to include in the slice.
Stop: The index of the first element you want to exclude from the slice.
Step: The step size between elements. This is optional, and the default is 1.
Reshaping of Array
Converting to 3D Array :-
# Creating a 2D array
array_2d = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
import numpy as np
# Creating a 2D array
array_2d = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Basic Syntax:
The basic syntax for slicing a Two-Dimensional array typically looks like this:
Variable matrix name[start_row : stop_row , start_column : stop_column]
Seed Function in Python Numpy
In NumPy, “numpy.random.randint()” function generates random numbers.
Every time we run the programme, new setup random number is generated but what if we
want to fix the generation of this random number in this case we use seed function, this
By setting the seed value, you ensure that the sequence of random numbers generated is
Unique method:-
The Unique method function is
used to find the unique elements of an
array and return them in sorted order. It
can also return the indices of the unique
elements.
You can also use the return_counts parameter to get the counts of each unique element:
Stack function:-
The Stack function is used to join a sequence of arrays along a new axis.
The arrays must have the same shape, and the new axis is added to the result to represent the stack of
the input arrays.
Searching an Element in NumPy Array:-
We can search a value in an array and if the value is found in the concerred index is returned.
To search an array we use where() method.
Search sorted() method:-
The search sorted() method in NumPy is used to find the indices where elements should
be inserted to maintain the order of the array. It performs a binary search on the sorted array to locate the
insertion points.