0% found this document useful (0 votes)
5 views12 pages

Advantages of NumPy Arrays

NumPy is a core library for scientific computing in Python, providing a powerful n-dimensional array object and tools for various mathematical operations. It offers advantages over Python lists, such as reduced memory usage, faster execution, and convenience. The document covers basic NumPy operations, including array creation, dimension checking, data type identification, reshaping, and finding minimum, maximum, and sum of array elements.

Uploaded by

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

Advantages of NumPy Arrays

NumPy is a core library for scientific computing in Python, providing a powerful n-dimensional array object and tools for various mathematical operations. It offers advantages over Python lists, such as reduced memory usage, faster execution, and convenience. The document covers basic NumPy operations, including array creation, dimension checking, data type identification, reshaping, and finding minimum, maximum, and sum of array elements.

Uploaded by

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

Training On Python

Numpy Library
What is Python Numpy?

NumPy is a Python package which stands for ‘Numerical Python’. It is the core library for
scientific computing, which contains a powerful n-dimensional array object, provide tools
for integrating C, C++ etc. It is also useful in linear algebra, random number capability etc.
NumPy array can also be used as an efficient multi-dimensional container for generic data.

NumPy Array: Numpy array is a powerful N-dimensional array object which is in the form
of rows and columns. We can initialize numpy arrays from nested Python lists and access it
elements. In order to perform these numpy operations.
Implementation Of Numpy In Jupyter Notebook

Single-dimensional Numpy Array:

import numpy as np
a=[Link]([1,2,3])
print(a)
Output – [1 2 3]

Multi-dimensional Array:

a=[Link]([(1,2,3),(4,5,6)])
print(a)
O/P – [[ 1 2 3]
[4 5 6]]
Python Numpy Array v/s List

We use python numpy array instead of a list because of the below three reasons:
1. Less Memory
2. Fast
3. Convenient
The very first reason to choose python numpy array is that it occupies less memory as
compared to list. Then, it is pretty fast in terms of execution and at the same time it is very
convenient to work with numpy. So these are the major advantages that python numpy array
has over list.
Python Numpy Operations

ndim:
You can find the dimension of the array, whether it is a two-dimensional array or a single
dimensional array. So, let us see this practically how we can find the dimensions. In the
below code, with the help of ‘ndim’ function, I can find whether the array is of single
dimension or multi dimension.
import numpy as np
a = [Link]([(1,2,3),(4,5,6)])
print([Link])
Output – 2
Since the output is 2, it is a two-dimensional array (multi dimension).
Python Numpy Operations (cont..)

itemsize:
You can calculate the byte size of each element. In the below code, I have defined a single
dimensional array and with the help of ‘itemsize’ function, we can find the size of each
element.

import numpy as np
a = [Link]([(1,2,3)])
print([Link])

Output – 4
Python Numpy Operations (cont..)

dtype:
You can find the data type of the elements that are stored in an array. So, if you want to
know the data type of a particular element, you can use ‘dtype’ function which will print the
datatype along with the size. In the below code, I have defined an array where I have used
the same function.

import numpy as np
a = [Link]([(1,2,3)])
print([Link])

Output – int32
Python Numpy Operations (cont..)

You can find the size and shape of the array using ‘size’ and ‘shape’ function respectively.
import numpy as np
a = [Link]([(1,2,3,4,5,6)])
print([Link])
print([Link])

Output – 6 (1,6)
Python Numpy Operations (cont..)
reshape:
Reshape is when you change the number of rows and columns which gives a new view to an object. Now, let
us take an example to reshape the below array:

As you can see in the above image, we have 3 columns and 2 rows which has converted into 2 columns and 3
rows.
Python Numpy Operations (cont..)

Reshape Example:-

import numpy as np
a = [Link]([(8,9,10),(11,12,13)])
print(a)
a=[Link](3,2)
print(a)

Output – [[ 8 9 10] [11 12 13]] [[ 8 9] [10 11] [12 13]]


Python Numpy Operations (cont..)

linspace
This is another operation in python numpy which returns evenly spaced numbers over a specified
interval. Consider the below example:

import numpy as np
a=[Link](1,3,10)
print(a)

Output – [ 1. 1.22222222 1.44444444 1.66666667 1.88888889 2.11111111 2.33333333 2.55555556


2.77777778 3. ]
As you can see in the result, it has printed 10 values between 1 to 3.
Python Numpy Operations (cont..)

max/ min
Next, we have some more operations in numpy such as to find the minimum, maximum as well the sum of the
numpy array. Let’s go ahead in python numpy tutorial and execute it practically.

import numpy as np

a= [Link]([1,2,3])
print([Link]())
print([Link]())
print([Link]())

Output – 1 3 6

You might also like