def reverseWords(s: str) -> str:
words = s.split()
return ' '.join(word[::-1] for word in words)
def reverseWords(s: str) -> str:
words = s.split()
return ' '.join(word[::-1] for word in words)
Socket Programming - Youtube
Socket Address
A socket is one endpoint of a two
way communication link between two programs running on the network.
The socket mechanism provides a means of inter-process communication (IPC) by
establishing named contact points between which the communication take
place.
sockets is created using ‘socket’ system
call. The socket provides bidirectional FIFO Communication
facility over the network. A socket connecting to the network is created at
each end of the communication. Each socket has a specific address. This address
is composed of an IP address and a port number.
The server creates a socket, attaches it to a network port
addresses then waits for the client to contact it. The client creates a socket
and then attempts to connect to the server socket. When the connection is
established, transfer of data takes place.
Types of Sockets : There are two types of
Sockets: the datagram socket and the stream socket.
Sockets: A Comprehensive Overview
1. Socket Address Structures
struct sockaddr_in
: For IPv4 addresses.struct sockaddr_in6
: For IPv6 addresses.struct sockaddr_un
: For Unix domain sockets.sin_family
: Address family (AF_INET, AF_INET6, AF_UNIX).sin_port
: Port number in network byte order.sin_addr
: IP address in network byte order.bind
, connect
, and listen
to specify the address of the socket.2. Value-Result Arguments
accept
function takes a sockaddr
structure as an argument. After accepting a connection, the function fills the structure with the address of the client and returns it to the caller.3. Byte Ordering and Manipulation Functions
htons
: Converts a 16-bit host value to network byte order.htonl
: Converts a 32-bit host value to network byte order.ntohs
: Converts a 16-bit network value to host byte order.ntohl
: Converts a 32-bit network value to host byte order.4. Elementary TCP Sockets
socket(domain, type, protocol)
connect(sockfd, addr, addrlen)
bind(sockfd, addr, addrlen)
listen(sockfd, backlog)
accept(sockfd, addr, addrlen)
fork()
execve(pathname, argv, envp)
fork
or select
mechanisms.5. Close Function and Related Functions
close(sockfd)
shutdown(sockfd, how)
Classification
Classification is a Supervised Learning algorithm where the goal is to predict the category or class labelof given data points. It is used when the output variable is categorical .
How Classification Works?
1. Training Phase:
A model learns the relationship b/w the input features and their corresponding class labels from a labeled dataset.
2. Testing Phase:
The trained model is used to classify new, unseen data points into one of the predefined classes.
Types of Classification:
Binary Classification
Multi class classification
Multi label classification
Examples of Classification Tasks:
Email filtering
Medical diagnosis
Image recognition
sentiment Analysis
Classification Algorithms
Logistic Regression
Decision Trees
Support Vector Machines
K-Nearest Neighbours
Navie bayes
Random Forest
Evalution Metrics for Classification
Accuracy - Percentage of correctly labeled classes
Precision - How many +ve predictions are correct
Recall
F1 Score
ROC-AUC
Workflow of Classification
Data Collection: Gather labeled data.A Decision Tree is a supervised machine learning algorithm used for classification and regression tasks. It models decisions and their possible consequences as a tree-like structure of nodes, where:
The tree splits the data into subsets based on the feature that provides the most information gain or least impurity at each step.
Root Node:
The starting point of the tree, representing the entire dataset.
Splitting:
Dividing data at a node into subsets based on a condition (e.g., feature > value
).
Leaf Node:
A terminal node that provides the final prediction.
Pruning:
Reducing the size of the tree to avoid overfitting.
Impurity Measures (used for splitting):
Bayes' Theorem is a fundamental concept in probability theory and statistics that describes the probability of an event, based on prior knowledge of conditions that might be related to the event. In machine learning, it is widely used for classification tasks, particularly in Naive Bayes classifiers.
Bayes' Theorem is expressed mathematically as:
Where:
Prior Probability (P(A)): This represents what is known about A before any new data is observed. It’s the initial belief or assumption about a class or event.
Likelihood (P(B∣A)): This is the likelihood of observing the evidence B given that A is true. It quantifies how well the evidence supports the hypothesis.
Posterior Probability (P(A∣B)): This is the probability of A occurring after considering the evidence B. It's the updated belief after observing the data.
Evidence (P(B)): This is the total probability of observing B across all possible outcomes. It is used to normalize the result to ensure the probabilities sum to 1.
In machine learning, the Naive Bayes classifier is based on Bayes' Theorem, with the assumption that the features used for prediction are conditionally independent given the class label. This simplifies the calculation, hence the term "naive."
Compute the Prior Probability:
The probability of each class occurring in the dataset.
Compute the Likelihood:
Calculate the likelihood for each feature given the class label. For continuous features, this often assumes a Gaussian distribution.
Compute the Posterior Probability:
For each class Ck, compute the posterior probability of a new data point X=(X1,X2,...,Xn):
Classify the Data Point:
Choose the class Ck with the highest posterior probability as the predicted class.
Class imbalance occurs when the number of samples in one class significantly exceeds those in another class. This can negatively impact the performance of Support Vector Machines (SVM), as the decision boundary may be biased towards the majority class, leading to poor performance on the minority class.
Class Weight Adjustment:
Assign higher weights to the minority class to balance the penalty during training.
In Scikit-learn, this can be done using the class_weight
parameter:
from sklearn.svm import SVC
# Assign 'balanced' to automatically compute weights
model = SVC(class_weight='balanced')
Or manually specify weights:
model = SVC(class_weight={0: 1, 1: 10}) # Higher weight for class 1 (minority)
Resampling Techniques:
Example using SMOTE:
from imblearn.over_sampling import SMOTE
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
# Create synthetic samples for the minority class
smote = SMOTE(random_state=42)
X_resampled, y_resampled = smote.fit_resample(X, y)
# Train an SVM on the balanced dataset
model = SVC()
model.fit(X_resampled, y_resampled)
Change the Decision Threshold: Adjust the threshold for classifying a sample as a minority class based on the predicted probabilities.
Example:
from sklearn.svm import SVC
from sklearn.metrics import classification_report
# Fit SVM
model = SVC(probability=True)
model.fit(X_train, y_train)
# Predict probabilities
y_prob = model.predict_proba(X_test)[:, 1]
# Adjust threshold
threshold = 0.3
y_pred = (y_prob >= threshold).astype(int)
# Evaluate performance
print(classification_report(y_test, y_pred))
Kernel Selection: Use kernels that better separate the minority and majority classes, such as the RBF kernel, which can handle non-linear boundaries.
Feature Engineering: Transform or create features that emphasize differences between the classes. This can improve separability in the feature space.
Use of Alternative Metrics: Evaluate the model using metrics like:
Random Forest is a popular ensemble learning technique used for both classification and regression tasks. It combines the predictions of multiple decision trees to improve accuracy, reduce overfitting, and enhance generalization.
Bootstrap Aggregation (Bagging):
Feature Randomness:
Prediction Aggregation:
BOX PLOT
A boxplot (or box-whisker plot) is a graphical representation of the distribution of the dataset.
Components:
Minimum(Q0): The smallest data point.
First Quartile(Q1): The median of the lower half of the dataset(25% Percentile)
Median(Q2): The middle value of the dataset(50% percentile)
Third Quartile(Q3)
Maximum(Q4):
def reverseWords(s: str) -> str: words = s.split() return ' '.join(word[::-1] for word in words)