Meligy / ML-DL_cheatsheet

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Machine Learning and Deep Learning Cheatsheet

Table of Contents:

  1. Introduction to Machine Learning 1.1 Definition of Machine Learning 1.2 Types of Machine Learning 1.3 Key Terminology

  2. Machine Learning Algorithms 2.1 Linear Regression 2.2 Logistic Regression 2.3 Decision Trees 2.4 Random Forests 2.5 Support Vector Machines (SVM) 2.6 Naive Bayes 2.7 K-Nearest Neighbors (KNN)

  3. Deep Learning 3.1 Introduction to Deep Learning 3.2 Artificial Neural Networks (ANN) 3.3 Convolutional Neural Networks (CNN) 3.4 Recurrent Neural Networks (RNN) 3.5 Long Short-Term Memory (LSTM) 3.6 Generative Adversarial Networks (GAN) 3.7 Transfer Learning

  4. Evaluation Metrics 4.1 Classification Accuracy 4.2 Logarithmic Loss 4.3 Confusion Matrix 4.4 Area Under Curve (AUC) 4.5 F1 Score 4.6 Mean Absolute Error (MAE) 4.7 Mean Squared Error (MSE) 4.8 Inlier Ratio Metric

  5. Data Preprocessing 5.1 Data Cleaning 5.2 Vectorization 5.3 Normalization 5.4 Handling Missing Values

  6. Feature Engineering 6.1 Definition of Feature Engineering 6.2 Importance of Feature Engineering 6.3 Feature Engineering Techniques for Machine Learning 6.4 Best tools for Feature Engineering

  7. Model Evaluation and Validation 7.1 Train-Test Split 7.2 Cross-Validation 7.3 Hyperparameter Tuning 7.4 Overfitting and Underfitting 7.5 Bias-Variance Tradeoff

  8. Model Deployment and Productionization 8.1 Saving and Loading Models 8.2 Model Deployment Options (APIs, Web Applications) 8.3 Monitoring and Updating Models

Introduction to Machine Learning

Machine Learning (ML) is a subfield of artificial intelligence (AI) that focuses on developing algorithms and models enabling computers to learn from data and make predictions or decisions. ML aims to allow computers to learn and improve their performance on specific tasks without being explicitly programmed.

Definition of Machine Learning

Machine Learning is a scientific discipline that involves the development of algorithms and statistical models that enable computers to learn patterns and make predictions or decisions based on data without being explicitly programmed. The core idea of ML is to use data-driven approaches for problem-solving and decision-making.

Types of Machine Learning

  1. Supervised Learning: In supervised learning, the ML algorithms are trained on labeled datasets, where the input data is paired with corresponding output labels. The algorithm learns to map input to output to make predictions on new, unseen data. Supervised learning is used for tasks like classification and regression. Popular algorithms include Linear Regression, Decision Trees, and Support Vector Machines (SVM).

  2. Unsupervised Learning: Unsupervised learning algorithms are trained on unlabeled datasets, where there are no output labels. The goal is to find patterns or relationships in the data without explicit guidance. Unsupervised learning is used for tasks like clustering, anomaly detection, and dimensionality reduction. Popular algorithms include K-Means, Hierarchical Clustering, and Principal Component Analysis (PCA).

  3. Reinforcement Learning: Reinforcement learning is a type of ML where an agent interacts with an environment and receives feedback (rewards or penalties) based on its actions. The agent learns to maximize cumulative rewards over time through a trial-and-error process. Reinforcement learning is used for tasks like game playing, robotics, and autonomous systems. Popular algorithms include Q-Learning, Deep Q Networks (DQNs), and Proximal Policy Optimization (PPO).

Key Terminology

  • Features: In the context of ML, features refer to the input variables or attributes used to make predictions or classifications. For example, in a spam email classifier, features could include the frequency of certain words or the presence of specific patterns in the email content.

  • Labels: Labels, also known as target variables, are the known output values in supervised learning. During training, the model learns to associate features with corresponding labels. For example, in a dataset of images of cats and dogs, the labels would be "cat" or "dog" for each image.

  • Training: Training is the process of feeding data to the ML algorithm to learn patterns and update its internal parameters. During training, the algorithm adjusts its internal parameters (weights and biases) to minimize the difference between its predicted outputs and the true labels.

  • Testing: Testing is the phase where the trained model's performance is evaluated on new, unseen data. The model's ability to generalize to unseen data is assessed during testing. The testing data should be separate from the training data to ensure an unbiased evaluation.

Deep Learning

Deep Learning is a subfield of machine learning that focuses on learning hierarchical representations from data using artificial neural networks. Deep Learning has gained popularity due to its ability to automatically learn intricate patterns and features from large-scale data.

Introduction to Deep Learning

Deep Learning leverages large neural networks with multiple layers (deep neural networks) to learn hierarchical representations of data. Each layer in the neural network processes the data at different levels of abstraction, allowing the model to learn complex features and patterns.

Deep Learning is particularly effective for tasks that involve large amounts of data and complex relationships between features. It has achieved significant breakthroughs in computer vision, natural language processing, and speech recognition.

Artificial Neural Networks (ANN)

Artificial Neural Networks are computational models inspired by the structure and function of biological neural networks in the human brain. ANNs consist of interconnected nodes (neurons) organized in layers. Each neuron takes input, processes it through an activation function, and produces an output that is passed to other neurons in subsequent layers.

The connections between neurons are weighted, and during training, the model learns the optimal weights that allow it to make accurate predictions on the training data.

ANNs are the building blocks of deep learning models. They are widely used for tasks like image recognition, natural language processing, and reinforcement learning.

Convolutional Neural Networks (CNN)

Convolutional Neural Networks are a specialized type of deep neural network designed for image and video processing tasks. CNNs use convolutional and pooling layers to automatically extract relevant features from images.

The convolutional layers apply filters (kernels) to the input images, capturing local patterns and features. The pooling layers downsample the output of the convolutional layers, reducing the spatial dimensions and focusing on essential information.

CNNs are particularly effective for tasks like image classification, object detection, and image segmentation.

Recurrent Neural Networks (RNN)

Recurrent Neural Networks are designed to process sequential data, such as text or time series. Unlike feedforward neural networks (where the data flows in one direction from input to output), RNNs have loops that allow information to persist across time steps.

RNNs are well-suited for tasks that involve sequences, such as natural language processing and speech recognition. The ability to capture temporal dependencies makes RNNs suitable for tasks where the order of data points matters.

However, standard RNNs suffer from the vanishing gradient problem, which makes it challenging to capture long-range dependencies in sequences.

Long Short-Term Memory (LSTM)

Long Short-Term Memory (LSTM) is a variant of RNNs designed to address the vanishing gradient problem. LSTMs have memory cells and gating mechanisms that allow them to capture and retain information for long periods.

LSTMs are widely used in natural language processing tasks, sentiment analysis, machine translation, and speech recognition, where the ability to capture long-range dependencies is crucial.

Generative Adversarial Networks (GAN)

Generative Adversarial Networks (GANs) consist of two neural networks: a generator and a discriminator. GANs are used for generating synthetic data that resembles real data.

The generator tries to generate realistic data, while the discriminator tries to distinguish between real and generated data. The two networks are trained together in a competitive game, where the generator improves its ability to generate realistic data, and the discriminator improves its ability to differentiate between real and generated data.

GANs are used for tasks like image-to-image translation, style transfer, and data augmentation.

Transfer Learning

Transfer Learning is a technique that leverages knowledge from pre-trained models on large datasets to improve performance on smaller datasets or new tasks. Instead of training a model from scratch on the new data, transfer learning uses the learned representations from the pre-trained model as a starting point.

There are two main approaches to transfer learning:

  • Feature Extraction: In this approach, the pre-trained model's weights are frozen, and the output layers are replaced to match the new task. The pre-trained model acts as a feature extractor, and only the newly added layers are trained on the new data.

  • Fine-Tuning: Fine-tuning involves unfreezing some of the pre-trained model's layers and training them on the new data. Fine-tuning allows the model to adapt to the specific characteristics of the new data while leveraging the knowledge from the pre-trained model.

Transfer learning is particularly useful when the new task has limited labeled data or when the pre-trained model is trained on a related task.

Evaluation Metrics

Evaluation metrics are used to assess the performance of machine learning models.

Classification Accuracy

Classification Accuracy is the proportion of correct predictions out of the total predictions made by the model. It is a simple and intuitive metric for binary classification tasks.

$$ \text{Accuracy} = \frac{\text{Number of Correct Predictions}}{\text{Total Number of Predictions}} $$

Classification accuracy can be misleading, especially when dealing with imbalanced datasets, where one class is significantly more prevalent than others.

Logarithmic Loss

Logarithmic Loss (Log Loss) is used to evaluate the performance of classification models that output probabilities. It penalizes models for being confident but wrong.

Log Loss is a measure of how far the model's predicted probabilities are from the true class labels. It is commonly used for multi-class classification problems.

Confusion Matrix

The Confusion Matrix is a table used to evaluate the performance of a classification model by comparing predicted and actual class labels. It provides valuable insights into the model's performance for each class.

The Confusion Matrix contains four components:

  • True Positives (TP): The number of instances that are correctly predicted as positive (correctly classified as the positive class).

  • True Negatives (TN): The number of instances that are correctly predicted as negative (correctly classified as the negative class).

  • False Positives (FP): The number of instances that are incorrectly predicted as positive (incorrectly classified as the positive class).

  • False Negatives (FN): The number of instances that are incorrectly predicted as negative (incorrectly classified as the negative class).

Using the components of the Confusion Matrix, various evaluation metrics can be derived, such as Precision, Recall, F1 Score, and Specificity.

Area Under Curve (AUC)

The Area Under the Receiver Operating Characteristic Curve (ROC AUC) is a metric used to visualize the tradeoff between the true positive rate and the false positive rate for different classification thresholds. AUC provides an aggregate measure of the model's ability to discriminate between classes.

The ROC curve plots the true positive rate (sensitivity or recall) against the false positive rate (1-specificity) for various classification thresholds. The AUC represents the area under the ROC curve, with values ranging from 0 to 1. A perfect classifier has an AUC of 1, while a random classifier has an AUC of 0.5.

F1 Score

The F1 Score is the harmonic mean of precision and recall, making it useful when there is an uneven class distribution. It provides a balanced measure of the model's performance on both positive and negative classes.

The F1 Score is given by:

$$ F1 Score = 2 \cdot \frac{\text{Precision} \cdot \text{Recall}}{\text{Precision} + \text{Recall}} $$

The F1 Score ranges from 0 to 1, with higher values indicating better model performance.

Mean Absolute Error (MAE)

Mean Absolute Error measures the average absolute difference between predicted and actual values. It is commonly used in regression tasks.

MAE is given by:

$$ \text{MAE} = \frac{1}{n} \sum_{i=1}^{n} | \text{predicted}_i - \text{actual}_i | $$

Mean Squared Error (MSE)

Mean Squared Error measures the average squared difference between predicted and actual values. It is also used in regression tasks.

MSE is given by:

$$ \text{MSE} = \frac{1}{n} \sum_{i=1}^{n} ( \text{predicted}_i - \text{actual}_i )^2 $$

Inlier Ratio Metric

The Inlier Ratio Metric is used to evaluate anomaly detection models. It calculates the percentage of data points classified as inliers, i.e., normal instances, by the model.

The Inlier Ratio is useful for assessing the model's ability to correctly identify normal instances and detect anomalies or outliers.

Data Preprocessing

Data Preprocessing involves preparing the data before feeding it to the machine learning model.

Data Cleaning

Data Cleaning is the process of identifying and handling errors, missing values, and inconsistencies in the dataset. Cleaning the data ensures that the model is not biased by irrelevant or erroneous information.

Data cleaning steps include:

  • Handling missing values: Replacing missing values with a suitable value, such as mean, median, or mode.

  • Removing duplicates: Removing identical or nearly identical data entries from the dataset.

  • Handling outliers: Identifying and dealing with data points that significantly deviate from the majority of the data.

  • Rescaling features: Normalizing or standardizing features to ensure they have similar scales.

Vectorization

Vectorization is the process of converting non-numeric data, such as text or categorical variables, into numerical form for model compatibility. Many ML algorithms require numerical inputs, and vectorization enables the use of categorical or textual data in the model.

Common techniques for vectorization include one-hot encoding, where each category is converted to a binary vector, and word embeddings, which represent words as dense numerical vectors.

Normalization

Normalization is the scaling of features to ensure they have similar scales. It is essential for algorithms that rely on distance metrics, such as k-Nearest Neighbors, Support Vector Machines, and Neural Networks. Normalization prevents features with larger scales from dominating the model's learning process.

Common normalization techniques include Min-Max scaling, where feature values are scaled to a specified range (usually [0, 1]), and Z-score normalization, where feature values are scaled to have a mean of 0 and a standard deviation of 1.

Handling Missing Values

Handling Missing Values involves strategies to deal with data points that have missing values. Missing data can negatively impact model training and performance.

Common approaches for handling missing values include:

  • Imputation: Replacing missing values with estimated values based on statistical measures like mean, median, or mode.

  • Deletion: Removing data points with missing values from the dataset.

  • Advanced imputation: Using more sophisticated techniques like regression or k-Nearest Neighbors to predict missing values based on the available data.

It is essential to choose the appropriate method based on the nature of the missing data and the specific use case.

Feature Engineering

Feature Engineering involves creating new features or transforming existing features to improve model performance.

Definition of Feature Engineering

Feature Engineering is the process of selecting, creating, or transforming features to make them more suitable for modeling. It involves leveraging domain knowledge, data analysis, and experimentation to extract meaningful information from the data.

Feature engineering plays a crucial role in the success of machine learning models. Well-engineered features can significantly improve model performance by providing more relevant and informative inputs to the model.

Importance of Feature Engineering

Feature engineering is critical because the quality and relevance of the features directly impact the model's ability to learn and make accurate predictions. By engineering relevant features, you can provide the model with the most critical information for decision-making.

Feature engineering also helps in dimensionality reduction, removing redundant or irrelevant features, and improving model training time and efficiency.

Feature Engineering Techniques for Machine Learning

There are various techniques for feature engineering, including:

  • Statistical Aggregation: Creating new features by aggregating statistics (e.g., mean, median, standard deviation) across different groups or time periods. For example, if we have transaction data, we can aggregate the transactions by customer to create features like total spending, average spending, etc.

  • Binning: Converting continuous numeric features into categorical features by grouping them into bins or intervals. For example, we can bin age data into age groups (e.g., 0-10, 11-20, 21-30, etc.).

  • Encoding Categorical Variables: Transforming categorical variables into numerical representations that can be processed by ML algorithms. Common encoding techniques include one-hot encoding, where each category is represented as a binary vector, and label encoding, where each category is replaced with a numerical label.

  • Interaction Features: Creating new features by combining existing features, allowing the model to capture interactions between variables. For example, if we have height and weight features, we can create an interaction feature by multiplying height and weight to capture the relationship between the two.

  • Text Feature Extraction: Extracting meaningful information from text data using techniques like bag-of-words, TF-IDF (Term Frequency-Inverse Document Frequency), and word embeddings. Text data is often transformed into numerical representations for modeling.

  • Domain-Specific Feature Engineering: Incorporating domain knowledge and understanding of the problem to engineer relevant features. Domain-specific features can significantly improve model performance, especially in specialized domains.

Best tools for Feature Engineering

Python libraries like Pandas and Scikit-learn offer powerful tools for feature engineering tasks. Pandas provides a wide range of data manipulation functionalities, making it easy to preprocess and engineer features. Scikit-learn offers feature selection and transformation methods, such as Principal Component Analysis (PCA) and Recursive Feature Elimination (RFE).

Model Evaluation and Validation

Model Evaluation and Validation are crucial steps to assess the performance and generalization ability of the trained models.

Train-Test Split

The Train-Test Split involves dividing the dataset into training and testing subsets. The model is trained on the training data and evaluated on the testing data to assess its performance on unseen data.

The train-test split helps in estimating how well the model is likely to perform on new, unseen data. It is essential to use separate datasets for training and testing to avoid overfitting, where the model memorizes the training data but fails to generalize to new data.

Cross-Validation

Cross-Validation is a technique used to evaluate model performance by dividing the data into multiple subsets (folds) and performing several training and testing cycles.

The data is divided into k subsets, and the model is trained k times, each time using a different fold as the testing set and the remaining k-1 folds as the training set. Cross-validation helps in obtaining a more reliable estimate of the model's performance, as it evaluates the model on different subsets of data.

Cross-validation is especially useful when the dataset is limited, and it helps prevent overfitting during model evaluation.

Hyperparameter Tuning

Hyperparameter Tuning involves adjusting model hyperparameters to optimize performance. Hyperparameters are model settings that are not learned during training but set before the training process.

Common hyperparameters include learning rate, batch size, number of layers, and number of units in each layer. Finding the optimal hyperparameter values can significantly impact model performance.

Grid search and random search are popular techniques used for hyperparameter tuning, where different combinations of hyperparameter values are evaluated.

Overfitting and Underfitting

Overfitting occurs when the model learns to perform well on the training data but fails to generalize to new data. It happens when the model is too complex and captures noise or random fluctuations in the training data. Overfitting can be detected when the model's performance on the training data is much higher than on the testing data.

Underfitting, on the other hand, occurs when the model is too simple to capture the underlying patterns in the data. It results in poor performance on both the training and testing data.

To mitigate overfitting, techniques like regularization, early stopping, and reducing model complexity can be used. To address underfitting, increasing model complexity, adding more layers or units, or using more sophisticated algorithms may be necessary.

Bias-Variance Tradeoff

The Bias-Variance Tradeoff refers to the balance between a model's ability to capture underlying patterns (low bias) and its sensitivity to fluctuations or noise in the training data (low variance).

  • A model with high bias (underfitting) fails to capture the true relationship between the features and the target and performs poorly on both training and testing data.

  • A model with high variance (overfitting) fits the training data too closely and performs well on training data but poorly on unseen data.

The goal is to find the right level of model complexity that minimizes both bias and variance. This can be achieved through appropriate feature engineering, regularization, and hyperparameter tuning.

Model Deployment and Productionization

Model Deployment involves making trained models accessible and usable in real-world applications.

Saving and Loading Models

Once a model is trained, it can be saved to disk for future use. Saving a model allows you to reuse the trained parameters without retraining the model from scratch each time it is needed.

In Python, libraries like Pickle or joblib are commonly used to save and load models. Saving the model also makes it possible to share the trained model with others or deploy it to production environments.

Model Deployment Options (APIs, Web Applications)

Trained models can be deployed as APIs or web applications, making it easy to integrate their predictions into real-time systems or web services.

For example, you can create a RESTful API using frameworks like Flask or FastAPI to serve the model's predictions on new data. Web applications can also be built to allow users to interact with the model through a graphical user interface.

Monitoring and Updating Models

Once a model is deployed, it's crucial to continuously monitor its performance and update it as new data becomes available or model performance changes. Models in production may experience drift over time due to changes in the data distribution or other external factors.

Monitoring helps detect changes in model performance and identify potential issues. Regular updates and retraining help ensure that the model remains accurate and relevant as the data distribution evolves over time.

This comprehensive cheatsheet covers essential topics in Machine Learning and Deep Learning, providing a solid foundation for learning and applying these techniques in various real-world scenarios.

About