Module_5
Module_5
Algorithms
1. Support Vector Machine (SVM)
Definition:
Key Points:
Code Implementation:
# Import libraries
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, confusion_matrix
# Load dataset
iris = datasets.load_iris()
X = iris.data[:, :2] # Only first two features for simplicity
y = iris.target
# Split dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42)
# Train model
svm_model = SVC(kernel='linear', C=1.0)
svm_model.fit(X_train, y_train)
# Predict
y_pred = svm_model.predict(X_test)
# Evaluate
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
Definition:
KNN is a lazy learning algorithm that classifies a new data point based on the
majority class of its kk nearest neighbors.
Key Points:
Code Implementation:
from sklearn.neighbors import KNeighborsClassifier
# Train model
knn_model = KNeighborsClassifier(n_neighbors=3)
knn_model.fit(X_train, y_train)
# Predict
y_pred = knn_model.predict(X_test)
# Evaluate
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
Definition:
Code Implementation:
from sklearn.naive_bayes import GaussianNB
# Train model
nb_model = GaussianNB()
nb_model.fit(X_train, y_train)
# Predict
y_pred = nb_model.predict(X_test)
# Evaluate
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
Definition:
A Decision Tree splits data into subsets based on feature values, creating a tree-
like structure to make decisions.
Key Points:
Code Implementation:
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import plot_tree
import matplotlib.pyplot as plt
# Train model
dt_model = DecisionTreeClassifier(criterion='gini', random_state=42) #
Change to 'entropy' for ID3
dt_model.fit(X_train, y_train)
# Predict
y_pred = dt_model.predict(X_test)
# Evaluate
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
5. Ensemble Learning
Definition:
Ensemble learning combines multiple models to improve performance. Two
common techniques:
# Train model
rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
rf_model.fit(X_train, y_train)
# Predict
y_pred = rf_model.predict(X_test)
# Evaluate
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
1. Confusion Matrix:
• Displays the counts of true positives, true negatives, false positives, and
false negatives.
2. Accuracy:
3. Precision:
4.
Recall (Sensitivity):
5. F1-Score: