ML Club sessions
Introduction to ML Models

Community session · Epoch, IIT Hyderabad · Aug 28, 2022

A brief introduction to modeling, a few basic terms and linear regression models, with a hands-on exercise on data collected from the audience.

Slide 1 of 57
1 / 57

Introduction to ML Models

Community session · Epoch, IIT Hyderabad · Aug 28, 2022

A brief introduction to modeling, a few basic terms and linear regression models, with a hands-on exercise on data collected from the audience.

  1. Introduction to ML Models By Epoch Team, IIT Hyderabad
  2. Let’s start with an example… Given: Data of salary of some persons with their years of experience Goal: Predict the salary of a person with 8 years of experience.
  3. Recap time!!! Supervised Learning vs Unsupervised Learning 1. Supervised learning trains a model on known input and output data so that it can predict future outputs whereas unsupervised learning finds hidden patterns or intrinsic structures in input data. 2. Or in other words, supervised learning make predictions based on labeled training data & unsupervised learning itself identifies relationships between the unlabeled training data.
  4. 1. It is a supervised learning algorithm that assumes a linear relationship between the dependent variable and the independent variable. 2. We will try to fit a line (hyperplane in case of multidimensional features) that will best map the training data to its label. Linear Regression!!? What is it?
  5. 1 2 3 Which line do you think is the best fit line?
  6. Finding the best-fit line or Hyperplane
  7. In case of multiple number of features, it can be written as: Multiple features => More Math :/
  8. Matrix Multiplication - The Real Dad!! :)
  9. Loss function - Meet our old friend!! 1. Loss function predicts the deviation of the predicted outcome from the actual result. It measures how well the machine learning algorithm models the training dataset. 2. We convert the machine learning problem into an optimization problem, by defining the loss function and then gradually optimizing the algorithm so that the loss function is minimized. 3. One of the common loss functions is mean squared error (MSE).
  10. Loss function in Linear Regression More or less… This is the difference between predicted values by the line/hyperplane and actual values. Goal: Find which minimizes the loss function 𝚹
  11. Gradient Descent 1. It is an optimization algorithm/technique commonly used to train machine learning models. 2. Key Idea: Take the fastest route towards the minimum of the function from current point to converge fast. 3. We take repeated steps in the opposite direction of the gradient of the function as that is the steepest descent possible
  12. Audience be like:
  13. Get this clear! What do we do in Gradient Descent?? 1. Initialize to any value. 𝚹 2. Compute the loss function at that value of . 𝚹 3. Update until the loss function reaches very close to its minimum. 𝚹
  14. How to update ??? 𝚹 α decides the length of the steps. It is called learning rate. This determines the direction of the step. To update , 𝚹move in the opposite direction of the slope.
  15. More about the Learning Rate - ⍺ 1. Parameter you have to choose on your own. 2. Parameter tuning: Process of choosing optimal ⍺, which will: a. Reduce computing time b. Increase accuracy of model
  16. How does the value of matter? ⍺
  17. (image slide)
  18. (The Chain Rule :3) Let’s get into the Math! Courtesy: CS229 lecture notes
  19. So, now we can update the parameters as: We get this as the Final Algorithm: Courtesy: CS229 lecture notes
  20. Back to the example… (8.0,93. 9)
  21. ❖We update the parameters after looking at every example in the entire training set on every step. This method is called batch gradient descent ❖Even though gradient descent can be susceptible to local minima in general, the optimization problem we have posed here for linear regression has only one global, and no other local, optima; thus gradient descent always converges (assuming the learning rate α is not too large) to the global minimum. Points to Remember…
  22. So, we have the csv file….. Now what? ❖Pandas is a software library written for the Python programming language for data manipulation and analysis Well, now we use pandas to read and analyse the data and perform preprocessing.
  23. Commonly used functions from pandas ●read_csv() : read_csv() function helps read a comma-separated values (csv) file into a Pandas DataFrame. All you need to do is mention the path of the file you want it to read.
  24. ●head(): head() is used to return the first n rows of a dataset. By default, df.head() will return the first 5 rows of the DataFrame. If you want more/less number of rows, you can pass an integer to the function.
  25. ●describe(): describe() is used to generate descriptive statistics of the data in a Pandas DataFrame or Series. It summarizes central tendency and dispersion of the dataset.
  26. ●info(): This method prints information about a DataFrame including the index dtype and columns, non-null values and memory usage.
  27. ●sample(): This method returns a random sample from the dataset. frac specifies the % of dataset to used. Random_state specifies the seed for random number generator. We generally give this seed so that we don’t get different samples everytime we run the code.
  28. ●drop(): This method drops the specified column. Pass the parameter inplace=True if you want the changes to be applied on the actual dataset. axis=1 declares to look for the value to be dropped in columns.
  29. ●mean(): This method return the mean of the specified column in dataset. ●median(): This method return the median of the specified column in dataset.
  30. ●fillna(): This method fills the null values with the the user defined value.
  31. ●to_numpy(): This method converts the data frame into a numpy array. (numpy? o_0)
  32. ❖NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays. Adding to your armoury - The NumPy Library!!
  33. (image slide)
  34. Commonly used functions from NumPy ●array(): It is used to create one-dimensional or multidimensional arrays from scratch. ●reshape(): It is used to reshape arrays. It returns a new array and doesn’t modify the actual array.
  35. (image slide)
  36. ●zeros: This method returns a matrix of user defined size with all elements equal to zero. ●ones: This method returns a matrix of user defined size with all elements equal to one.
  37. ●mean(): This method returns the mean of the specified array. ●median(): This method return the median of the specified array.
  38. ●c_[]: This method adds a given column to the beginning of a given array. ●r_[]: This method adds a given row to the beginning of a given array.
  39. ●append(): This method adds given column(s) or row(s) to the end of a given array. The parameter ‘axis’ defines if the array is appended as column(s) or row(s).
  40. ●dot(): This method performs dot product of two matrices. We also can use ‘@’ operator to do the same.
  41. ●linalg.inv(): This method returns inverse of a matrix. ●T(): This method returns the transpose of a matrix.
  42. Yes! You can do such cool stuff with matplotlib! ;p Time for some colourful plots :D
  43. A few commonly used functions from matplotlib: ●plot() :
  44. ●scatter():
  45. ●bar():
  46. ●xlabel(), ylabel():
  47. ●legend():
  48. What if some feature is not a number? Gender Male Female Female Male Remarks Nice Good Nice Great Nationality India USA UK India
  49. Encoding Ordinal Encoding Encoding is a technique of converting categorical variables into numerical values so that it could be easily fitted to a machine learning model. For example, ASCII table is character encoding. Gender Male Female Gender 0 1 Color Red Green Blue Color 1 2 3 Each unique category value is assigned an integer value
  50. One-Hot Encoding Remarks Nice Good Great Great_rem Nice_rem Good_rem 0 1 0 0 0 1 1 0 0 The categorical parameters will prepare separate columns for each label
  51. Time to train our model :3
  52. (image slide)
  53. The Holy “Normal Equation” No iterations! :D
  54. Time for just a tiny bit more maths :3 Note that is a vector, and so is y. So, when we multiply one by another, it doesn't matter what the order is (as long as the dimensions work out). So, we can further simplify: It’s derivative:
  55. To minimize J, we set its derivative to be zero and obtain the normal equation. Dimensions of is (n x n). Time complexity for inverting it will be O( ). Hence, it is not advised to use the normal equation if no. of features is too high. Note: Normal equation works only when m > n.
  56. Thanks for coming!
  57. Social Media Follow our social media handles for regular updates Instagra m Twitter YouTube WhatsAp p