Skip to main content

Posts

Showing posts from May, 2022

Linear regression with n variables - Introduction

 Consider a function to be a more than 1 variable  y = mx this is a one variable ,m  y = w1*x1 , w1 the variable y = w1*x1 +w2 *x2 , 2 variables w1 and w2  y = w 1 *x 1 + w 2 * x 2  -  -  - - - - - - w n * X n  n    - variables these w1 --- w n are also called as weights. since from the data we have x1 , x2 ,,,,,x n and corresponding y 's  the idea is to optimise the equation ,that means to find the optimised  values for w1 .....w n , so why is that  we cannot change  the values , data as inputs ,the only thing we can do is to vary the data x using a multiplier w. so the input changes. assume the equation to be  y = x1 + x2  x1 be the data from real life scenario the housing data ,like no of bedrooms x2 be the data as no of floors y - the price of the house with no of bedroom and no of floors  y = 3 + 2 = 5  but the reals price might be  $7 , so difference of the values 7 - 5 =2 so the loss is not zero . the basic technique here would be  y = 3 + 2 * 2 what i have done here is mult

Newton–Raphson - Failure of the method to converge - example

The method does not converge , it means the approximation could not found for certain equations like for ex  y = 2 * x +2 * z +1 - b here b is the value we need to find  2 * x +2 * z +1 =  b if we try the iteration method for this to find the value of x where the loss is zero the cycle tends to repeat the same value for x alternatively . finding dy/dx = 2 ,dy/dz =2  x1 = x 0 - y(x0, z0)/ f '(x) z1 = z 0 - y(x0, z0)/ f '(z)  assume values for x0 = 0.5 , z0 = 0.4 , b = 24 y(0) = 2 *(0.5) +2 *(0.4) +1 - 24  y0=-21.2 x1 = 0.5 - (-21.2 /2) x1 = 11.1 z1 = 0.4 -  ( -21.2 /2)  z1 = 11 to check for convergence i wil substitute the value of x1 and z1 in the equation  so that if it converges ,means  y =0 ,because iam expecting a value of 24 from the  equation, lets see y1 = 2(0.5) +2(11) +1 -24 y1= 21.2  the loss here is too large not even close to zero. ok we wil go for the next iteration and see x2 = x1 - y1/f '(x1) z2 = z1 - y1/f '(z1) x2 = 11.1 - (21.2/2) x2= 0.5 z2 = 11 - (2

Newton–Raphson method - Introduction

This page only deals with the algorithm theory for example of this theory  check out   newtons-raphson-method-for-arriving-at.html   Alright  the theory says for a function  y = x - a , the optimization of parameter ' x ' where y = 0 /close to zero a technique  used for optimization of parameters in machine learning problem,  consider a = 5 , x = 5 , y will be zero but this case is not straight forward  the value for x is obtained using Newton–Raphson method , which is via iteration says  x n+1 = x n - f(x)/f '(x)  f(x) = y = x- a f '(x) = partial derivative of x  = dy/dx = 1 consider 2 iterations here n =0 ,1 it starts with assuming a initial value for x which is n =0 x 0   , y 0 plotting the values in graph  from the graph u can see i have plotted the x0, y0  tangent line to the point cuts the x - axis at x1 where the y is '0' says loss is zero at iteration x1, u may now wonder the the value of X has moved from X0 to X1 minimize the value of X where the loss

Newton's Raphson method - for arriving at minimum using approximation

Alright the  Newton–Raphson method is used here to find the approximate value of some variable. looks weird right ,the idea is to try out some technique to find the  parameter values of a function so that the loss is minimum /close to '0' The method derives the equation as   x n = x n-1 - f(x) /f '(x) f(x) is some function  f '(x) is the derivative of x   here  i have tried some function of my own or u could see the wiki ref for example from below u can the 1st iteration value ,but to minimse the loss we need to go for further iterations.         lets see how the 2nd iteration performs we have arrived at the value of x at x 1 so the loss which is y is zero so what we have achieved with this u can try out some other equation  to get the approx value , still thinking  consider another equation x ^2 = a ,this is to find the root of a ,meaning x = sqrt( a) . so the idea here to to optimize some parameter now it is 'x'  could be other parameter 'm ' or &#

Loss Function - Mean Absolute Error

 Mean Absolute Error -  it is straight forward import numpy as np import tensorflow as tf y_true = np.float32([ 1 , 2 ] ) y_pred = np.float32([ 2 , 2 ] ) #loss = mean(abs(y_true - y_pred)) #np.abs will return the absolute values , nps.abs[-1] will return 1 loss_mae = tf.keras.losses.mean_absolute_error(y_true, y_pred) print (loss_mae.numpy()) print (np.mean(np. abs (y_true - y_pred)))   print and verify  0.5 0.5 or u can run the code in colab using the link https://github.com/naveez-alagarsamy/matplotlib/blob/main/mean_absolute_error.ipynb   reference - https://www.tensorflow.org/api_docs/python/tf/keras/metrics/mean_absolute_error

Loss function - Mean Square Error

Mean Square Error is nothing but loss = square(y_true - y_pred) loss = (y_true - y_pred)**2   both are same     import numpy as np import tensorflow as tf   y_true = np.float32([ 1 , 2 ] ) y_pred = np.float32([ 2 , 2 ] ) iam using numpy mean to calculate the average of values loss = np.mean(np.square(y_true - y_pred) ) on the other hand u can verify with tensorflow MeanSquaredError  function to calculate mse because in future we will use these for our machine  learning samples    mse = tf.keras.losses.MeanSquaredError() mse_loss_tf = mse(y_true, y_pred).numpy() print the values to verify print (loss) print (mse_loss_tf) 0.5 0.5 u can use the git link and run it in colab https://github.com/naveez-alagarsamy/matplotlib/blob/main/mean_square_error.ipynb   reference -  https://www.tensorflow.org/api_docs/python/tf/keras/metrics/mean_squared_error   

Linear Regression with one variable - Introduction

 It is not but making a some how clear relationship among variables the dependent and independent variables. talking in terms of maths the equation can be used meaningfully for something may be to determine /predict values from data. if y = m * x + b  the values for m , b can be anything but has to appropriate to predict y  so the loss which is  difference from existing to prediction is close to zero ~0 to start with we can say the one variable as -x  in some scenario m , b are called variables    the equation stated about is a line equation we have any equation  y = 2*x  y = x*x y = 2x +2x*x  so why the need of all these equations , it is all about playing data now a days in machine learning problems we create a data sets , lets consider as x  y to be a value of x the datas . y = datas  when we express the data as a function and plot in the graph we get the curves  take some random data x and plot x and y  x =1 , 2, 3 ,4  y = x  the equation we have formed here is whatever the value o

Partial Derivative - Chain Rule

what is the chain rule ? consider the function y = m * x +b  i am going to work on the function which we can use for our machine learning problems . consider a loss function h = [y - y_hat] ^2 which is also called mean square error y - dependent on x y_hat - predicted value  h = called loss    so why again two functions , ok h = [m * x +b - y_hat]^2  looks complicated right ,this is where we will use the  chain rule ,how then ? say partial derivative of m , b are [1]  dy /dm  = x , dy/db = 1 do a substitution for h  = u^2  , u = [y- y_hat] [2]  dh/du = 2u = 2 * (y - y_hat) du/dy = 1 so dh/dy  = dh/du * du /dy    --------[ chain rule] = 2 * (y - y_hat) *1 dh/dy = 2 * (y - y_hat) so what is partial derivative of  dh/dm and dh/db  then ?   chain rule again from 1 and 2  dh/dm = dh/dy * dy/dm [3] dh/dm =  2 * (y - y_hat) * x  dh/db = dh/dy * dy/db [4] dh/db   = 2 * (y - y_hat) * 1 values 3 and 4 are used in values corecction for m and b  new value of  m = m - dh/dm new value of b = b

Partial Derivative - Introduction

  So what is partial derivatives ,don't worry i am not going to take a boring maths class will go straight to the point 1.partial derivative are used in machine learning back propagation. so all of a sudden what is this back propagation  before that there must be a forward prop or forward pass , ok got it don't worry we will talk about a example 1.consider data points x =[1, 2, 3] some values for our understanding 2. consider a function y =x so the values of y will be [1, 2 , 3] 3.consider a function y= x^2 so the values of y will be [1, 4, 9]  4. In real life the data points could be different and the dependent function y could be like y =m*x +b or y = w1* x1 +w2  * x2 +b as u can see the data plotted and the line we are trying to fit could be either the green or blue line , so what are tryin to achieve with this  1. plot your data as X 2. have a function Y as dependant of X 3.use of function is to predict values of y for x since we have introduced variables like m , b , w1, w

Fourier Transform - Basics - Example

 U might wonder how fft is performed using python or any using any language the concept here is to explain the fft in detail with example u can run the code in python using colab or copy the code below to run in your local m/c. Consider a sample of data  that  has N data points against time the fft for the data points will give you the amplitude for the data points which could a  complex number  again what is a complex number (a+ib) i*i = -1 again why this complexity don't worry too much we will talk about this in detail. consider  a - to be the x axis (real) ib - y axis (Imaginary) still confused with the formula don't worry we will work a sample for f(0) so now we have seen the computation for f(0) in the same way calculate till f(N-1) ,run the code below to cross check the values.  result - f(0) = (-1.1102230246251565e-16+0j) f(1) = (5.551115123125783e-16-3.9996979771955568j) f(2) = (-1.410503594010501e-16+2.220446049250313e-16j) f(3)= 0.00030202280444291407j f(4) = 2.028015

Fourier Transform - FFT

  so what is FFT or the fourier transform. what is the use of the transform ok an example would be a couple of data points in a digital world and you want to  transform the data. so why we need to transform the data  consider a couple of digital data for sound wave and you want to do some manipulation on the data ,sine it resembles a sine wave  we plot the data points as a sin wave    your x axis would be time  y axis would be your data  The theory here is sound wave has a mix of signals so sin wave of  different frequencies ,since we have collected data over time we have to transform time data to freq data   in the freq domain our x axis would be angular frequency  y axis would be amplitude.     you can find the link for the FFT and inverse FFT for  a sin wave      https://github.com/naveez-alagarsamy/matplotlib/blob/main/fourier.ipynb   if u cant acess the link use the below code to run locally   import matplotlib.pyplot as plt import numpy as np from numpy.fft import fft