0% found this document useful (0 votes)
1 views52 pages

Num Py

The document provides an overview of NumPy, a library created by Travis Oliphant in 2005, which facilitates efficient mathematical and logical operations on arrays, outperforming Python lists in speed and memory usage. It details various functionalities, including array creation, indexing, slicing, and essential functions like zeros, ones, and random number generation. Additionally, it compares NumPy arrays with Python lists in terms of data types, performance, memory usage, and ease of use, highlighting NumPy's advantages for scientific computing.
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)
1 views52 pages

Num Py

The document provides an overview of NumPy, a library created by Travis Oliphant in 2005, which facilitates efficient mathematical and logical operations on arrays, outperforming Python lists in speed and memory usage. It details various functionalities, including array creation, indexing, slicing, and essential functions like zeros, ones, and random number generation. Additionally, it compares NumPy arrays with Python lists in terms of data types, performance, memory usage, and ease of use, highlighting NumPy's advantages for scientific computing.
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/ 52

This was created in 2005 by Travis Oliphant.

Using NamPy we can perform the following functionalities:


1. Mathematical and logical calculations on array.
2. Fourier transformation and routines for shape manipulation.
3. It has built in function for Linear Algebra, random number generation.
4. Used for the scientific calculation.
5. It is faster than the python list.
6. It is fast because it is associated with C programming.
1.Data Types:
1.NumPy Arrays: NumPy arrays are homogeneous, meaning all elements of an
array must be of the same data type. This allows for more efficient storage and
operations.
2.Lists: Lists in Python can contain elements of different data types. This flexibility
comes at the cost of increased memory overhead.
2.Performance:
1.NumPy Arrays: NumPy is designed for numerical operations and is highly
optimized for performance. It uses contiguous blocks of memory and allows for
vectorized operations, making it faster for numerical computations.
2.Lists: Lists are more general-purpose and are not optimized for numerical
operations. Iterating over a list in Python involves interpreting the type of each
element, which can be slower than NumPy operations.
3.Memory Usage:
1.NumPy Arrays: NumPy arrays are more memory efficient compared to lists,
especially for large datasets, because they store data in a contiguous block of
memory.
2.Lists: Lists in Python have more overhead, as they store not only the data but
also additional information such as the type of each element and the size of the
list.
4.Conciseness of Code:
1.NumPy Arrays: NumPy provides a concise and expressive syntax for array
operations, often allowing you to perform complex operations in just a few lines of
code.
2.Lists: While lists are versatile, performing complex numerical operations with lists
may require more code and be less readable than equivalent NumPy code.
5.Functionality:
3.NumPy Arrays: NumPy provides a wide range of mathematical functions and
operations that are optimized for arrays, such as linear algebra, statistical
operations, and Fourier analysis.
4.Lists: Lists are more general-purpose and lack the specialized functionality for
numerical operations that NumPy provides.
6.Ease of Use:
5.NumPy Arrays: NumPy is more suited for mathematical and scientific computing,
and it provides convenient methods for array manipulation.
6.Lists: Lists are more versatile and may be easier to use for general-purpose tasks
where numerical efficiency is not a primary concern.
 Numpy is often called as an alternate for MATLAB (a programming platform designed
specifically for engineers and scientists for Data Analysis, Developing Algorithm,
Create models and applications etc).
 NumPy is used with packages like SciPy (Scientific Python) and Mat-Plotlib (plotting
library) and this makes it capable to replace MATLAB.

 1 Dimensional Array is referred to as Vector.


 2 Dimensional Array is referred to as Matrix.
 3 Dimensional Array is referred to as Tensor.
Creating NumPy Array (One Dimensional)
Creating NumPy Array (Two Dimensional)
NumPy Functions
1. Zeros:
This function creates an array (either one dimensional or multi dimensional)
and fill all the values with Zero (0).
2. Ones:
This function creates an array (either one dimensional or multi dimensional)
and fill all the values with One (1).
3. eye:
These function creates an array with all the diagonal elements as 1 and rest as 0 (in a square matrix).
In case of a non square matrix the values are still 1 for diagonal (upto which it can be drawn diagonal) and
rest are 0.
4. diag:
This function creates a two dimensional array with all the diagonal elements as the given value and rest
as 0 (in a square matrix).

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

Now it has been converted to 2D Array


Again converting this 2D Array to 1d Array:-

Converting to 3D Array :-

Again converting this 3D Array to 1d Array:-


Indexing of 2 Dimensional Array
1st Column 2nd Column 3rd Column
1st row [0][0] [0][1] [0][2]

2nd row [1][0] [1][1] [1][2]

3rd row [2][0] [2][1] [2][2]

1st Column 2nd Column 3rd Column


1st row [0,0] [0,1] [0,2]

2nd row [1,0] [1,1] [1,2]

3rd row [2,0] [2,1] [2,2]


import numpy as np

# Creating a 2D array
array_2d = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

# Accessing individual elements


Basic Syntax:
The basic syntax for indexing a Two-Dimensional array typically looks like this:
Variable array name[row_index, column_index]
Accessing Individual Elements:
Slicing Two Dimensional Array

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

generates fixed set of random numbers.

By setting the seed value, you ensure that the sequence of random numbers generated is

the same every time the code is run.


Conditional Selection in Python NumPy
Various Operations on NumPy Array
Operations on 2D Array
NumPy Some More Important Functions
Linspace Function:
The linspace function in NumPy is used to create an array of evenly spaced values over
a specified range. It is particularly useful for generating linearly spaced vectors.
Creating 2D Array with fixed Datatypes
Cumulative Sum:-
The cumulative sum at each position in an array is the sum of all the elements before it, including itself.
Shuffle method:-
Shuffle method function is randomly
rearranges the elements of the
array.
Note that the shuffle is done in-
place, so the original array is
modified.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy