0% found this document useful (0 votes)
7 views19 pages

ML Surya

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)
7 views19 pages

ML Surya

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

FACULTY OF ENGINEERING AND

TECHNOLOGY
Department of Computer Engineering
01CE0524 – Machine Learning – Lab #1

Practical 1 : Implement Naive Bayes algorithm using sample data.

Output:-

#Displaying iris dataset

Suryakant Kumar (92301703051) 1


FACULTY OF ENGINEERING AND
TECHNOLOGY
Department of Computer Engineering
01CE0524 – Machine Learning – Lab #1

#Displaying X_test dataset

Suryakant Kumar (92301703051) 2


FACULTY OF ENGINEERING AND
TECHNOLOGY
Department of Computer Engineering
01CE0524 – Machine Learning – Lab #1

#Model name

#Printing accuracy

Code:-

#importing libraries

from sklearn import datasets

from sklearn.model_selection import train_test_split

from sklearn.naive_bayes import GaussianNB

from sklearn.metrics import accuracy_score

#load datasets

iris=datasets.load_iris()

iris

#assigning dataset to variable


X=iris.data
y=iris.target

#split the data


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1)

Suryakant Kumar (92301703051) 3


FACULTY OF ENGINEERING AND
TECHNOLOGY
Department of Computer Engineering
01CE0524 – Machine Learning – Lab #1

#displaying X_test
X_test

#intialize gaussian naive bayes classifier and assign it to the variable


nb=GaussianNB()

#train model
nb.fit(X_train,y_train)

#using predict method to predict and storing in variable


y_pre=nb.predict(X_test)

#printing accuracy
accuracy=accuracy_score(y_test,y_pre)
print(accuracy)

Suryakant Kumar (92301703051) 4


FACULTY OF ENGINEERING AND
TECHNOLOGY
Department of Computer Engineering
01CE0524 – Machine Learning – Lab #2

Practical 2:- Implement Random Forest and ensemble learning techniques.

Output:-

#Displaying iris dataset

#Model Name

Suryakant Kumar (92301703051) 5


FACULTY OF ENGINEERING AND
TECHNOLOGY
Department of Computer Engineering
01CE0524 – Machine Learning – Lab #2

#Printing accuracy

Code:-

#importing libraries
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# Load and displaying the iris dataset


iris = datasets.load_iris()
iris
X = iris.data
y = iris.target

# Split the data


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1)

# Create the Random Forest classifier


rf = RandomForestClassifier(n_estimators=100, random_state=1)

# Train the model


rf.fit(X_train, y_train)

# Predict
y_pred = rf.predict(X_test)

#Printing accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

Suryakant Kumar (92301703051) 6


FACULTY OF ENGINEERING AND
TECHNOLOGY
Department of Computer Engineering
01CE0524 – Machine Learning – Lab #3

Practical 3:- Using a dataset implement SVM classifier.

Output:

#Displaying bank-full dataset

#Display basic information

Suryakant Kumar(92301703051) 7
(5EC4)
FACULTY OF ENGINEERING AND
TECHNOLOGY
Department of Computer Engineering
01CE0524 – Machine Learning – Lab #3

#Display statistical summary of numeric columns

#Display first few rows of dataset(df.head(5))

#Display last few rows of dataset(df.tail(5))

#Check for null (missing) values in the column

Suryakant Kumar(92301703051) 8
(5EC4)
FACULTY OF ENGINEERING AND
TECHNOLOGY
Department of Computer Engineering
01CE0524 – Machine Learning – Lab #3

#Check whether all values in the column are unique

#Count total number of missing values in each column of the dataset

#Encode all categorical (object-type) columns into numeric using LabelEncoder

#Model Name

#Printing Accuracy

Suryakant Kumar(92301703051) 9
(5EC4)
FACULTY OF ENGINEERING AND
TECHNOLOGY
Department of Computer Engineering
01CE0524 – Machine Learning – Lab #3

Code:-

#Importing libraries
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import LabelEncoder
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score

# Read the CSV file and displaying bank-full dataset


df = pd.read_csv('/content/bank-full.csv')
df

#Display basic information about the dataset


df.info()

#Display statistical summary of numeric columns


df.describe()

# Display the first few rows of the Dataset


display(df.head())

#Display last few rows of Dataset


display(df.tail())

#Check for null (missing) values in the column


df['loan'].isnull()

#Check whether all values in the column are unique


df['loan'].is_unique

#Count total number of missing values in each column of the dataset


df.isnull().sum()

Suryakant Kumar (92301703051) 10


(5EC5)
FACULTY OF ENGINEERING AND
TECHNOLOGY
Department of Computer Engineering
01CE0524 – Machine Learning – Lab #3

#Encode all categorical (object-type) columns into numeric using LabelEncoder


for i in df.columns:
if df[i].dtype==object:
df[i]=LabelEncoder().fit_transform(df[i])
df.head()

#Separate features and target variable — remove 'Target' from features


x = df.drop('Target',axis=1)
y=df['Target']

#Split the data


x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.3,random_state=42)

#Normalize the feature data using StandardScaler for better SVM performance
scaler = StandardScaler()
x_train = scaler.fit_transform(x_train)
x_test = scaler.transform(x_test)

#Initialize the Support Vector Classifier (SVC) model


svm=SVC()

#Train the model


svm.fit(x_train,y_train)

#Make predictions using the trained SVM model on the test dataset
y_pre=svm.predict(x_test)

#Printing Accuracy
accuracy=accuracy_score(y_test,y_pre)
print(accuracy)

Suryakant Kumar (92301703051) 11


(5EC5)
Practical 4:Implement classification techniques evaluation parameters
using sample dataset

Output:
#Rows represent individual transaction records.

# Drop the Nameorig and nameDest

Suryakant Kumar (92301703051) 12


(5EC5)
#It converts the categorical values in the type
column in numerical labels

#Define features and target

#It trains a Decision Tree model on the training


data X_train and y_train.

Suryakant Kumar (92301703051) 13


(5EC5)
#It prints the performance metrics (precision,
recall, f1-score) and the confusion matrix of the
Decision Tree model on the test data.

Suryakant Kumar (92301703051) 14


(5EC5)
Code :

# Import required libraries


import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import classification_report, confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt

df=pd.read_csv("/content/transactions.csv")
df

df = df.drop(['nameOrig', 'nameDest'], axis=1)


df
le = LabelEncoder()
df['type'] = le.fit_transform(df['type'])
df
# Step 4: Define features and target
X = df.drop('isFraud', axis=1)
y = df['isFraud']
Y
#It trains a Decision Tree model
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42,
stratify=y)
dt_model = DecisionTreeClassifier(random_state=42)
dt_model.fit(X_train, y_train)
y_pred = dt_model.predict(X_test)
print("=== Decision Tree Classification Report ===")
print(classification_report(y_test, y_pred))
print("=== Confusion Matrix ===") print(confusion
matrix(y_test, y_pred))

Suryakant Kumar (92301703051) 15


(5EC5)
FACULTY OF ENGINEERING AND
TECHNOLOGY
Department of Computer Engineering
01CE0524 – Machine Learning – Lab #2

Practical 5:- Develop a cost function of linear regression using sample


dataset.

Output:-

#Displaying graph of cost function history

#Displaying Graph of Linear Regression

Suryakant Kumar (92301703051) 2


(5EC5)
FACULTY OF ENGINEERING AND
TECHNOLOGY
Department of Computer Engineering
01CE0524 – Machine Learning – Lab #2

#Printing optimized parameters

Code:-

#importing libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Step 1: Load the Salary Data CSV file


# Assuming the CSV file has columns 'YearsExperience' and 'Salary'
df=pd.read_csv('/content/Salary_Data.csv')

# Step 2: Extract the input (x) and output (y) data


x = df['YearsExperience'].values # Years of experience
y = df['Salary'].values # Salary

# Step 3: Number of data points


m = len(x)

# Step 4: Prepare the data (add column of ones for intercept term)
X = np.c_[np.ones(m), x] # Adds a column of 1s for theta_0 (intercept term)

Suryakant Kumar (92301703051) 2


(5EC5)
FACULTY OF ENGINEERING AND
TECHNOLOGY
Department of Computer Engineering
01CE0524 – Machine Learning – Lab #2

# Step 5: Initialize parameters (theta_0 and theta_1)


theta = np.zeros(2) # [theta_0, theta_1]

# Step 6: Learning rate and number of iterations for gradient descent


learning_rate = 0.01
iterations = 1000

# Step 7: Define the cost function (Mean Squared Error)


def cost_function(X, y, theta):
predictions = X.dot(theta)
cost = (1/(2*m)) * np.sum((predictions - y) ** 2)
return cost

# Step 8: Gradient descent function to optimize theta


def gradient_descent(X, y, theta, learning_rate, iterations):
cost_history = []

for _ in range(iterations):
predictions = X.dot(theta)
errors = predictions - y
gradient = (1/m) * X.T.dot(errors) # Gradient of the cost function
theta -= learning_rate * gradient # Update the parameters

cost_history.append(cost_function(X, y, theta)) # Record cost for each iteration

return theta, cost_history

# Step 9: Run gradient descent to fit the model


theta_optimized, cost_history = gradient_descent(X, y, theta, learning_rate, iterations)

# Step 10: Output optimized parameters


print(f"Optimized parameters: theta_0 = {theta_optimized[0]}, theta_1 =
{theta_optimized[1]}")

Suryakant Kumar (92301703051) 2


(5EC5)
FACULTY OF ENGINEERING AND
TECHNOLOGY
Department of Computer Engineering
01CE0524 – Machine Learning – Lab #2

# Step 11: Plot the cost function history


plt.plot(range(iterations), cost_history)
plt.xlabel('Iterations')
plt.ylabel('Cost (J)')
plt.title('Cost Function History during Gradient Descent')
plt.show()

# Step 12: Plotting the data and the regression line


plt.scatter(x, y, color='red', label='Data points')
plt.plot(x, X.dot(theta_optimized), label='Regression Line', color='blue')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.title('Linear Regression: Experience vs Salary')
plt.legend()
plt.show()

Suryakant Kumar (92301703051) 2


(5EC5)

You might also like