0% found this document useful (0 votes)
5 views17 pages

ML Lab Final QP

The document outlines a final examination for a machine learning lab, covering various topics such as computing central tendency measures, data pre-processing techniques, and implementing algorithms like KNN, Random Forest, Decision Trees, SVM, and Logistic Regression. It includes example datasets and code snippets for each task, demonstrating how to apply these techniques using Python libraries. The examination aims to assess the understanding and application of machine learning concepts and methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

ML Lab Final QP

The document outlines a final examination for a machine learning lab, covering various topics such as computing central tendency measures, data pre-processing techniques, and implementing algorithms like KNN, Random Forest, Decision Trees, SVM, and Logistic Regression. It includes example datasets and code snippets for each task, demonstrating how to apply these techniques using Python libraries. The examination aims to assess the understanding and application of machine learning concepts and methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ML LAB FINAL EXAMINATION

1. Compute Central Tendency Measures: Mean, Median, Mode Measure of Dispersion: Variance,

Standard Deviation for given data. data = [12, 1.8, 22, 5, 14, 35, 44, 55,7.2]

2. Apply the following Pre-processing techniques for a given dataset.

a. Attribute selection b. Handling Missing Values c. Discretization d. Elimination of Outliers

data = {

'Age': [25, 30, [Link], 35, 40, 80, 23],

'Salary': [400, 600, 550, [Link], 700, 800, 120],

'Education': [1, 2, 1, 3, 2, 3, 1],

'Target': [0, 1, 0, 1, 1, 0, 0]

3. Apply KNN algorithm for regression , let k=2, determine the performance of KNN algorithm.

data = {

"Age": [25, 20, 25, 30, 35, 40, 40, 31, 24, 33],

"Experience": [5, 1, 3, 4, 6, 8, 10, 12, 6, 8],

"Salary": [25, 18, 22, 28, 35, 42, 48, 50, 30, 38]

4. Apply Random Forest algorithm for classification, use library to import dataset, from
[Link] import load_iris.
5. Demonstrate decision tree algorithm for a regression problem for the given dataset.

data = {

"Age": [25, 20, 25, 30, 35, 40, 40, 31, 24, 33],

"Experience": [5, 1, 3, 4, 6, 8, 10, 12, 6, 8],

"Salary": [25, 18, 22, 28, 35, 42, 48, 50, 30, 38]

6. Apply Support for Vector algorithm classification, use library to import dataset, from
[Link] import load_iris. Assign species name to this new sample [7.1, 2.0, 6.4, 0.5] .
7. Apply Logistic regression algorithm for a classification problem use library to import dataset, Assign
species name to this new sample [5.1, 2.0, 6.4, 0.7].

8. Implement the K-means algorithm and Evaluate performance . And Test the performance of
the algorithm as a function of the parameters K. use library to import dataset, from [Link]
import load_iris.

9. Demonstrate the use of Fuzzy C-Means Clustering.


10. Demonstrate the use of Expectation Maximization based clustering algorithm

def compute_statistics(data):

Q1. Compute Central Tendency Measures: Mean, Median, Mode Measure of Dispersion:
Variance, Standard Deviation for given data.
def compute_statistics(data):

mean = sum(data) / len(data)

#Median

sorted_data = sorted(data)

n = len(data)

if n % 2 == 0:

median = (sorted_data[n // 2 - 1] + sorted_data[n // 2]) / 2

else:

median = sorted_data[n // 2 ]

# Mode

frequency = { }

for num in data:

frequency[num] = [Link] (num,0) + 1

max_freq = max([Link]())

mode = [key for key, val in [Link]() if val == max_freq]

if len(mode) > 1: mode = "No unique mode found" # Multiple modes

# Variance

mean_diff_squared = [(x - mean) ** 2 for x in data]

variance = sum(mean_diff_squared) / (n - 1) # For sample data

# Standard Deviation

std_dev = variance ** 0.5

# Display results

print("sorted_data:", sorted_data)

print(f"Mean: {mean:.3f}")
print(f"Median: {median}")

print(f"Mode: {mode[0]}")

print(f"Variance: {variance:.2f}")

print(f"Standard Deviation: {std_dev:.2f}")

# Example dataset

data = [12, 1.8, 22, 5, 14, 35, 44, 5,7.2]

compute_statistics(data)

OUTPUT:
sorted_data: [1.8, 5, 5, 7.2, 12, 14, 22, 35, 44]
Mean: 16.222
Median: 12
Mode: 5
Variance: 215.20
Standard Deviation: 14.67
2. Apply the following Pre-processing techniques for a given dataset.
a. Attribute selection b. Handling Missing Values c. Discretization

import pandas as pd

import numpy as np

from sklearn.feature_selection import SelectKBest, f_classif

from [Link] import SimpleImputer

data = {

'Age': [25, 30, [Link], 35, 40, 80, 23],

'Salary': [400, 600, 550, [Link], 700, 800, 120],

'Education': [1, 2, 1, 3, 2, 3, 1],

'Target': [0, 1, 0, 1, 1, 0, 0]

#Handling Missing Values

df = [Link](data)

print("\nHandling Missing Values:")

imputer = SimpleImputer(strategy='mean')

df[['Age', 'Salary']] = imputer.fit_transform(df[['Age', 'Salary']])

df['Age']=df['Age'].apply(lambda x: round(x))

print(df)
# Step 2: Attribute Selection (Feature Selection)

print("\nApplying Attribute Selection:")

X = df[['Age', 'Salary', 'Education']]

y = df['Target']

selector = SelectKBest(score_func=f_classif, k=2) # Select top 2 features

selector.fit_transform(X, y)

selected_features = [Link][selector.get_support()]

print("Selected Features:", list(selected_features))

# Step 3: Discretization

print("\nApplying Discretization:")

# Discretize 'Salary' into bins

df['Age_Bins'] = [Link](df['Age'], bins=[0, 25, 35, 50, [Link]], labels=['Youth', 'Young Adult', 'Middle-
Aged', 'Senior'])

print(df)

OUTPUT:
Handling Missing Values:
Age Salary Education Target
0 25 400.000000 1 0
1 30 600.000000 2 1
2 39 550.000000 1 0
3 35 528.333333 3 1
4 40 700.000000 2 1
5 80 800.000000 3 0
6 23 120.000000 1 0

Applying Attribute Selection:


Selected Features: ['Salary', 'Education']

Applying Discretization:
Age Salary Education Target Age_Bins
0 25 400.000000 1 0 Youth
1 30 600.000000 2 1 Young Adult
2 39 550.000000 1 0 Middle-Aged
3 35 528.333333 3 1 Young Adult
4 40 700.000000 2 1 Middle-Aged
5 80 800.000000 3 0 Senior
6 23 120.000000 1 0 Youth
Q3. Apply KNN algorithm for regression , let k=2, determine the performance of KNN
algorithm
from [Link] import KNeighborsRegressor

from sklearn.model_selection import train_test_split

from [Link] import StandardScaler


from [Link] import r2_score, mean_squared_error

import pandas as pd

data = {

"Age": [25, 20, 25, 30, 35, 40, 40, 31, 24, 33],

"Experience": [5, 1, 3, 4, 6, 8, 10, 12, 6, 8],

"Salary": [25, 18, 22, 28, 35, 42, 48, 50, 30, 38]

df = [Link](data)

X = df[["Age", "Experience"]]

y = df["Salary"]

X_train, X_test, y_train, y_test = train_test_split(

X, y, test_size=0.3, random_state=11)

scaler = StandardScaler()

X_train_scaled = scaler.fit_transform(X_train)

X_test_scaled = [Link](X_test)

knn = KNeighborsRegressor(n_neighbors=2)

[Link](X_train_scaled, y_train)

y_pred = [Link](X_test_scaled)

r2 = r2_score(y_test, y_pred)

mse = mean_squared_error(y_test, y_pred)

print("R² Score:", r2)

print("Mean Squared Error:", mse)

new_sample = [[34, 6]]

new_sample_scaled = [Link](new_sample)

predicted_salary = [Link](new_sample_scaled)[0]

print("\nPredicted Salary for new sample:", predicted_salary)


OUTPUT:
R² Score: 0.8040865384615384
Mean Squared Error: 27.166666666666668

Predicted Salary for new sample: 31.5


Q4: Apply Random Forest algorithm for classification

import pandas as pd

from sklearn.model_selection import train_test_split

from [Link] import RandomForestClassifier

from [Link] import accuracy_score, classification_report

from [Link] import LabelEncoder

from [Link] import load_iris

iris=load_iris()

df=[Link](data=[Link],columns=iris.feature_names)

df['species']= [Link]

le = LabelEncoder()

df['species'] = le.fit_transform(df['species'])

X = [Link](columns=['species'])

y = df['species']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=29)

clf = RandomForestClassifier(n_estimators=100, random_state=42)

[Link](X_train, y_train)

y_pred = [Link](X_test)

print("Accuracy of Random Forest Algorithm:", accuracy_score(y_test, y_pred))

print(classification_report(y_test, y_pred))

# new sample

target_names = iris.target_names

le=LabelEncoder()

new_sample =[Link]([[5.1, 3.5, 1.4, 0.2]],columns=[Link]) # Example new sample

y_new_pred = [Link](new_sample)

#predicted_species = le.inverse_transform(y_new_pred)

predicted_species = target_names[y_new_pred[0]]

print("Predicted species:", predicted_species)

OUTPUT:
Accuracy of Random Forest Algorithm: 0.9210526315789473
precision recall f1-score support
0 1.00 1.00 1.00 8
1 0.86 0.92 0.89 13
2 0.94 0.88 0.91 17

accuracy 0.92 38
macro avg 0.93 0.94 0.93 38
weighted avg 0.92 0.92 0.92 38

Predicted species: setosa


Q5 .Demonstrate decision tree algorithm for a regression problem for the given dataset.

import pandas as pd

import [Link] as plt

from sklearn.model_selection import train_test_split

from [Link] import StandardScaler

from [Link] import DecisionTreeRegressor, plot_tree

from [Link] import mean_squared_error, r2_score

data = {

"Age": [25, 20, 25, 30, 35, 40, 40, 31, 24, 33],

"Experience": [5, 1, 3, 4, 6, 8, 10, 12, 6, 8],

"Salary": [25, 18, 22, 28, 35, 42, 48, 50, 30, 38]

df = [Link](data)

X = df[['Age', 'Experience']]

y = df['Salary']

X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=22)

model = DecisionTreeRegressor(random_state=22)

[Link](X_train, y_train)

y_pred = [Link](X_test)

mse = mean_squared_error(y_test, y_pred)

r2 = r2_score(y_test, y_pred)

print("Predicted Salaries :", y_pred)

print("MSE :", round(mse, 3))

print("R² Score :", round(r2, 3))

[Link](figsize=(12, 6))

plot_tree(model, filled=True, feature_names=['Age', 'Experience'])


[Link]("Decision Tree Regression")

[Link]()

#New Sample Prediction:

new_sample = [Link]([[35, 8]])

predicted_salary = [Link](new_sample)[0]

print(f"\nPredicted Salary for sample {new_sample}= {predicted_salary:.2f}")

OUTPUT:

Test Predicted Salaries : [25. 22.]


MSE : 20.5
R² Score : 0.431

Predicted Salary for sample [[35 8]]= 35.00

Q6. Apply Support for Vector algorithm classification

import pandas as pd

from sklearn.model_selection import train_test_split

from [Link] import SVC

from [Link] import LabelEncoder

from [Link] import accuracy_score, classification_report, confusion_matrix

from [Link] import load_iris

iris=load_iris()

df=[Link](data=[Link],columns=iris.feature_names)
df['species']= [Link]

le = LabelEncoder()

df['species'] = le.fit_transform(df['species'])

X = [Link](columns=['species'])

y = df['species']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=29)

svm_model = SVC(kernel='linear', C=1.0, random_state=42)

svm_model.fit(X_train, y_train)

y_pred = svm_model.predict(X_test)

print("Accuracy:", accuracy_score(y_test, y_pred))

print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))

print("Classification Report:\n", classification_report(y_test, y_pred))

#Predict a new sample

target_names = iris.target_names

new_sample =[Link]([[5.1, 3.5, 1.4, 0.2]],columns=[Link]) # Example new sample

y_new_pred = svm_model.predict(new_sample)

predicted_species = target_names[y_new_pred[0]]

print("Predicted species of new sample is:", predicted_species)

OUTPUT:
Accuracy: 0.9736842105263158
Confusion Matrix:
[[ 8 0 0]
[ 0 12 1]
[ 0 0 17]]
Classification Report:
precision recall f1-score support

0 1.00 1.00 1.00 8


1 1.00 0.92 0.96 13
2 0.94 1.00 0.97 17

accuracy 0.97 38
macro avg 0.98 0.97 0.98 38
weighted avg 0.98 0.97 0.97 38

Predicted species of new sample is: setosa


Q7. Apply Logistic regression algorithm for a classification problem
import pandas as pd
from sklearn.model_selection import train_test_split

from sklearn.linear_model import LogisticRegression


from [Link] import LabelEncoder
from [Link] import accuracy_score, classification_report, confusion_matrix
import [Link] as plt
from [Link] import load_iris

iris=load_iris()
df=[Link](data=[Link],columns=iris.feature_names)
df['species']= [Link]
le = LabelEncoder()
df['species'] = le.fit_transform(df['species'])

X = [Link](columns=['species'])
y = df['species']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=29)
model = LogisticRegression(max_iter=300)
[Link](X_train, y_train)

y_pred = [Link](X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("Classification Report:\n", classification_report(y_test, y_pred))
#Predict a new sample

target_names = iris.target_names
le=LabelEncoder()
new_sample =[Link]([[7.1, 2.0, 6.4, 0.5]],columns=[Link])
y_new_pred = [Link](new_sample)
predicted_species = target_names[y_new_pred[0]]
print("Predicted species:", predicted_species)
OUTPUT:

Accuracy: 0.9473684210526315
Confusion Matrix:
[[ 8 0 0]
[ 0 12 1]
[ 0 1 16]]
Classification Report:
precision recall f1-score support

0 1.00 1.00 1.00 8


1 0.92 0.92 0.92 13
2 0.94 0.94 0.94 17

accuracy 0.95 38
macro avg 0.95 0.95 0.95 38
weighted avg 0.95 0.95 0.95 38

Predicted species: virginica

Q 8. Implement the K-means algorithm and Evaluate performance

from [Link] import load_iris

import numpy as np

import pandas as pd

from [Link] import KMeans

import [Link] as plt

# Load dataset

iris=load_iris()

df=[Link](data=[Link],columns=iris.feature_names)

X = [Link]

Ks = range(1, 7)

sum_of_distances = [ ]

for k in Ks :

kmeans = KMeans(n_clusters=k, random_state=30)

[Link](X)

sum_of_distances.append(kmeans.inertia_)

[Link](figsize=(8,5))

[Link](Ks, sum_of_distances, marker='o')


[Link]("K Value")

[Link]("Sum of Euclidean Distances (Inertia)")

[Link]("K-Means Performance vs K")

[Link](True)

[Link]()

print("Performance Evaluation (Sum of Euclidean Distances):")

for k, dist in zip(Ks, sum_of_distances):

print(f"K={k}: {dist:.4f}")

best_k = 3

kmeans_final = KMeans(n_clusters=best_k, random_state=42)

kmeans_final.fit(X)

labels = kmeans_final.labels_

centers = kmeans_final.cluster_centers_

[Link](figsize=(8,5))

[Link](df["petal length (cm)"], df["petal width (cm)"], c=labels, s=90, edgecolor='k')

[Link](centers[:,2], centers[:,3], marker='*', s=300, c='red')

[Link]("Petal Length")

[Link]("Petal Width")

[Link](f"K-Means Cluster Visualization (K={best_k})")

[Link](True)

[Link]()#Test with new sample

new_sample = [Link]([[5.2, 3.4, 1.5, 0.3]]) # change values to test any input

cluster = [Link](new_sample)[0]

sample_list = new_sample[0].tolist()

print(f"{sample_list}sample belongs to the cluster number:{cluster}")

OUTPUT:
QNo.9 Demonstrate the use of Fuzzy C-Means Clustering. Plot the Fuzzy C-Means Clustering for iris
dataset.

import numpy as np

import pandas as pd

import [Link] as plt

import skfuzzy as fuzz

from [Link] import StandardScaler

from [Link] import load_iris

#Load the dataset

iris=load_iris()
df=[Link](data=[Link],columns=iris.feature_names)

X = [Link]

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)

# Transpose for skfuzzy

data = X_scaled.T

#Apply Fuzzy C-Means

n_clusters = 3 # for iris, usually 3 clusters

m = 2.0 # fuzziness parameter


cntr, u, u0, d, jm, p, fpc = [Link](data=data,c=n_clusters,
m=m,error=0.005,maxiter=1000, init=None)

print("Cluster Centers:\n", cntr)

print("\nFuzzy Partition Coefficient (FPC):", fpc)

#Hard cluster assignment

cluster_membership = [Link](u, axis=0)

print("\nCluster labels:\n", cluster_membership)

[Link](figsize=(7,6))

for j in range(n_clusters):

[Link](

X_scaled[cluster_membership == j, 0],

X_scaled[cluster_membership == j, 1],

label=f"Cluster {j}"

[Link](cntr[:, 0], cntr[:, 1], marker='X', s=200, c='black', label="Centers")

[Link]("Feature 1 (scaled)")

[Link]("Feature 2 (scaled)")

[Link]("Fuzzy C-Means Clustering (Iris Dataset)")

[Link]()

[Link](True)

[Link]()
OUTPUT:

QNo.10. Demonstrate the use of Expectation Maximization based clustering algorithm

import numpy as np

import pandas as pd

import [Link] as plt

from [Link] import GaussianMixture

from [Link] import StandardScaler

from [Link] import PCA

from [Link] import accuracy_score, adjusted_rand_score

from [Link] import load_iris

iris = load_iris()

X = [Link]
true_labels = [Link] # Actual class labels for comparison

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)

num_clusters = 3 # We know Iris has 3 species

gmm = GaussianMixture(n_components=num_clusters, covariance_type='full', random_state=42)

[Link](X_scaled)

predicted_labels = [Link](X_scaled)

ari_score = adjusted_rand_score(true_labels, predicted_labels)

print(f"Adjusted Rand Index (ARI): {ari_score:.4f}")

pca = PCA(n_components=2)

X_pca = pca.fit_transform(X_scaled)

[Link](figsize=(8, 4))

[Link](1, 2, 1)

[Link](X_pca[:, 0], X_pca[:, 1], c=true_labels, cmap='viridis', edgecolors='k', s=100)

[Link]("PCA Component 1")

[Link]("PCA Component 2")

[Link]("Actual Classes (Iris Dataset)")

[Link](1, 2, 2)

[Link]("PCA Component 1")

[Link]("PCA Component 2")

[Link](X_pca[:, 0], X_pca[:, 1], c=predicted_labels, cmap='coolwarm', edgecolors='k', s=100)

[Link]("PCA Component 1")

[Link]("PCA Component 2")

[Link]("EM-based Clustering using GMM")

plt.tight_layout()

[Link]()

OUTPUT:

Adjusted Rand Index (ARI): 0.9039


Note: USE COLOR PENCILS/PENS FOR ABOVE ANY FIGURE.

You might also like