NUMPY -Numerical Python
•NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays.
•A powerful N-dimensional array object
•Sophisticated functions
•Tools for integrating C/C++ and Fortran code
•Useful linear algebra, Fourier transform, and random number capabilities
#Single-dimensional Numpy Array
import numpy as np
a = np.array([1,2,3])
#Multi-dimensional Array
a = np.array([(1,2,3),(4,5,6)])
print(a)
------------------------------------------------------------------------------------------------------------
Python NumPyArray v/s List
We use python numpy array instead of a list because of the below three reasons:
•Less Memory
•Fast
•Convenient
#comparing memory occupied by both
import numpy as np
import sys
#memory occupied by list
s = range(1000)
print('memory occupied by List',sys.getsizeof(10)*len(s))
#memory occupied by numpy
d = np.arange(1000)
print('memory occupied by Numpy Array',d.size*d.itemsize)
-----------------------------------------------------------------------------------------------------------
#comparing speed of list and numpy array
import time
size = 10000000
#checking list taken time
l1 = range(size)
l2 = range(size)
start = time.time()
result = [(x,y) for x,y in zip(l1,l2)]
print('Time taken by List:', time.time()-start)
#checking numpy array taken time
a1 = np.arange(size)
a2 = np.arange(size)
start2 = time.time()
result_numpy = a1+a2
print('Time taken by numpy:', time.time()-start2)
-------------------------------------------------------------------------------------------------------------
import numpy as np
np.arange(1,10,3)
np.arange(1,8,3)
np.arange(1,10.1,3)
np.arange(7, 0, -3)
x = np.arange(5)
print(x)
print(x.dtype)
print(x.itemsize)
-------------------------------------------------------------------------------------------------------------
# if you provide equal values for start and stop,
# then you will get an empty array
np.arange(2,2)
np.arange(8,2,1)
np.arange(2,8,-1)
-------------------------------------------------------------------------------------------------------------
# ndim
# Find the dimension of the array, whether it is a two-dimensional array or a single dimensional array
import numpy as np
a = np.array([(1,2,3),(4,5,6)])
a1 = np.array([((1,2,3),(4,5,6)),((1,2,3),(4,5,6))])
print(a.ndim)
print(a1.ndim)
-------------------------------------------------------------------------------------------------------------
# itemsize
# calculate the byte size of each element
import numpy as np
a = np.array([(1,2,3)])
print(a.itemsize)
# dtype
# find the data type of the elements that are stored in anarray. So, if you want to know the data type of a particular element, you can use‘dtype’ function which will print the datatypealong with the size.
import numpy as np
a = np.array([(1,2,3)])
print(a.dtype)
--------------------------------------------------------------------------------------------------------------
# ‘size’ and ‘shape’
# find the size and shape of the array using ‘size’ and ‘shape’ function respectively.
import numpy as np
a = np.array([(1,2,3,4,5,6)])
a1 = np.array([(1,2,3,3),(4,5,6,4)])
print(a.size)
print(a.shape)
Comments
Post a Comment