UNIT-6::PP
=======================================================================
Numpy Arrays: Creation, Processing Arrays, Types of Arrays, Arrays using numpy,
Operations on Arrays, attributes of arrays, multi-dimensional arrays, matrices in numpy,
=========================================================================
NumPy - Introduction
NumPy is a Python package. It stands for 'Numerical Python'. It is a library consisting of
multidimensional array objects and a collection of routines for processing of array.
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.
Why Use NumPy?
In Python we have lists that serve the purpose of arrays, but they are slow to process.
NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.
The array object in NumPy is called ndarray, it provides a lot of supporting functions that
make working with ndarray very easy.
Arrays are very frequently used in data science, where speed and resources are very
important.
Why is NumPy Faster Than Lists?
NumPy arrays are stored at one continuous place in memory unlike lists, so processes can
access and manipulate them very efficiently.
This behavior is called locality of reference in computer science.
This is the main reason why NumPy is faster than lists. Also it is optimized to work with
latest CPU architectures.
What are NumPy Arrays?
NumPy is a Python package that stands for ‘Numerical Python’. It is the core library for
scientific computing, which contains a powerful n-dimensional array object.
Where is NumPy used?
Python NumPy arrays 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.
Python 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, the next question which
will come in your mind is:
Operations using NumPy
Using NumPy, a developer can perform the following operations −
Mathematical and logical operations on arrays.
Fourier transforms and routines for shape manipulation.
, Operations related to linear algebra. NumPy has in-built functions for linear algebra
and random number generation.
Creation, Processing Arrays, Types of Arrays, Arrays using numpy
Moving ahead in python numpy tutorial, let us understand what exactly is a multi-
dimensional numPy array.
Here, I have different elements that are stored in their respective memory locations.
It is said to be two dimensional because it has rows as well as columns. In the above
image, we have 3 columns and 4 rows available.
Single-dimensional Numpy Array:
import numpy as np
a=np.array([1,2,3])
print(a)
Output – [1 2 3]
Multi-dimensional Array:
a=np.array([(1,2,3),(4,5,6)])
print(a)
O/P – [[ 1 2 3]
[4 5 6]]
a = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(a)
OUTPUT
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
a_3d_array = np.zeros((2, 2, 2))
print(a_3d_array)
OUTPUT
, [[[0. 0.]
[0. 0.]]
[[0. 0.]
[0. 0.]]]
Python NumPy Array v/s List
Why NumPy is used in Python?
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.
import numpy as np
import time
import sys
S= range(1000)
print(sys.getsizeof(5)*len(S))
D= np.arange(1000)
print(D.size*D.itemsize)
O/P – 14000
4000
The above output shows that the memory allocated by list (denoted by S) is 14000 whereas
the memory allocated by the NumPy array is just 4000. From this, you can conclude that
there is a major difference between the two and this makes Python NumPy array as the
preferred choice 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 = np.array([(1,2,3),(4,5,6)])
print(a.ndim)
Output – 2
Since the output is 2, it is a two-dimensional array (multi dimension).
=======================================================================
Numpy Arrays: Creation, Processing Arrays, Types of Arrays, Arrays using numpy,
Operations on Arrays, attributes of arrays, multi-dimensional arrays, matrices in numpy,
=========================================================================
NumPy - Introduction
NumPy is a Python package. It stands for 'Numerical Python'. It is a library consisting of
multidimensional array objects and a collection of routines for processing of array.
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.
Why Use NumPy?
In Python we have lists that serve the purpose of arrays, but they are slow to process.
NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.
The array object in NumPy is called ndarray, it provides a lot of supporting functions that
make working with ndarray very easy.
Arrays are very frequently used in data science, where speed and resources are very
important.
Why is NumPy Faster Than Lists?
NumPy arrays are stored at one continuous place in memory unlike lists, so processes can
access and manipulate them very efficiently.
This behavior is called locality of reference in computer science.
This is the main reason why NumPy is faster than lists. Also it is optimized to work with
latest CPU architectures.
What are NumPy Arrays?
NumPy is a Python package that stands for ‘Numerical Python’. It is the core library for
scientific computing, which contains a powerful n-dimensional array object.
Where is NumPy used?
Python NumPy arrays 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.
Python 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, the next question which
will come in your mind is:
Operations using NumPy
Using NumPy, a developer can perform the following operations −
Mathematical and logical operations on arrays.
Fourier transforms and routines for shape manipulation.
, Operations related to linear algebra. NumPy has in-built functions for linear algebra
and random number generation.
Creation, Processing Arrays, Types of Arrays, Arrays using numpy
Moving ahead in python numpy tutorial, let us understand what exactly is a multi-
dimensional numPy array.
Here, I have different elements that are stored in their respective memory locations.
It is said to be two dimensional because it has rows as well as columns. In the above
image, we have 3 columns and 4 rows available.
Single-dimensional Numpy Array:
import numpy as np
a=np.array([1,2,3])
print(a)
Output – [1 2 3]
Multi-dimensional Array:
a=np.array([(1,2,3),(4,5,6)])
print(a)
O/P – [[ 1 2 3]
[4 5 6]]
a = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(a)
OUTPUT
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
a_3d_array = np.zeros((2, 2, 2))
print(a_3d_array)
OUTPUT
, [[[0. 0.]
[0. 0.]]
[[0. 0.]
[0. 0.]]]
Python NumPy Array v/s List
Why NumPy is used in Python?
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.
import numpy as np
import time
import sys
S= range(1000)
print(sys.getsizeof(5)*len(S))
D= np.arange(1000)
print(D.size*D.itemsize)
O/P – 14000
4000
The above output shows that the memory allocated by list (denoted by S) is 14000 whereas
the memory allocated by the NumPy array is just 4000. From this, you can conclude that
there is a major difference between the two and this makes Python NumPy array as the
preferred choice 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 = np.array([(1,2,3),(4,5,6)])
print(a.ndim)
Output – 2
Since the output is 2, it is a two-dimensional array (multi dimension).