Posts

Showing posts from July, 2020
Image
Numpy (Continued) Reshape Reshape is when you change the number of rows and columns which gives a new view to an object. import numpy as np a = np.array([(8,9,10),(11,12,13)]) print(a) b = a.reshape(6,1) print(b) -------------------------------------------------------------------------------------------------------------------- Slicing Slicing is basically extracting a particular set of elements from an array. import numpy as np a = np.array([(8,9),(10,11),(12,13)]) print(a[0:2,1]) # 2nd element from the zeroth and first index of the array import numpy as np a = np.array([(1,2,3,4),(3,4,5,6)]) print(a[0:, 2]) Few more example of slicing shown below: --------------------------------------------------------------------------------------------------------------------- linspace returns evenly spaced numbers over a specified interval. #It has printed 10 values between 1 to 3 import numpy as np a=np.linspace(1,3,10)
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