NumPy Part-1
NumPy Part-1
org/
NumPy is a Python package that stands for Numerical Python. NumPy is a library for the Python programming
language, adding support for large, multi-dimensional arrays and matrices, along with an extensive collection of
high-level mathematical functions to operate on these arrays.
Why NumPy?
The very basic question that might come to our mind is why we use NumPy when we already have the list data
structure in Python; well, we have got many reasons to use NumPy over a list, but the very basic reason is the
use of less space and time.
Numpy is faster:
It provides efficient storage
Array creation
There are 5 general mechanisms for creating arrays:
2- Intrinsic numpy array array creation objects (e.g., arange, ones, zeros, etc).
4- Creating arrays from raw bytes through the use of strings or buffers.
1
Out[6]:
array([[1, 2, 3],
Out[7]:
[4, 5, 6]])
array([[[1, 2, 3],
Out[8]:
[4, 5, 6],
[7, 8, 9]]])
(4,)
Out[9]:
(2, 3)
Out[10]:
4
Out[11]:
6
Out[12]:
dtype('int32')
Out[13]:
array([3, 5, 7, 8])
Out[14]:
dtype('float64')
Out[16]:
In [17]: z.shape
(3, 3)
Out[17]:
In [18]: # array using arange attribute
# It returns from 0 to n-1 array
r=np.arange(5)
r
array([0, 1, 2, 3, 4])
Out[18]:
In [20]: # Reshape
ar.reshape(3,33)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_5052/2691225820.py in <module>
1 # Can't reshape of unmatched order
----> 2 ar.reshape(3,31)
In [24]:
# Empty array/matrics
e=np.empty((3,3))
e
In [26]: o=np.ones((3,3))
o
Axis in NumPy
Cartesian Coordinate Systems
You probably remember this, but just so we’re clear, let’s take a look at a simple Cartesian coordinate system.
array([[1, 2, 3],
Out[27]:
[4, 5, 6]])
array([5, 7, 9])
Out[28]:
array([ 6, 15])
Out[29]:
array([[1, 4],
Out[30]:
[2, 5],
[3, 6]])
2
Out[31]:
24
Out[32]:
2
Out[33]:
0
Out[34]:
6
Out[36]:
8
Out[37]:
Proof:
In [40]: import sys
In [42]: sys.getsizeof(1)*len(python_array)
112
Out[42]:
In [43]: numpy_array.itemsize*numpy_array.size
16
Out[43]:
NumPy TASK:
Q1: How to create an empty and a full NumPy 2D-Array of shape (3,4)? Take any integer value for full NumPy
2D-Array.
Q2: Create a NumPy Array that return evenly spaced numbers over a specified interval. Take 1 as start value, 12
as end value and number of samples are 12.
Q3: Create a NumPy Array that return evenly spaced values within a given interval. Take 99 as interval value. Can
we reshape this array into shape (3, 31)?
Q4: How to find elements of min index position in an given 2D array at axis 1, [[1,2,3],[4,5,6],[7,1,0]] Hint: argmin()
Q5: How to sort elements w.r.t indexing in asending order of given 2D array at axis 0, [[1,2,3],[4,5,6],[7,1,0]] Hint:
argsort()
Empty Array
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
Full Array
[[56 56 56 56]
[56 56 56 56]
[56 56 56 56]]
In [47]: arr=arr.reshape(3,31)
arr
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_5052/1710490344.py in <module>
----> 1 arr=arr.reshape(3,31)
2 arr
array([[0, 2, 2],
Out[49]:
[1, 0, 0],
[2, 1, 1]], dtype=int64)