0% found this document useful (0 votes)
3 views13 pages

ML Lab Programs

The document outlines a series of machine learning programs using Python and sklearn, covering various techniques such as data preprocessing, classification, regression, and model evaluation. Each program follows a structured format that includes importing packages, data upload, preprocessing, model training, and performance metrics. Key algorithms demonstrated include KNN, Decision Trees, Random Forests, Naïve Bayes, SVM, and Logistic Regression, applied to datasets like Iris, Diabetes, and Breast Cancer.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

ML Lab Programs

The document outlines a series of machine learning programs using Python and sklearn, covering various techniques such as data preprocessing, classification, regression, and model evaluation. Each program follows a structured format that includes importing packages, data upload, preprocessing, model training, and performance metrics. Key algorithms demonstrated include KNN, Decision Trees, Random Forests, Naïve Bayes, SVM, and Logistic Regression, applied to datasets like Iris, Diabetes, and Breast Cancer.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Machine Learning Lab

Python Programs (Programs 2–12)


All programs use sklearn built-in datasets

Structure of Every Program:


Import Packages → Data Upload → Data Preprocessing → Train-Test Split → Load the Model → Train the Model
→ Test the Model → Performance Metrics
Program 2: Data Preprocessing
Dataset: Iris ([Link].load_iris)
Techniques: Attribute Selection, Handling Missing Values, Discretization, Elimination of Outliers

Code
# ── Import Packages ──────────────────────────────────────────────────
import pandas as pd
import numpy as np
from [Link] import load_iris
from [Link] import KBinsDiscretizer

# ── Data Upload (Load Dataset) ────────────────────────────────────────


data = load_iris()
df = [Link]([Link], columns=data.feature_names)
df['target'] = [Link]
print('Original Shape:', [Link])
print([Link]())

# ── Data Preprocessing: a) Attribute Selection ───────────────────────


# Select only 2 features (sepal length and petal length)
selected = df[['sepal length (cm)', 'petal length (cm)', 'target']]
print('After Attribute Selection:', [Link])

# ── Data Preprocessing: b) Handling Missing Values ───────────────────


# Artificially introduce missing values for demo
df_missing = [Link]()
df_missing.iloc[0, 0] = [Link]
df_missing.iloc[5, 1] = [Link]
print('Missing values before:', df_missing.isnull().sum().sum())
df_missing.fillna(df_missing.mean(numeric_only=True), inplace=True)
print('Missing values after:', df_missing.isnull().sum().sum())

# ── Data Preprocessing: c) Discretization ────────────────────────────


kbd = KBinsDiscretizer(n_bins=3, encode='ordinal', strategy='uniform')
disc = kbd.fit_transform(df[['sepal length (cm)']])
print('Discretized sepal length (first 5):', disc[:5].flatten())

# ── Data Preprocessing: d) Elimination of Outliers ──────────────────


Q1 = df['sepal length (cm)'].quantile(0.25)
Q3 = df['sepal length (cm)'].quantile(0.75)
IQR = Q3 - Q1
df_clean = df[(df['sepal length (cm)'] >= Q1 - 1.5*IQR) &
(df['sepal length (cm)'] <= Q3 + 1.5*IQR)]
print('Rows before outlier removal:', len(df))
print('Rows after outlier removal:', len(df_clean))
Program 3: KNN – Classification & Regression
Classification Dataset: Iris ([Link].load_iris)
Regression Dataset: Diabetes ([Link].load_diabetes)

Part A – KNN Classification


# ── Import Packages ──────────────────────────────────────────────────
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import KNeighborsClassifier
from [Link] import accuracy_score, classification_report

# ── Data Upload ───────────────────────────────────────────────────────


data = load_iris()
X, y = [Link], [Link]

# ── Data Preprocessing (no missing values in this dataset) ──────────


# Features are already numeric – no extra preprocessing needed

# ── Train-Test Split ──────────────────────────────────────────────────


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

# ── Load the Model ────────────────────────────────────────────────────


model = KNeighborsClassifier(n_neighbors=5)

# ── Train the Model ───────────────────────────────────────────────────


[Link](X_train, y_train)

# ── Test the Model ────────────────────────────────────────────────────


y_pred = [Link](X_test)

# ── Performance Metrics ───────────────────────────────────────────────


print('Accuracy:', accuracy_score(y_test, y_pred))
print(classification_report(y_test, y_pred))

Part B – KNN Regression


from [Link] import load_diabetes
from [Link] import KNeighborsRegressor
from [Link] import mean_squared_error, r2_score

data = load_diabetes()
X, y = [Link], [Link]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)

model = KNeighborsRegressor(n_neighbors=5)
[Link](X_train, y_train)
y_pred = [Link](X_test)

print('MSE:', mean_squared_error(y_test, y_pred))


print('R2 Score:', r2_score(y_test, y_pred))
Program 4: Decision Tree – Classification with Parameter Tuning
Dataset: Breast Cancer ([Link].load_breast_cancer)

# ── Import Packages ──────────────────────────────────────────────────


from [Link] import load_breast_cancer
from sklearn.model_selection import train_test_split, GridSearchCV
from [Link] import DecisionTreeClassifier
from [Link] import accuracy_score, classification_report

# ── Data Upload ───────────────────────────────────────────────────────


data = load_breast_cancer()
X, y = [Link], [Link]

# ── Data Preprocessing ────────────────────────────────────────────────


# No missing values in this dataset; features are numeric

# ── Train-Test Split ──────────────────────────────────────────────────


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

# ── Load the Model ────────────────────────────────────────────────────


model = DecisionTreeClassifier(random_state=42)

# ── Train the Model ───────────────────────────────────────────────────


[Link](X_train, y_train)

# ── Test the Model ────────────────────────────────────────────────────


y_pred = [Link](X_test)
print('Before Tuning Accuracy:', accuracy_score(y_test, y_pred))

# ── Parameter Tuning with GridSearchCV ────────────────────────────────


params = {'max_depth': [3, 5, 10, None],
'min_samples_split': [2, 5, 10]}
grid = GridSearchCV(DecisionTreeClassifier(random_state=42),
params, cv=5)
[Link](X_train, y_train)

# ── Performance Metrics ───────────────────────────────────────────────


best = grid.best_estimator_
y_pred2 = [Link](X_test)
print('Best Params:', grid.best_params_)
print('After Tuning Accuracy:', accuracy_score(y_test, y_pred2))
print(classification_report(y_test, y_pred2))
Program 5: Decision Tree – Regression
Dataset: Diabetes ([Link].load_diabetes)

# ── Import Packages ──────────────────────────────────────────────────


from [Link] import load_diabetes
from sklearn.model_selection import train_test_split
from [Link] import DecisionTreeRegressor
from [Link] import mean_squared_error, r2_score

# ── Data Upload ───────────────────────────────────────────────────────


data = load_diabetes()
X, y = [Link], [Link]

# ── Data Preprocessing ────────────────────────────────────────────────


# Dataset is clean; features are already normalized

# ── Train-Test Split ──────────────────────────────────────────────────


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

# ── Load the Model ────────────────────────────────────────────────────


model = DecisionTreeRegressor(max_depth=5, random_state=42)

# ── Train the Model ───────────────────────────────────────────────────


[Link](X_train, y_train)

# ── Test the Model ────────────────────────────────────────────────────


y_pred = [Link](X_test)

# ── Performance Metrics ───────────────────────────────────────────────


print('MSE:', mean_squared_error(y_test, y_pred))
print('R2 Score:', r2_score(y_test, y_pred))
Program 6: Random Forest – Classification & Regression
Classification Dataset: Breast Cancer ([Link].load_breast_cancer)
Regression Dataset: Diabetes ([Link].load_diabetes)

Part A – Random Forest Classification


# ── Import Packages ──────────────────────────────────────────────────
from [Link] import load_breast_cancer
from sklearn.model_selection import train_test_split
from [Link] import RandomForestClassifier
from [Link] import accuracy_score, classification_report

# ── Data Upload ───────────────────────────────────────────────────────


data = load_breast_cancer()
X, y = [Link], [Link]

# ── Train-Test Split ──────────────────────────────────────────────────


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

# ── Load the Model ────────────────────────────────────────────────────


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

# ── Train the Model ───────────────────────────────────────────────────


[Link](X_train, y_train)

# ── Test the Model ────────────────────────────────────────────────────


y_pred = [Link](X_test)

# ── Performance Metrics ───────────────────────────────────────────────


print('Accuracy:', accuracy_score(y_test, y_pred))
print(classification_report(y_test, y_pred))

Part B – Random Forest Regression


from [Link] import load_diabetes
from [Link] import RandomForestRegressor
from [Link] import mean_squared_error, r2_score

data = load_diabetes()
X, y = [Link], [Link]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)

model = RandomForestRegressor(n_estimators=100, random_state=42)


[Link](X_train, y_train)
y_pred = [Link](X_test)

print('MSE:', mean_squared_error(y_test, y_pred))


print('R2 Score:', r2_score(y_test, y_pred))
Program 7: Naïve Bayes Classification
Dataset: Iris ([Link].load_iris)

# ── Import Packages ──────────────────────────────────────────────────


from [Link] import load_iris
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from [Link] import accuracy_score, classification_report

# ── Data Upload ───────────────────────────────────────────────────────


data = load_iris()
X, y = [Link], [Link]

# ── Data Preprocessing ────────────────────────────────────────────────


# Gaussian NB works well with continuous numeric features (no changes needed)

# ── Train-Test Split ──────────────────────────────────────────────────


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

# ── Load the Model ────────────────────────────────────────────────────


model = GaussianNB()

# ── Train the Model ───────────────────────────────────────────────────


[Link](X_train, y_train)

# ── Test the Model ────────────────────────────────────────────────────


y_pred = [Link](X_test)

# ── Performance Metrics ───────────────────────────────────────────────


print('Accuracy:', accuracy_score(y_test, y_pred))
print(classification_report(y_test, y_pred))
Program 8: Support Vector Machine (SVM) – Classification
Dataset: Breast Cancer ([Link].load_breast_cancer)

# ── Import Packages ──────────────────────────────────────────────────


from [Link] import load_breast_cancer
from sklearn.model_selection import train_test_split
from [Link] import SVC
from [Link] import StandardScaler
from [Link] import accuracy_score, classification_report

# ── Data Upload ───────────────────────────────────────────────────────


data = load_breast_cancer()
X, y = [Link], [Link]

# ── Data Preprocessing: Feature Scaling (important for SVM) ─────────


scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# ── Train-Test Split ──────────────────────────────────────────────────


X_train, X_test, y_train, y_test = train_test_split(
X_scaled, y, test_size=0.2, random_state=42)

# ── Load the Model ────────────────────────────────────────────────────


model = SVC(kernel='rbf', C=1.0)

# ── Train the Model ───────────────────────────────────────────────────


[Link](X_train, y_train)

# ── Test the Model ────────────────────────────────────────────────────


y_pred = [Link](X_test)

# ── Performance Metrics ───────────────────────────────────────────────


print('Accuracy:', accuracy_score(y_test, y_pred))
print(classification_report(y_test, y_pred))
Program 9: Simple Linear Regression
Dataset: Diabetes ([Link].load_diabetes) – using 1 feature

# ── Import Packages ──────────────────────────────────────────────────


import numpy as np
from [Link] import load_diabetes
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error, r2_score
import [Link] as plt

# ── Data Upload ───────────────────────────────────────────────────────


data = load_diabetes()
# Use only 1 feature (BMI index) for Simple Linear Regression
X = [Link][:, [Link], 2] # BMI feature
y = [Link]

# ── Data Preprocessing ────────────────────────────────────────────────


# Feature is already scaled in diabetes dataset

# ── Train-Test Split ──────────────────────────────────────────────────


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

# ── Load the Model ────────────────────────────────────────────────────


model = LinearRegression()

# ── Train the Model ───────────────────────────────────────────────────


[Link](X_train, y_train)

# ── Test the Model ────────────────────────────────────────────────────


y_pred = [Link](X_test)

# ── Performance Metrics ───────────────────────────────────────────────


print('Coefficient:', model.coef_)
print('Intercept:', model.intercept_)
print('MSE:', mean_squared_error(y_test, y_pred))
print('R2 Score:', r2_score(y_test, y_pred))

# ── Plot ──────────────────────────────────────────────────────────────
[Link](X_test, y_test, color='blue', label='Actual')
[Link](X_test, y_pred, color='red', label='Predicted')
[Link]('BMI'); [Link]('Disease Progression')
[Link]('Simple Linear Regression'); [Link]()
[Link]()
Program 10: Logistic Regression – Classification
Dataset: Iris ([Link].load_iris) – binary: classes 0 & 1

# ── Import Packages ──────────────────────────────────────────────────


from [Link] import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from [Link] import accuracy_score, classification_report, confusion_matrix

# ── Data Upload ───────────────────────────────────────────────────────


data = load_iris()
X, y = [Link], [Link]

# ── Data Preprocessing ────────────────────────────────────────────────


# Logistic Regression can handle multi-class natively (one-vs-rest)

# ── Train-Test Split ──────────────────────────────────────────────────


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

# ── Load the Model ────────────────────────────────────────────────────


model = LogisticRegression(max_iter=200)

# ── Train the Model ───────────────────────────────────────────────────


[Link](X_train, y_train)

# ── Test the Model ────────────────────────────────────────────────────


y_pred = [Link](X_test)

# ── Performance Metrics ───────────────────────────────────────────────


print('Accuracy:', accuracy_score(y_test, y_pred))
print('Confusion Matrix:')
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
Program 11: Multi-layer Perceptron (MLP) – Classification
Dataset: Digits ([Link].load_digits)

# ── Import Packages ──────────────────────────────────────────────────


from [Link] import load_digits
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from [Link] import StandardScaler
from [Link] import accuracy_score, classification_report

# ── Data Upload ───────────────────────────────────────────────────────


data = load_digits()
X, y = [Link], [Link]

# ── Data Preprocessing: Scale features ───────────────────────────────


scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# ── Train-Test Split ──────────────────────────────────────────────────


X_train, X_test, y_train, y_test = train_test_split(
X_scaled, y, test_size=0.2, random_state=42)

# ── Load the Model ────────────────────────────────────────────────────


# hidden_layer_sizes=(100,50) means 2 hidden layers with 100 and 50 neurons
model = MLPClassifier(hidden_layer_sizes=(100, 50),
max_iter=500, random_state=42)

# ── Train the Model ───────────────────────────────────────────────────


[Link](X_train, y_train)

# ── Test the Model ────────────────────────────────────────────────────


y_pred = [Link](X_test)

# ── Performance Metrics ───────────────────────────────────────────────


print('Accuracy:', accuracy_score(y_test, y_pred))
print(classification_report(y_test, y_pred))
Program 12: K-Means Clustering
Dataset: Iris ([Link].load_iris) – unsupervised clustering

K-Means is an unsupervised algorithm. There is no train-test split; instead, we fit the model on all data and
measure the sum of Euclidean distances (Inertia) as a function of K.

# ── Import Packages ──────────────────────────────────────────────────


from [Link] import load_iris
from [Link] import KMeans
from [Link] import StandardScaler
import [Link] as plt

# ── Data Upload ───────────────────────────────────────────────────────


data = load_iris()
X = [Link]

# ── Data Preprocessing: Scale the features ───────────────────────────


scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# ── Load & Train the Model for a fixed K ─────────────────────────────


model = KMeans(n_clusters=3, random_state=42)
[Link](X_scaled)

# ── Test / Evaluate ───────────────────────────────────────────────────


labels = model.labels_
print('Cluster Labels:', labels)
print('Cluster Centers:')
print(model.cluster_centers_)

# ── Performance Metric: Sum of Euclidean distances (Inertia) ─────────


print('Inertia (Sum of Squared Euclidean Distances) for K=3:',
model.inertia_)

# ── Evaluate as a Function of K (Elbow Method) ───────────────────────


inertias = []
K_values = range(1, 11)

for k in K_values:
km = KMeans(n_clusters=k, random_state=42)
[Link](X_scaled)
[Link](km.inertia_)
print(f'K={k} Inertia={km.inertia_:.2f}')

# ── Plot Elbow Curve ──────────────────────────────────────────────────


[Link](K_values, inertias, marker='o')
[Link]('Number of Clusters K')
[Link]('Inertia (Sum of Euclidean Distances)')
[Link]('Elbow Method – Choosing Best K')
[Link]()

You might also like