Machine Learning and ML Model?

ML is the process of training a ML Model, to make useful predictions or generate content (like text, images, audio, or video) from data.

Types of Machine Learning

Supervised Learning Unsupervised Learning Semisupervised Learning Reinforcement learning Generative AI
What Training data feed to the algorithm includes the desired solutions(called labels).
Example: This is like a student learning new material by studying old exams that contain both questions and answers
Dataset does not have labels. ML model tries to learn without teacher. ML has to estimate whether it's correct/incorrect and create its own rules lot of unlabeled data and a little bit of labeled data.
Examples: 1. FB Photos: When we load photos, we provide labels to few and leave others. AI identifies photos.
Agent(AI Program) can observe the environment, select and perform actions, and get rewards or penalties based on actions performed within an environment
Example: used to train robots to perform tasks, like walking around a room
Models that creates content from user input.
Eg:
Create unique images, music compositions, jokes,
Text-to-text
Text-to-image
Text-to-video
Text-to-code
Text-to-speech
Image and text-to-image
Types 1. Clustering:
2. Visualization and dimensionality reduction
With unlabelled data this algorithm provides ouput which can be plotted on 2-D, 3-D plane. Algorithms used:
  Principal Component Analysis (PCA)
  Kernel PCA
  Locally-Linear Embedding (LLE)
  t-distributed Stochastic Neighbor Embedding (t-SNE)
3. Association rule learning:
This provides output as relations between attributes. Algorithms used:
  Apriori
  Eclat
Algorithms Clustering: k-Means, Hierarchical Cluster Analysis (HCA), Expectation Maximization
Visualization and dimensionality reduction: Principal Component Analysis (PCA), Kernel PCA, Locally-Linear Embedding (LLE), t-distributed Stochastic Neighbor Embedding (t-SNE)
Association rule learning: Apriori, Eclat
Deep belief networks (DBNs)

1. Supervised Learning Models

Algorithms: k-Nearest Neighbors, Linear Regression, Logistic Regression, Support Vector Machines (SVMs), Decision Trees & Random Forests, Neural networks

1.1 Regression (Gives % or numeric value)

Examples: Predict whether based on inputs, Predict price of car provided with some inputs(mileage, age, brand, etc.)Decision Tree Regressor
Logistic Regression: Mix of classfication & Regression. Example: 20% of chances being a spam.

1.1.1 Linear Regression = Linear Equation

linear regression finds the relationship between features(Given value = x) and a label(To be predicted value = y), ie finding the best-fit line
Linear equation in n variables is called 1 Neuron

Understand linear equation in Mathematics
Linear Regression

y = mx + b    // x(Feature/Given value), y(Label/To be predicted value)
m(slope) = y2-y1 / x2-x1
         = 5-0 / 0 - 7 = -5/7

b is y intercept. ie point where line intersect y axis = 5

Equation: y = -5/7x + 5.
            = 7y + 5x = 35   //(0,5) (7,0) both satify the equation
      
In Machine Learning(Model with 1 feature(x))

y = wx + b
    w(weight), b(Bias)
                

Linear Regression
In Machine Learning(Model with many features)

y = w1x1 + w2x2 + w3x3 ..+ b
    w(weight), b(Bias)
                

Linear Regression

Loss in linear regression

Purpose of linear regression: Create the Line

You are given many (x, y) points, but you do NOT know the line, we need to create the line, so that in future if we provide x(feature) value we get y(label) prediction output
Example: Suppose House prices are given, but we donot know the equation. In future when some house size would be given we want to calculate price, that means we should know the equation. In order to know the equation we should know slope, gradient desent

| Size (x)  | Price (y) |
| --------- | --------- |
| 1000 sqft | 2 Cr      |   (1000,2)
| 1200 sqft | 2.4 Cr    |   (1200,2.4)
| 1500 sqft | 3 Cr      |   (1500,3)
| 1800 sqft | 3.6 Cr    |   (1800,3.6)

Prediction from Model: if x=1700, y=?

Gradient Desent

This is the technique to find weight(w), bias(b) iteratively with the lowest loss, and finally creating the linear equation.
Steps of Gradient Decent

w = 0.1, b = 50     // 1. Start with random values of w,b.
y = .1x + 50
y = .1x1000+50=150  // Put x=1000, y=150. 
  loss = y-actual_value
  if (loss > 5) { // Suppose
    // Determine the direction to move the weights and bias that reduce loss.
    // Repeat
  }

Types of Gradient Desent
1. Vanishing Gradients: During backpropagation, the gradients become extremely small, so the earlier layers of the neural network learn extremely slowly or almost stop learning. When the gradient values approach 0 for the lower layers, the gradients are said to "vanish". The ReLU activation function can help prevent vanishing gradients.
2. Exploding Gradients: During backpropagation, the gradients become extremely large. Batch normalization can help prevent exploding gradients

Hyperparameters

These are variables that control different aspects of training.
So m,b are parameters which need to be calculated during training, but hyperparameters define how m,b are derived
1. Learning rate: This determines magnitude of the changes to make to the weights and bias during each step of the gradient descent process for model to converge quickly. For example, if the gradient's magnitude is 2.5 and the learning rate is 0.01, then the model will change the parameter by 0.025.
  If the learning rate is too low, the model can take a long time to converge.
  if the learning rate is too high, the model never converges, but bounces around the weights and bias that minimize the loss.   Pick a learning rate that's not too high nor too low so that the model converges quickly.
2. Batch size: number of examples the model processes before updating its weights and bias
a. Stochastic gradient descent(SGD): Uses single example (a batch size of one) per iteration to adjust m, b.
  b. Mini-batch stochastic gradient descent(mini-batch SGD): Compromise between full-batch and SGD.
3. Epochs

1.1.2 Logistic Regression = Sigmoid function (Find Probability)

Sigmoid/S Shaped Function

Value of function will always remain between 0 and 1, making it good fit for probability
e = 2.718(Euler's number)
Sigmoid function
Sigmoid translates linear regression to a probability
Example: Given number of hours student studied, can model predict if student can pass?
Hours Studied -> Linear Equation(z= wx+b) -> Sigmoid -> Probability

z = 2x − 5       //x=Hours Studied. z can be any value
x      z      Sigmoid
5	     5      1/1+e(pow,-5) = .993 (99.3% chance of passing.)
100   195     1/1+e(pow,-100) = 

Other Examples:
- Predict whether mail is spam or not

Loss in Sigmoid is Log Loss

1.2 Classification (Gives yes/no or enums)

Example: predict if an email is spam or if a photo contains a cat

1.2.1 Binary classification (true or false)

models output a value from a class that contains only two values, for example, a model that outputs either rain or no rain

1.2.2 Multiclass classification (enums)

models output a value from a class that contains more than two values, for example, a model that can output either rain, hail, snow, or sleet.

1.1.2 Decision Tree

This is Binary Tree(not balanced) which works in flowchart-like manner where each internal node represents a "test" on an attribute, each branch represents the outcome of the test, and each leaf node represents numerical value (ie Prediction).
How it works? Predicting Whether a Customer Will Buy a Product Using Two Decision Trees
Decision Tree If Tree 1 predicts "Purchase" and Tree 2 predicts "No Purchase", the final prediction might be "Purchase" or "No Purchase" depending on the weight or confidence assigned to each tree.
Information Gain in Decision Tree
  Information Gain tells us how useful a question (or feature) is for splitting data into groups
  It measures how much the uncertainty decreases after the split.
  For example if we split a dataset of people into "Young" and "Old" based on age and all young people bought the product while all old people did not, the Information Gain would be high
Gini Index in Decision Tree: Metric to measure how often a randomly chosen element would be incorrectly identified. Lower Gini index represents a better split

Fitting
Overfitting in Decision Tree
when tree has too many splits becomes too deep, and it makes very good predictions to training data, but does poor predictions on new data.
As the tree gets deeper, the dataset gets sliced up into leaves with fewer choices. we'll have 210 groups by the time we get to the 10th level. That's 1024 leaves.
When we divide the tree in too many leaves, we also have fewer data in each leaf. Very few Leaves will make predictions that are quite close to actual values, but they may make very unreliable predictions for new data (because each prediction is based on only a few input samples).
Underfitting in Decision Tree
On otherside, if only few splits are performed(only 2 or 4), Resulting predictions may be far off for most input samples, even in the training data.
When a model fails to capture important distinctions and patterns in the data, & perform poorly even in training data, poorly in new data is underfitting
Overfitting or underfitting(What's preferred)? We need to hit a sweet spot between 2

1.1.2 Random Forest

Problems with Decision Tree:   Overfitting(Deep tree with lot of leaves) or underfitting(very less depth)   Random forest solves it

What is Random Forest?
  Random forest uses lot of decision trees & make prediction by averaging each of Component trees.
  Has much better predictive accuracy than a single decision tree & works well with default params

2. Unsupervised Learning

2.1 Clustering

Clustering differs from classification because the categories aren't defined by you. For example, an unsupervised model might cluster a weather dataset based on temperature.
Algorithms used: k-Means, Hierarchical Cluster Analysis (HCA), Expectation Maximization

trie