In machine learning, classification tasks involve predicting categories or labels. For instance, classifying emails as 'spam' or 'not spam'.
df['Letter_Grade'] = df['Letter_Grade'].replace({'A': 4, 'B': 3, 'C': 2, 'D': 1, 'F': 0})
Label encoding converts categories into numeric values. For example, assigning '1' for 'disease' and '0' for 'no disease'.
df = pd.get_dummies(df, columns=['Accommodation'], dtype=int)
One-hot encoding converts categorical data into binary columns. Each category gets its own column with a '1' for presence and '0' for absence.
One-hot encoding example: For 'Rental', 'Dorm', 'With Family', 'Other', each category is turned into a separate column with '1' or '0'.
The sigmoid function converts a value to a probability between 0 and 1, helping in binary classification tasks.
model = nn.Sequential(nn.Linear(5, 3), nn.ReLU(), nn.Linear(3, 1), nn.Sigmoid())
In binary classification, a threshold helps determine the class based on the probability output. For instance, if the threshold is 0.5, values above it are classified as '1' and below as '0'.
threshold = 0.5 # Adjust the threshold as needed classification = int(probability >= threshold)
This loss function measures how well the model's predicted probabilities match the actual labels in binary classification.
def BCELoss(p, y): if y == 1: # True label is 1 return -np.log(p) else: # True label is 0 return -np.log(1 - p) from torch import nn loss = nn.BCELoss()
Stochastic Gradient Descent (SGD) is an optimization technique used to minimize the loss function and update model weights in PyTorch.
import torch.optim as optim optimizer = optim.SGD(model.parameters(), lr=0.001)
Accuracy is a metric that measures how many predictions were correct out of the total number of predictions.
Accuracy = number_of_correct_predictions / total_number_of_predictions
These metrics help evaluate model performance by measuring true positives, false positives, and false negatives.
from sklearn.metrics import precision_score, recall_score, f1_score precision = precision_score(y_true, y_pred) recall = recall_score(y_true, y_pred) f1 = f1_score(y_true, y_pred)
Softmax converts model outputs into probabilities for each class in multiclass classification tasks. Each class gets a probability score that sums up to 1.
import torch output = torch.nn.Softmax(dim=1)(model_output)
The argmax function selects the class with the highest probability from the softmax output, determining the final classification.
predicted_class = torch.argmax(model_output, dim=1)
Cross-entropy loss measures how well the predicted probabilities match the actual classes in multiclass classification.
from torch import nn criterion = nn.CrossEntropyLoss() loss = criterion(predictions, targets)
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!