0% found this document useful (0 votes)
17 views18 pages

ML Programs

The document outlines several lab programs focused on various machine learning techniques, including data visualization, dimensionality reduction, classification algorithms, and regression analysis using different datasets such as California Housing, Iris, and Breast Cancer. Each program includes code snippets for implementing specific algorithms like PCA, KNN, Decision Trees, and Naive Bayes, along with data preprocessing and visualization steps. The document serves as a comprehensive guide for practical applications of machine learning concepts.

Uploaded by

munirajgowdabr
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)
17 views18 pages

ML Programs

The document outlines several lab programs focused on various machine learning techniques, including data visualization, dimensionality reduction, classification algorithms, and regression analysis using different datasets such as California Housing, Iris, and Breast Cancer. Each program includes code snippets for implementing specific algorithms like PCA, KNN, Decision Trees, and Naive Bayes, along with data preprocessing and visualization steps. The document serves as a comprehensive guide for practical applications of machine learning concepts.

Uploaded by

munirajgowdabr
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 Programs

Lab Program 1 :
Develop a program to create histograms for all numerical features and analyse the distribution of each
feature. Generate box plots for all numerical features and identify any outliers. Use California Housing
dataset.

import pandas as pd
import numpy as np
import seaborn as sns
import [Link] as plt
from [Link] import fetch_california_housing

#Step 1: Load the califoria Housing dataset


data = fetch_california_housing(as_frame = True)
housing_df = [Link]
#Display basic information about the dataset
print("Dataset Display:")
print(housing_df)
print("Dataset Overview:")
print(housing_df.info())

#Step 2:create histograms for numberical features


numerical_features = housing_df.select_dtypes(include = [[Link]]).columns
print(numerical_features)

#Plot Hostograms
[Link](figsize = (15,10))
for i, features in enumerate(numerical_features):
[Link](3,3,i+1)
[Link](housing_df[features], kde=True, bins=30, color='blue')
[Link](f'Distribution of {features}')
plt.tight_layout()
[Link]("[Link]", dpi=200)
[Link]()
#Step 3: Generate box plot for numerical features
[Link](figsize = (15,10))
for i, features in enumerate(numerical_features):
[Link](3,3,i+1)
[Link](x=housing_df[features], color='green')
[Link](f'Box Plot of {features}')
plt.tight_layout()
[Link]("[Link]", dpi=200)
[Link]()

#Step 4: Identify outlines using the IQR method


print("Outliers Detection")
outliers_summery ={}
for features in numerical_features:
Q1 = housing_df[features].quantile(0.25)
Q3 = housing_df[features].quantile(0.75)
IQR = Q3-Q1
lower_bound = Q1-1.5*IQR
upper_bound = Q3+1.5*IQR
outliers = housing_df[(housing_df[features]<lower_bound) | (housing_df[features]<upper_bound)]
outliers_summery[features] = len(outliers)
print(f"{features} : {len(outliers)} outliers")

#Optional: Print a summery of the dataset


print("Dataset Summery:")
print(housing_df.describe())
Lab program 2:
Develop a program to Compute the Correlation Matrix to understand the relationships between pairs of
features. Visualize the correlation matrix using a heatmap to know which variables have strong
positive/negative correlations. Create a pair plot to visualize pairwise relationships between features. Use
California Housing dataset.

import pandas as pd
import seaborn as sns
import [Link] as plt
from [Link] import fetch_california_housing

#Step 1: load california housing dataset


california_data = fetch_california_housing(as_frame=True)
data = california_data.frame
print(data)

#Step 2: compute the correlation matrix


correlation_matrix = [Link]()
print(correlation_matrix)

# Step 3: Visualize the correlation matrix using heatmap


[Link](figsize=(8, 8))
[Link](correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f", linewidths=0.5)
[Link]('Correlation Matrix of California Housing Features')
[Link]("[Link]")
[Link]()

# Step 4: Create a pair plot to visualize pairwise relationships (scatter matrix)


[Link](data, diag_kind='kde', plot_kws={'alpha': 1.0})
[Link]('Pair Plot of California Housing Features', y=1.02)
[Link]("[Link]")
[Link]()
lab program 3:
Develop a program to implement Principal Component Analysis (PCA) for reducing the dimensionality
of the Iris dataset from 4 features to 2.

import numpy as np
import pandas as pd
from [Link] import load_iris
from [Link] import PCA
import [Link] as plt

#Load the iris dataset


iris = load_iris()
data = [Link]
labels = [Link]
label_names = iris.target_names
print("Iris dataset")
print("Target names:")
print(label_names)
print("labels:")
print(labels)

# Convert to a dataframe for better visualization


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

#perform PCA to reduce dimentionality to 2


pca = PCA(n_components=2)
data_reduced = pca.fit_transform(data)
print(data_reduced)

# Create a dataframe for the related data


reduced_df = [Link](data_reduced, columns=['Principal Component 1', 'Principal Component 2'])
reduced_df['label'] = labels
print(reduced_df)
# Plot the reduced data
[Link](figsize=(6,6))
colors = ['y', 'r', 'b']
for i, label in enumerate([Link](labels)):
[Link](
reduced_df[reduced_df['label'] == label]['Principal Component 1'],
reduced_df[reduced_df['label'] == label]['Principal Component 2'],
label=label_names[label],
color=colors[i]
)
[Link]('PCA on Iris Dataset')
[Link]('Principal Component 1')
[Link]('Principal Component 2')
[Link]()
[Link]()
[Link]("[Link]")
[Link]()
Lab program 4:
Find S Algorithm

import csv
dataset = []
with open('[Link]', 'r') as csv_file:
csv_reader = [Link](csv_file, delimiter='\t') # <-- Important fix
header = next(csv_reader)
print("Header:", header)
num_attributes = len(header) – 1

for row in csv_reader:


[Link](row)
print("Dataset:")
print(dataset)
# Initialize hypothesis
hypothesis = ['0'] * num_attributes
print("Initial hypothesis:")
print(hypothesis)
# Apply Find-S algorithm
print("The hypotheses are:")
for i in range(len(dataset)):
row = dataset[i]
target = row[-1].strip().lower()

if target == 'yes':
for j in range(num_attributes):
if hypothesis[j] == '0':
hypothesis[j] = row[j]
elif hypothesis[j] != row[j]:
hypothesis[j] = '?'
print(f"{i+1} = {hypothesis}")
print("Final hypothesis:")
print(hypothesis)
Lab program 5:
KNN Classification function

import numpy as np
import [Link] as plt
from collections import Counter

# Data generation function


def generate_data():
x = [Link](100)
labels = [Link](["Class1" if xi <= 0.5 else "Class2" for xi in x])
return x, labels

# KNN classification function


def knn_classification(train_x, train_labels, test_x, k):
predictions = []
for x_test in test_x:
distances = [Link](train_x - x_test)
nearest_indices = [Link](distances)[:k]
nearest_labels = train_labels[nearest_indices]
most_common = Counter(nearest_labels).most_common(1)[0][0]
[Link](most_common)
return [Link](predictions)

# Main Function
def main():
x, labels = generate_data()
train_x, test_x = x[:50], x[50:]
train_labels = labels[:50]
test_labels = labels[50:]
print("Train X:\n", train_x)
print("Test X:\n", test_x)
print("Labels:\n", labels)
k_values = [1, 2, 3, 4, 5, 20, 30]
results = {}

for k in k_values:
predictions = knn_classification(train_x, train_labels, test_x, k)
results[k] = predictions

# Plotting
[Link](figsize=(8, 4))
[Link](train_x, [0]*len(train_x),
c=["blue" if lbl == "Class1" else "red" for lbl in train_labels],
label="Training Data", marker='o')
[Link](test_x, [1]*len(test_x),
c=["blue" if lbl == "Class1" else "red" for lbl in predictions],
label=f"Test Data (Predicted)", marker='x')
[Link]("x")
[Link]("Classification Level")
[Link](f"KNN Classification (k={k})")
[Link]()
[Link](True)
[Link]()

print(f"Results for K={k}:\n{predictions}\n")

if __name__ == "__main__":
main()
Lab program 6:
Implement the Non-Parametric Locally Weighted Regression Algorithm in order to fit data points. select
appropriate set of your experiment and draw graphs

import numpy as np
import [Link] as plt

x=[Link](1,10,100).reshape(-1,1)
y=[Link](x).ravel()+0.1*[Link](100)
x_bias=[Link]([[Link](([Link][0],1)),x])

x_test=[Link](1,10,200)
y_pred=[]
tau=0.5

for xq in x_test:
xq_bias=[Link]([1,xq])
w=[Link]([Link][0])
for i in range([Link][0]):
diff=x[i]-xq
w[i,i]=[Link](-(diff@diff.T)/(2*tau**2))
theta=[Link](x_bias.T@w@x_bias)@x_bias.T@w@y
y_pred.append(xq_bias@theta)

[Link](figsize=(10,6))
[Link](x,y,label="training data",color="blue",alpha=0.6)
[Link](x_test,y_pred,label="LWR Fit(tau=0.5)",color="red",linewidth=2)
[Link]("locally weighted regression(LWR)")
[Link]('x')
[Link]('y')
[Link]()
[Link](True)
[Link]()
Lab program 7:
Develop a program to demonstrate the working of Linear Regression and polynomial regression. Use
Boston housing dataset for linear regression and auto MPG dataset for polynomial regression (dataset –
for vehicle fuel efficiency prediction)
a. Linear Regression

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

boston=fetch_openml(name='boston',version=1,as_frame=True)
x_boston=[Link]
y_boston=[Link]

[Link]

[Link]

[Link]

print([Link]())

x_boston = x_boston.astype(float)
y_boston = y_boston.astype(float)
x_train, x_test, y_train, y_test = train_test_split(x_boston, y_boston, test_size=0.2, random_state=42)

lr=LinearRegression()
[Link](x_train, y_train)
y_pred=[Link](x_test)

print(f"y_test:\n{y_test}/ny_pred:\n{y_pred}")
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print("linear regression results (boston housing):")
print(f"MSE: {mse:.2f}")
print(f"R2 score: {r2:.2f}")

[Link](figsize=(10,6))
[Link](y_test, y_pred, c="blue", label='predicted values', alpha=0.6)
min_val = min(min(y_test), min(y_pred))
max_val = max(max(y_test), max(y_pred))
[Link]([min_val, max_val], [min_val, max_val], 'r--', label='perfect prediction')
[Link]('Actual vs Predicted House Prices')
[Link]('Actual Prices ($1000s)')
[Link]('Predicted Prices ($1000s)')
[Link](True)
[Link]()
b. Polynomial Regression
import numpy as np
import pandas as pd
import [Link] as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from [Link] import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error,r2_score

auto_mpg=pd.read_csv("[Link]")
auto_mpg.dropna(inplace=True)
auto_mpg=auto_mpg[auto_mpg['horsepower']!='?']
auto_mpg['horsepower']=auto_mpg['horsepower'].astype(float)
x_auto=auto_mpg[['horsepower']]
y_auto=auto_mpg['mpg']
x_train,x_test,y_train,y_test=train_test_split(x_auto,y_auto,test_size=0.2,random_state=42)

poly=PolynomialFeatures(degree=2)
x_train_poly=poly.fit_transform(x_train)
x_test_poly=[Link](x_test)

poly_reg=LinearRegression()
poly_reg.fit(x_train_poly,y_train)
y_pred_poly=poly_reg.predict(x_test_poly)

mse=mean_squared_error(y_test,y_pred_poly)
r2=r2_score(y_test,y_pred_poly)
print("PolynomialRegression Results(Auto MPG Dataset):")
print(f"MSE:{mse:.2f}")
print(f"r2 score:{r2:.2f}")

[Link](x_test,y_test,color='blue',label="Actual Data",alpha=0.5)
[Link](x_test,y_pred_poly,color='red',label="PredictedData",alpha=0.5)
[Link]("Horsepower")
[Link]("MPG")
[Link]()
[Link]("PolynomialRegression on AutoMPGDataset")
[Link]("PolynomialRegression")
[Link]()
Lab program 8:
Develop a program to demonstrate the working od the Decision Tree Algorithm. Use Breast cancer
dataset for building the decision tree and apply this knowledge to classify a new sample.

import numpy as np
from [Link] import load_breast_cancer
from [Link] import DecisionTreeClassifier, export_text, plot_tree
from sklearn.model_selection import train_test_split
from [Link] import accuracy_score
import [Link] as plt

data=load_breast_cancer()
x=[Link]
y=[Link]
feature_names=data.feature_names
target_names=data.target_names

[Link]

data.feature_names

data.target_names

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2, random_state=42)
model=DecisionTreeClassifier(criterion="entropy",random_state=42)
[Link](x_train,y_train)

y_pred=[Link](x_test)
print(f"y_test:\n{y_test}\ny_pred:\n{y_pred}")
accuracy=accuracy_score(y_test,y_pred)
print(f"Decision Tree Accuracy:{accuracy:.2f}")
[Link](figsize=(16,10))
plot_tree(model,feature_names=feature_names,class_names=target_names,filled=True, rounded=True)
[Link]("Decision tree visualization")
[Link]("decision tree")
[Link]()

tree_rules=export_text(model, feature_names=list(feature_names))
print("\n Decision tree rules:")
print(tree_rules)

new_sample=[[
20.57, 17.77, 132.89, 1326.0, 0.08474,0.07864,0.0869,0.07017,0.1812,0.05667,
0.5435,0.7339,3.398,74.08,0.005225,0.01308,0.0186,0.0134,0.01389,0.003532,25.38,
24.99,166.1,2019.0,0.1622,0.6656,0.7119,0.2654,0.4601,0.1189
]]
predicted_class=[Link](new_sample)
print("\n new sample classification :")
print(f"predicted class:{target_names[predicted_class[0]]}")
Lab program 9:
Develop a program to implement the Native Bayesian classifier considering Olivetti Face dataset for
training. Compute the accuracy of the classifier, considering a few test datasets.

from [Link] import fetch_olivetti_faces


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

data=fetch_olivetti_faces(shuffle=True,random_state=42)
x=[Link]
y=[Link]

print(f"Dataset size : {[Link][0]} samples, each with {[Link][1]} features (pixels)")

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.25,random_state=42)
model=GaussianNB()
[Link](x_train,y_train)
y_pred=[Link](x_test)

accuracy=accuracy_score(y_test,y_pred)
print(f"Accuracy:{accuracy*100:.2f}%")
print("/nClassification Report:")
print(classification_report(y_test,y_pred,zero_division=1))

num_visualize=10
[Link](figsize=(10,5))
for i in range(num_visualize):
[Link](1,num_visualize,i+1)
[Link](x_test[i].reshape(64,64),cmap=[Link])
[Link](f"True:{y_test[i]}\npred:{y_pred[i]}")
[Link]('off')
plt.tight_layout()
[Link]()
Lab program 10:
Develop a program to implement k-means clustering algorithm using Wisconsin breast cancer dataset
and visualize the clustering result

import numpy as np
import [Link] as plt
from [Link] import load_breast_cancer
from [Link] import KMeans
from [Link] import PCA
from [Link] import StandardScaler

# Load dataset
data = load_breast_cancer()
x = [Link]
y = [Link]
feature_names = data.feature_names
target_names = data.target_names

print(f"Dataset size: {[Link][0]} samples, each with {[Link][1]} features")


print("Feature names:", feature_names)

# Standardize the data


scaler = StandardScaler()
x_scaled = scaler.fit_transform(x)

# Apply KMeans clustering


kmeans = KMeans(n_clusters=2, n_init=10, random_state=42)
[Link](x_scaled)
y_kmeans = kmeans.labels_

# Reduce dimensionality using PCA


pca = PCA(n_components=2)
x_pca = pca.fit_transform(x_scaled)
centroids = [Link](kmeans.cluster_centers_)
print("Centroids in PCA-reduced space:")
print(centroids)

# Plotting the clusters and centroids


[Link](figsize=(8, 6))
scatter = [Link](x_pca[:, 0], x_pca[:, 1], c=y_kmeans, cmap='coolwarm', edgecolors='k', s=50)
[Link](centroids[:, 0], centroids[:, 1], c='yellow', s=200, marker='X', edgecolors='black', label='Centroids')
[Link]("K-Means Clustering on Breast Cancer Dataset (PCA Reduced)")
[Link]("PCA Component 1")
[Link]("PCA Component 2")
[Link]()
[Link](True)
[Link]()

You might also like