0% found this document useful (0 votes)
3 views

AIML PRACTICALS

The document is a laboratory manual for the Artificial Intelligence & Machine Learning Lab at Jabalpur Engineering College for the 2024-25 session. It outlines a series of experiments involving various machine learning techniques, including linear regression, logistic regression, decision trees, and clustering methods. Each experiment includes code snippets and descriptions of the processes involved in implementing these algorithms.

Uploaded by

zeszest5
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 views

AIML PRACTICALS

The document is a laboratory manual for the Artificial Intelligence & Machine Learning Lab at Jabalpur Engineering College for the 2024-25 session. It outlines a series of experiments involving various machine learning techniques, including linear regression, logistic regression, decision trees, and clustering methods. Each experiment includes code snippets and descriptions of the processes involved in implementing these algorithms.

Uploaded by

zeszest5
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
You are on page 1/ 22

JABALPUR ENGINEERING COLLEGE

(Established in 1947 as Government Engineering College, Jabalpur Declared Autonomous by Government of Madhya Pradesh and RGPV, Bhopal)

DEPARTMENT OF MECHATRONICS ENGINEERING


SESSION 2024-25

ARTIFICIAL INTELLIGENCE & MACHINE


LEARNING LAB
Laboratory Manual
Semester- 7th

Submitted To: Submitted By:


Prof. Sachindra Dubey Ayush Gupta
0201MT211012
List Of Experiments
Experiment-1: Implement Linear Regression of One Variable

Experiment-2: Implement Linear Regression of Multiple

Variable

Experiment-3: Apply Logistic Regression to solve the given


problem

Experiment-4: Apply Decision Tree to solve the given problem

Experiment-5: Implement Naive Bayes Classifier to solve the


given problem

Experiment-6: Implement K-Nearest Neighbors Classifier to solve


the given problem

Experiment-7: Apply Hierarchical Clustering to solve the given


problem

Experiment-8: Implement k-Means Clustering to solve the given


problem

Experiment-9: Implement Perceptrons to solve the given problem

Experiment-10: Apply Error Backpropagation Algorithm to solve


the given problem
Experiment-1: Implement Linear Regression of One Variable:
#Importing Libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# Load Data
df = pd.read_csv('../input/house-prices-advanced-regression-techniques/housetrain.csv')
df_float = df.select_dtypes(include=['float64']).copy()

# Fill missing values


for col in ['LotFrontage', 'MasVnrArea', 'GarageYrBlt']:
df_float[col].fillna(df[col].mean(), inplace=True)

# Regression function
def regression_analysis(feature, target):
lr = LinearRegression()
lr.fit(feature, target)
mse = mean_squared_error(target, lr.predict(feature))
return lr, mse

# Run regression analyses


salePrice = df['SalePrice']
lr1, mse1 = regression_analysis(df_float[['LotFrontage']], salePrice)
lr2, mse2 = regression_analysis(df_float[['MasVnrArea']],
salePrice) lr3, mse3 =
regression_analysis(df_float[['GarageYrBlt']], salePrice)

# Plot regression results


fig, axes = plt.subplots(1, 3, figsize=(24, 6))
features = ['LotFrontage', 'MasVnrArea', 'GarageYrBlt']
models = [lr1, lr2, lr3]
mses = [mse1, mse2, mse3]

for ax, feature, model in zip(axes, features, models):


ax.scatter(df_float[[feature]], salePrice, label=f'{feature}')
ax.plot(df_float[[feature]], model.predict(df_float[[feature]]),
color='orange') ax.set_xlabel(feature)
ax.set_ylabel('SalePrice')
plt.tight_layout(),
plt.show()

# Bar plot for MSE


plt.figure(figsize=(8, 6))
plt.bar(features, mses)
plt.ylabel("Mean Squared Error")
plt.show()
Output :
Experiment-2: Implement Linear Regression of Multiple Variable:
#Importing Libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import
train_test_split from sklearn import metrics

# Load dataset
df = pd.read_csv("/kaggle/input/cardataset/data.csv")
print(df.head(), "\n", df.shape, "\n", df.corr(), "\n", df.describe())

# Set X and y
X = df[['Weight', 'Volume']]
y = df['CO2']

# Exploratory Data Analysis


fig, axs = plt.subplots(2, figsize=(5,
5)) sns.boxplot(df['Weight'],
ax=axs[0]) sns.boxplot(df['Volume'],
ax=axs[1]) plt.tight_layout()
sns.distplot(df['CO2'])
sns.pairplot(df, x_vars=['Weight', 'Volume'], y_vars='CO2', height=4, aspect=1)
sns.heatmap(df.corr(), annot=True, cmap='coolwarm')
plt.show()

# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=100)

# Model training
reg_model = LinearRegression().fit(X_train, y_train)
print('Intercept:', reg_model.intercept_)
print('Coefficients:', dict(zip(X.columns, reg_model.coef_)))

# Predictions
y_pred = reg_model.predict(X_test)
reg_model_diff = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
print(reg_model_diff)

# Metrics
mae = metrics.mean_absolute_error(y_test,
y_pred) mse = metrics.mean_squared_error(y_test,
y_pred) rmse = np.sqrt(mse)

print(f'MAE: {mae}\nMSE: {mse}\nRMSE: {rmse}')


Output :
Experiment-3: Apply Logistic Regression to solve the given problem:
# Importing libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
from matplotlib.colors import ListedColormap

# Loading dataset and extracting variables


data = pd.read_csv('user_data.csv')
x, y = data.iloc[:, [2, 3]].values, data.iloc[:, 4].values

# Splitting data into training and test sets


x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=0)

# Feature scaling
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)

# Fitting Logistic Regression


classifier = LogisticRegression(random_state=0)
classifier.fit(x_train, y_train)

# Predicting and creating confusion matrix


y_pred = classifier.predict(x_test)
cm = confusion_matrix(y_test, y_pred)

# Visualizing the results


x_set, y_set = x_train, y_train
x1, x2 = np.meshgrid(np.arange(x_set[:, 0].min() - 1, x_set[:, 0].max() + 1, 0.01),
np.arange(x_set[:, 1].min() - 1, x_set[:, 1].max() + 1, 0.01))
plt.contourf(x1, x2, classifier.predict(np.array([x1.ravel(), x2.ravel()]).T).reshape(x1.shape),
alpha=0.75, cmap=ListedColormap(('purple', 'green')))
plt.xlim(x1.min(), x1.max())
plt.ylim(x2.min(), x2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1],
c=ListedColormap(('purple', 'green'))(i), label=j)
plt.title('Logistic Regression (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated
Salary') plt.legend()
plt.show()
Output :
Experiment-4: Apply Decision Tree to solve the given problem:
# Importing libraries
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix
from matplotlib.colors import ListedColormap

# Loading and preparing the dataset


df = pd.read_csv('Social_Network_Ads.csv')
X = df.iloc[:, 2:4].values
y = df.iloc[:, 4].values

# Splitting the dataset and scaling features


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Fitting Decision Tree classifier


clf = DecisionTreeClassifier(criterion='entropy', random_state=0)
clf.fit(X_train, y_train)

# Visualizing the training set results


X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start=X_set[:, 0].min() - 1, stop=X_set[:, 0].max() + 1, step=0.01),
np.arange(start=X_set[:, 1].min() - 1, stop=X_set[:, 1].max() + 1, step=0.01))
plt.contourf(X1, X2, clf.predict(np.array([X1.ravel(),
X2.ravel()]).T).reshape(X1.shape), alpha=0.75, cmap=ListedColormap(('red',
'green')))
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
c=ListedColormap(('red', 'green'))(i), label=j)
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
plt.title('Decision Tree Classifier (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated
Salary') plt.legend()
plt.show()

# Predicting test results and confusion matrix


y_pred = clf.predict(X_test)
cm = confusion_matrix(y_test, y_pred)
print(cm)
Output :
Experiment-5: Implement Naive Bayes Classifier to solve the given
problem:
# Importing libraries
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import confusion_matrix
from matplotlib.colors import ListedColormap

# Loading and preparing the dataset


df = pd.read_csv('Social_Network_Ads.csv')
X = df.iloc[:, 2:4].values
y = df.iloc[:, 4].values

# Splitting the dataset and scaling features


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Fitting Naive Bayes classifier


clf = GaussianNB()
clf.fit(X_train, y_train)

# Visualizing the training set results


X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start=X_set[:, 0].min() - 1, stop=X_set[:, 0].max() + 1, step=0.01),
np.arange(start=X_set[:, 1].min() - 1, stop=X_set[:, 1].max() + 1, step=0.01))
plt.contourf(X1, X2, clf.predict(np.array([X1.ravel(),
X2.ravel()]).T).reshape(X1.shape), alpha=0.75, cmap=ListedColormap(('red',
'green')))
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
c=ListedColormap(('red', 'green'))(i), label=j)
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
plt.title('Naive Bayes Classifier (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated
Salary') plt.legend()
plt.show()
# Predicting test results and confusion matrix
y_pred = clf.predict(X_test)
cm = confusion_matrix(y_test, y_pred)
print(cm)
Output :
Experiment-6: Implement K-Nearest Neighbors Classifier to solve the
given problem:

# Importing libraries
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix
from matplotlib.colors import ListedColormap
# Loading the dataset
df = pd.read_csv('Social_Network_Ads.csv')
X = df.iloc[:, 2:4].values
y = df.iloc[:, 4].values
# Splitting the dataset and scaling features
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# Fitting KNN classifier
clf = KNeighborsClassifier(n_neighbors=5, metric='minkowski', p=2)
clf.fit(X_train, y_train)
# Predicting the test set results
y_pred = clf.predict(X_test)
# Confusion matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
# Visualizing the training set results
X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start=X_set[:, 0].min() - 1, stop=X_set[:, 0].max() + 1, step=0.01),
np.arange(start=X_set[:, 1].min() - 1, stop=X_set[:, 1].max() + 1, step=0.01))
plt.contourf(X1, X2, clf.predict(np.array([X1.ravel(),
X2.ravel()]).T).reshape(X1.shape), alpha=0.75, cmap=ListedColormap(('red',
'green')))
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
c=ListedColormap(('red', 'green'))(i), label=j)
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
plt.title('KNN Classifier (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated
Salary') plt.legend()
plt.show()
Output :
Experiment-7: Apply Hierarchical Clustering to solve the given
problem:
# Importing necessary libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import scipy.cluster.hierarchy as sch
from sklearn.cluster import AgglomerativeClustering

# Loading the dataset


df = pd.read_csv('Mall_Customers.csv')
print(df.head())

# Extracting the relevant features


X = df.iloc[:, [3, 4]].values

# Using Dendrogram to find the optimal number of clusters


plt.figure(figsize=(8, 6))
dendrogram = sch.dendrogram(sch.linkage(X, method='ward'))
plt.title('Dendrogram')
plt.xlabel('Customers')
plt.ylabel('Euclidean
Distance') plt.show()

# Fitting the Agglomerative Hierarchical Clustering model


hc = AgglomerativeClustering(n_clusters=5, affinity='euclidean', linkage='ward')
y_hc = hc.fit_predict(X)

# Plotting the clusters


plt.figure(figsize=(8, 6))
plt.scatter(X[y_hc == 0, 0], X[y_hc == 0, 1], color='red', s=60, label='Cluster 1', edgecolors='black')
plt.scatter(X[y_hc == 1, 0], X[y_hc == 1, 1], color='green', s=60, label='Cluster 2',
edgecolors='black')
plt.scatter(X[y_hc == 2, 0], X[y_hc == 2, 1], color='blue', s=60, label='Cluster 3', edgecolors='black')
plt.scatter(X[y_hc == 3, 0], X[y_hc == 3, 1], color='yellow', s=60, label='Cluster 4',
edgecolors='black')
plt.scatter(X[y_hc == 4, 0], X[y_hc == 4, 1], color='cyan', s=60, label='Cluster 5', edgecolors='black')

# Titles and labels


plt.title('Hierarchical Clustering of Mall
Customers') plt.xlabel('Annual Income (k$)')
plt.ylabel('Spending Score (1-100)')
plt.legend()
plt.show()
Output :
Experiment-8: Implement k-Means Clustering to solve the given
problem:
# Importing libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Load dataset
df = pd.read_csv('Mall_Customers.csv')

# Selecting the features: Annual Income and Spending Score


X = df.iloc[:, [3, 4]].values
df.head()

# Import KMeans from sklearn


from sklearn.cluster import KMeans

# Plot setup for the Elbow Method


fig = plt.figure(figsize=(10, 8))
WCSS = []

# Using the Elbow Method to determine the optimal number of clusters


# Loop through different numbers of clusters to calculate Within-Cluster Sum of Squares
(WCSS)
for i in range(1, 11):
clf = KMeans(n_clusters=i, init='k-means++', max_iter=300, n_init=10, random_state=0)
clf.fit(X)
WCSS.append(clf.inertia_) # Inertia is the WCSS

# Plot WCSS to visualize the Elbow Method


plt.plot(range(1, 11), WCSS)
plt.title('The Elbow Method')
plt.ylabel('WCSS')
plt.xlabel('Number of Clusters')
plt.show()

# Applying KMeans clustering to the data with the optimal number of clusters (5 as determined
from the elbow method)
clf = KMeans(n_clusters=5, init='k-means++', max_iter=300, n_init=10, random_state=0)
y_kmeans = clf.fit_predict(X)

# Visualizing the clusters


fig = plt.figure(figsize=(10, 8))

# Plot each cluster with a unique color and label


plt.scatter(X[y_kmeans == 0, 0], X[y_kmeans == 0, 1], color='red', s=60, label='Cluster 1',
edgecolors='black')
plt.scatter(X[y_kmeans == 1, 0], X[y_kmeans == 1, 1], color='green', s=60, label='Cluster 2',
edgecolors='black')
plt.scatter(X[y_kmeans == 2, 0], X[y_kmeans == 2, 1], color='blue', s=60, label='Cluster 3',
edgecolors='black')
plt.scatter(X[y_kmeans == 3, 0], X[y_kmeans == 3, 1], color='yellow', s=60, label='Cluster 4',
edgecolors='black')
plt.scatter(X[y_kmeans == 4, 0], X[y_kmeans == 4, 1], color='cyan', s=60, label='Cluster 5',
edgecolors='black')

# Plotting the centroids of each cluster


plt.scatter(clf.cluster_centers_[:, 0], clf.cluster_centers_[:, 1], color='magenta', s=100,
label='Centroid', edgecolors='black')

# Adding labels and title


plt.legend()
plt.title('Clusters using KMeans')
plt.ylabel('Annual Income (k$)')
plt.xlabel('Spending Score (1-100)')
plt.show()

Output :
Experiment-9: Implement Perceptrons to solve the given problem:
# Importing libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification

# Generate classification data


features, targets = make_classification(n_samples=20, n_features=2, n_informative=1,
n_clusters_per_class=1, random_state=1)

# Plotting the data


plt.plot(features[targets == 0, 0], features[targets == 0, 1], 'P', markersize=10, label='Class 0')
plt.plot(features[targets == 1, 0], features[targets == 1, 1], '^', markersize=10, label='Class 1')
plt.legend(loc=2), plt.xlim(-2, 2), plt.ylim(-2, 2)
plt.xlabel("Feature $x_1$"), plt.ylabel("Feature
$x_2$") plt.grid(), plt.show()

# Perceptron class definition


class Perceptron:
def init (self, num_features):
self.weights = [0.0 for _ in range(num_features)]
self.bias = 0

def forward(self, x):


return int(np.dot(x, self.weights) + self.bias > 0)

def update(self, x, y_true):


error = y_true - self.forward(x)
self.bias += error
self.weights = [w + error * xi for w, xi in zip(self.weights, x)]
return error

# Train the perceptron


def train(model, X_train, y_train, epochs):
for epoch in range(epochs):
error_count = sum(abs(model.update(x, y)) for x, y in zip(X_train, y_train))
print(f"Epoch {epoch+1}: {error_count} errors")

# Compute accuracy
def compute_accuracy(model, X, y):
return sum(model.forward(x) == y_true for x, y_true in zip(X, y)) / len(y)

# Plot decision boundary


def plot_boundary(model):
w1, w2 = model.weights
b = model.bias
x_vals = np.array([-2, 2])
y_vals = -(w1 * x_vals + b) / w2
return x_vals, y_vals
# Model training and evaluation
ppn = Perceptron(num_features=2)
train(ppn, features, targets,
epochs=5)
print("Model Accuracy:", compute_accuracy(ppn, features, targets))

# Plotting decision boundary


x_vals, y_vals = plot_boundary(ppn)
plt.plot(features[targets == 0, 0], features[targets == 0, 1], 'D', markersize=10, label='Class 0')
plt.plot(features[targets == 1, 0], features[targets == 1, 1], '^', markersize=10, label='Class 1')
plt.plot(x_vals, y_vals, 'k'), plt.legend(loc=2)
plt.xlim([-2, 2]), plt.ylim([-2, 2]), plt.xlabel("Feature $x_1$"), plt.ylabel("Feature $x_2$")
plt.grid(), plt.show()

Output :
Experiment-10: Apply Error Backpropagation Algorithm to solve the
given problem:
# Importing libraries
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

# Load dataset
data = load_iris()
X = data.data
y = pd.get_dummies(data.target).values

# Split data into train and test sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=20, random_state=4)

# Hyperparameters and initialization


learning_rate = 0.1
iterations = 5000
input_size, hidden_size, output_size = 4, 2, 3
N = y_train.size
np.random.seed(10)

# Initialize weights
W1 = np.random.normal(scale=0.5, size=(input_size, hidden_size))
W2 = np.random.normal(scale=0.5, size=(hidden_size, output_size))

# Activation function and metrics


def sigmoid(x): return 1 / (1 + np.exp(-x))

def mean_squared_error(y_pred, y_true): return ((y_pred - y_true)**2).mean()

def accuracy(y_pred, y_true): return (y_pred.argmax(axis=1) == y_true.argmax(axis=1)).mean()

# Training
for itr in range(iterations):
# Feedforward
A1 = sigmoid(np.dot(X_train, W1))
A2 = sigmoid(np.dot(A1, W2))

# Backpropagation
dW2 = (A2 - y_train) * A2 * (1 - A2)
dW1 = np.dot(dW2, W2.T) * A1 * (1 - A1)

W2 -= learning_rate * np.dot(A1.T, dW2) / N


W1 -= learning_rate * np.dot(X_train.T, dW1) / N

# Test Accuracy
A1_test = sigmoid(np.dot(X_test, W1))
A2_test = sigmoid(np.dot(A1_test, W2))
acc = accuracy(A2_test, y_test)
print(f"Accuracy: {acc}")

Output:

You might also like