<jupyter_start><jupyter_text>NUMPY These are very similar to python lists Functionalities in numpy are written in C language(which is very fast) Therefore we use numpy instead of list in python to perform operations in faster way NumPy actually converts data into set of numbers(NumPy array) on which the ML algorithms work to predict patterns  Importing libraries<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>DataTypes  There is actually only one data type i.e "nd-array" which refers to n dimensional array.                                        It is declared as :<jupyter_code>a1=np.array([1,2,3])
a1
type(a1)
a2=np.array([[1,2,3],
             [4,5,6]])
a2
a3=np.array([[[1,2,3],
              [4,5,6],
              [7,8,9]],
            [[10,11,12],
             [13,14,15],
             [16,17,18]]])
a3<jupyter_output><empty_output><jupyter_text>![anatomyof%20numpy%20array.jpg](attachment:anatomyof%20numpy%20array.jpg)  Attributes<jupyter_code>a1.shape
a2.shape
a3.shape
##ndim short for no of dimensions
a1.ndim,a2.ndim,a3.ndim
# dtype gives out the datatype
a3.dtype


# size attribute gives the total no of elements in the array
a1.size,a2.size,a3.size

type(a1),type(a2),type(a3)<jupyter_output><empty_output><jupyter_text>Creating a dataframe using numpy array<jupyter_code>import pandas as pd
df=pd.DataFrame(a2)
df<jupyter_output><empty_output><jupyter_text>Creating arrays  ones(size):<jupyter_code># this function creates an array consisting of only 1's of the given size
ones=np.ones((3,3))
ones # by default data type is float<jupyter_output><empty_output><jupyter_text>zeros(size)<jupyter_code># this function is similar to ones but here the array is filled with zeroes of given size
zeroes=np.zeros((2,4))
zeroes # default datatype is zeroes in order to change we have to mention the data type
zeroes=np.zeros((2,4),dtype=int)
zeroes<jupyter_output><empty_output><jupyter_text>arange(start,stop,step):<jupyter_code>#This function prints an array from the start and step and prints the no till it reacches stop
range_a=np.arange(0,15,3)
range_a<jupyter_output><empty_output><jupyter_text>random.randint(start,stop,size=(m,n))<jupyter_code># This function print an array of size m*n filled with random elements between start(included) and stop(excluded)
random=np.random.randint(1,9,size=(4,5))
random<jupyter_output><empty_output><jupyter_text>random.random((m,n)):<jupyter_code>random.shape
# this function creates an array of size m*n filled within interval of 0.0 and 1.0
np.random.random((3,5))
# each time we run it a new random array is generated<jupyter_output><empty_output><jupyter_text>So in order to stop generating new array each time we run we use special function called                                        "Random seed"  random.seed(x)<jupyter_code>np.random.seed(4)
random_1=np.random.random((5,3))
random_1
np.random.seed(5)
random_2=np.random.randint(1,10,size=(3,4))
random_2<jupyter_output><empty_output><jupyter_text>Viewing arrays and Matrices  np.unique(arrayname):gives out all the unique elements presents in the array<jupyter_code>np.unique(random_2)
a1
a2
a3
a1[1]
a2[0][2]
a3.shape
a3[:,:,:]<jupyter_output><empty_output><jupyter_text>Manipulating & Comparing arrays  Arithmetic<jupyter_code>a1
ones=np.ones(3,)
ones
a1+ones
a2
a2+ones
a2-ones
r=np.random.randint(10,size=(3,))
r
a2*r
a1*a2
# normal division doesn't remove decimals
a2/a1
# double-slash indicates "Floor division" which removes the decimals
a2//a1
# double-asterix indicates power i.e x**i == x^i
r ** 3
np.square(r)
# % == modular division prints out the remainder
r%2
r/2 # gives out quotient
# exponential
np.exp(a1)
# logarihmic
np.log(a1)<jupyter_output><empty_output><jupyter_text>Aggregationperforming the same operation on number of things<jupyter_code># consider a list
a=[1,2,3]
type(a)
sum(a)
#a function that caliculates sum of all elements in the list
# now consider an numpy array
a1
sum(a1),np.sum(a1)<jupyter_output><empty_output><jupyter_text>They both produce same output eventhough they are diffrent functions            they both are aggregation function So use Python's method ('sum()') on pyhton data types and use NumPy's methods on NumPy arrays('np.sum()')<jupyter_code>#create a massive array (which demonstrates the speed of numpy array)
massive_a=np.random.random(100000)
massive_a.size
massive_a[:10]<jupyter_output><empty_output><jupyter_text>Magic function's:They always start with an % symbol at start Ex: %timeit:this is going to show how long a particular line of code takes to run<jupyter_code>%timeit sum(massive_a)# python's sum()
%timeit np.sum(massive_a)# numpy's np.sum()
a2
np.mean(a2)
np.max(a2),np.min(a2)
# Standard Deviation = a measure of how spread out a group of numbers is from the mean
np.std(a2)
# Variance = measure of the average degree to which each number is diffrent to the mean
# High variance = wide range of numbers
# low variance = lower range of numbers
np.var(a2) 
# standard deviation = sqrt of variance
np.sqrt(np.var(a2))<jupyter_output><empty_output><jupyter_text>Reshaping  This is performed to reshape or change the shape of an array here the data stays the same only the shape of array changes.      This is done to do compatible operations for ex:<jupyter_code>a2
a2.shape
a3
a3.shape
# So when we perform their multiplication we get an error
a2*a3<jupyter_output><empty_output><jupyter_text>This error arose because their shapes are incompatible i.e they are not following broad casting rules which clearly says: ![broadcasting%20rules.jpg](attachment:broadcasting%20rules.jpg) So therefore in order to perform the operation we need to make their shapes equal or one of them to 1 dimension i.e reshape a2  reshape(newsize):This function reshapes the old array into new array given sizeCustomises shape<jupyter_code>a2.shape
reshape_a2=a2.reshape(2,3,1)
reshape_a2
# now we can perform multiplication since a2 dimension is 1 and they are compatible
reshape_a2*a3<jupyter_output><empty_output><jupyter_text>Transposeit switches the axis<jupyter_code>a2
a2.shape
a2.T #transpose the matrix
a3
a3.shape
a3.T
a3.T.shape<jupyter_output><empty_output><jupyter_text>Dot Product` Use case of transpose of matrix ![DotProduct.jpg](attachment:DotProduct.jpg)<jupyter_code>np.random.seed(10)

m1=np.random.randint(15,size=(4,5))
m2=np.random.randint(15,size=(4,5))

m1
m2
m1.shape,m2.shape
# gives out "Element wise multiplication"(Hadamard multiplication)
np.multiply(m1,m2) # numpy method
m1*m2 # python method
# Dot Product => Similar to "Matrix Multiplication"
np.dot(m1,m2)<jupyter_output><empty_output><jupyter_text>The error came up because we cant multiply (4,5) & (4,5)    the multiplication is onle possible when the shapes are (4,5) & (5,4)  ![WhatsApp%20Image%202021-05-04%20at%201.30.42%20PM.jpeg](attachment:WhatsApp%20Image%202021-05-04%20at%201.30.42%20PM.jpeg) ![WhatsApp%20Image%202021-05-04%20at%201.33.08%20PM.jpeg](attachment:WhatsApp%20Image%202021-05-04%20at%201.33.08%20PM.jpeg) Therefore we use transpose or we can use Reshape also<jupyter_code># transpose m2
m2.T
m1.shape,m2.T.shape
m3=np.dot(m1,m2.T)
m3
m3.shape<jupyter_output><empty_output><jupyter_text>Comparsion Operators<jupyter_code>a1
a2
a1>a2
a1>=a2
a2>5
a2<5
a1 == a2<jupyter_output><empty_output><jupyter_text>Sorting Arrays<jupyter_code>np.random.seed(10)
random_3=np.random.randint(15,size=(4,5))
random_3<jupyter_output><empty_output><jupyter_text>np.sort():This actually sorts each and every row  in ascending order<jupyter_code>np.sort(random_3)<jupyter_output><empty_output><jupyter_text>np.argsort():It returns the ranking of each element in a row i.e lowest no in a row gets index 0 and so on<jupyter_code>np.argsort(random_3)
np.argsort(a1)
#np.argmin() gives the index of min no in an array
np.argmin(a1)
np.argmax(a1)<jupyter_output><empty_output><jupyter_text>Image into NumPy array<jupyter_code># Turn image into numpy array
from matplotlib.image import imread 
panda = imread("panda.png")# imread() read an image from a file into array
type(panda)
panda
panda.size,panda.shape,panda.ndim<jupyter_output><empty_output><jupyter_text>This imread stores all the RGB values of each pixel<jupyter_code>car = imread("car-photo.png")
car.shape,car.size,car.ndim<jupyter_output><empty_output><jupyter_text><jupyter_code>dog=imread("dog-photo.png")
dog<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10673822301729279, 'fraction_numerical': 0.04042933810375671, 'mean_word_length': 6.058922558922559, 'pattern_counts': {'":': 0, '<': 110, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '27', 'dir': 'jupyter-structured-clean-dedup', 'id': '577050', 'license': 'MIT', 'n_tokens_mistral': 3166, 'n_tokens_neox': 3013, 'n_words': 801, 'path': 'NUMPY.ipynb', 'repo_name': 'sathvik-satrasala/ML-NoteBooks', 'size': '8385'}