You are on page 1of 55

TABLE OF CONTENTS

S.NO Date Title Page No Mark Sign

1. 13.06.22 ID3 01

2. 20.06.22 C 4.5 06

3. 02.07.22 CLASSIFICATION and REGRESSION TREE 12

4. 04.07.22 17
APRIORI

5. 11.07.22 EQUIVALENCE CLASS TRANSFORMATION 24

6. 11.07.22 NAÏVE BAYES ENSEMBLE 30

7. 18.07.22 RANDOM FOREST 34

8. 25.07.22 ADA BOOST 40

9. 01.08.22 XG BOOST 44

10. 08.08.22 SIMPLE NEURAL NEWORK 49


Page |1

Exp No : 01 Iterative Dichotomiser 3

Date : 13.06.22

AIM:
To Write a Python Program to Implement Iterative Dichomotiser 3 algorithm.

ALGORITHM:

1. install chefboost package

2.from chefboost package import Chefboost module.

3.Import dataset

4.Use fit function to fit the algorithm to the dataset

5.Predict the output.

6..Entropy:

7.Information Gain:
Page |2

Program:

#installing required package

pip install chefboost

#import required packages

from chefboost import Chefboost as chef


import pandas as pd
Page |3

#import dataset

df = pd.read_csv("golf.txt")
df
Page |4

#creating model

config = {'algorithm': 'ID3'}


model = chef.fit(df, config = config, target_label = 'Decision')

df
Page |5

#prediction

prediction = chef.predict(model, param = ['Sunny', 'Hot', 'High', 'Weak'])


prediction

prediction = chef.predict(model, param = ['Rain', 'Mild', 'Normal', 'Weak'])


prediction

Criteria Maximum Marks Marks Obtained

Aim and Algorithm 05


Program and Output 15
Viva 05
Total 25

RESULT:
Thus, the Python program to implement ID3 algorithm was implemented successfully
and the output is verified.
Page |6

Exp No : 02 C 4.5
Date : 20.06.22

AIM:
To Write a Python Program to Implement C 4.5 algorithm.

ALGORITHM:

1. Import Chefboost package

2. Import Pandas

3. Load the dataset using pandas

4. Set the algorithm as C4.5 and assign it to a variable config

5. Fit the model with the dataset,target label and config

6. Predict the output

7.Gain ratio:

8.Gain:

9.Split info:
Page |7

Program:

#installing required package

pip install chefboost

#import required packages

from chefboost import Chefboost as chef


import pandas as pd

#import dataset

df = pd.read_csv("golf.txt")
df
Page |8
Page |9

#creating model

config = {'algorithm': 'C4.5'}


model = chef.fit(df, config = config, target_label = 'Decision')

df
P a g e | 10

#prediction

prediction = chef.predict(model, param = ['Sunny', 'Hot', 'High', 'Weak'])


prediction

prediction = chef.predict(model, param = ['Rain', 'Mild', 'Normal', 'Weak'])


prediction
P a g e | 11

Criteria Maximum Marks Marks Obtained

Aim and Algorithm 05


Program and Output 15
Viva 05
Total 25

RESULT:
Thus, the Python program to implement ID3 algorithm was implemented successfully
and the output is verified.
P a g e | 12

Exp No : 03 Classification and Regression Tree


Date : 02.07.22

AIM:

To Write a Python Program to Implement Classification and Regression Tree


algorithm.

ALGORITHM:

1. Import Chefboost package

2. Import Pandas

3. Load the dataset using pandas

4. Set the algorithm as CART and assign it to a variable config

5. Fit the model with the dataset,target label and config

6. Predict the output

Gini index:
P a g e | 13

Program:

#installing required package

pip install chefboost

#import required packages

from chefboost import Chefboost as chef


import pandas as pd

#import dataset

df = pd.read_csv("mammals.csv")
df
P a g e | 14

#creating model
config = {'algorithm': 'C4.5'}
model = chef.fit(df, config = config, target_label = 'Decision')
P a g e | 15

df

#prediction

config = {'algorithm': 'CART'}


model = chef.fit(df, config = config, target_label = 'danger')

prediction = chef.predict(model, param = ['Yellow-


belliedmarmot',4.050,17.0,'NaN','NaN','NaN',13.0,38.0,3,1,1])
prediction

prediction = chef.predict(model, param =


["Vervet",4.190,58.0,9.7,0.6,10.3,24.0,210.0,4,3,4])
prediction
P a g e | 16

Criteria Maximum Marks Marks Obtained

Aim and Algorithm 05


Program and Output 15
Viva 05
Total 25

RESULT:
Thus, the Python program to implement CART algorithm was implemented
successfully and the output is verified.
P a g e | 17

Exp No : 04 APRIORI
Date : 04.07.22

AIM:
To Write a Python Program to implement Apiori algorithm.

ALGORITHM:

1.Install apyori package

2.Import required pacakages like pandas , numpy and matplotlib

3.import apriori from apyori package

4.import the dataset

5.preporocess the dataset for further analysis

6.train the apriori model on the dataset

7. Computing the support for each individual item

8. Deciding on the support threshold


9. Selecting the frequent items
10. Finding the support of the frequent itemsets
11.Repeat for larger sets
12. Generate Association Rules and compute confidence
13. Compute lift

PROGRAM:
#installing required packages

!pip install apyori


P a g e | 18

#importing required packages

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

#data preprocessing

dataset = pd.read_csv('groceries.csv',on_bad_lines='skip')
transactions = []
dataset

#dataset shape

dataset.shape
P a g e | 19

#assigning values to transactions list

for i in range(0, 6106):


transactions.append([str(dataset.values[i,j]) for j in range(0, 4)])
transactions

#creating model

from apyori import apriori


rules = apriori(transactions = transactions, min_support = 0.002, min_confidence = 0.1,
min_lift = 2, min_length = 2, max_length = 2)
print(rules)
P a g e | 20

#visualizing the result

results = list(rules)

def inspect(results):
lhs = [tuple(result[2][0][0])[0] for result in results]
rhs = [tuple(result[2][0][1])[0] for result in results]
supports = [result[1] for result in results]
confidences = [result[2][0][2] for result in results]
lifts = [result[2][0][3] for result in results]
return list(zip(lhs, rhs, supports, confidences, lifts))
resultsinDataFrame = pd.DataFrame(inspect(results), columns = ['Left Hand Side', 'Right
Hand Side', 'Support', 'Confidence', 'Lift'])
P a g e | 21

resultsinDataFrame
P a g e | 22

#displaying the results sorted by descending lifts

resultsinDataFrame.nlargest(n = 10, columns = 'Lift')


P a g e | 23

Criteria Maximum Marks Marks Obtained

Aim and Algorithm 05


Program and Output 15
Viva 05
Total 25

RESULT:
Thus, the Python program to implement Apriori algorithm was implemented
successfully and the output is verified.
P a g e | 24

Exp No : 05 Equivalence Class Transformation


Date : 11.07.22

AIM:
To Write a Python Program to implement Equivalence Class Transformation
algorithm.

ALGORITHM:

1.Install apyori package

2.Import required pacakages like pandas , numpy and matplotlib

3.import apriori from apyori package

4.import the dataset

5.preporocess the dataset for further analysis

6.train the apriori model on the dataset

7. Computing the support for each individual item


8. Deciding on the support threshold
9. Selecting the frequent items
10. Finding the support of the frequent itemsets
11. Repeat for larger sets
12. Generate Association Rules and compute confidence
13. Compute lift

PROGRAM:
#installing required packages

!pip install apyori


P a g e | 25

#importing required packages

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

#data preprocessing

dataset = pd.read_csv('groceries.csv',on_bad_lines='skip')
transactions = []
dataset

#dataset shape

dataset.shape
P a g e | 26

#assigning values to transactions list

for i in range(0, 6106):


transactions.append([str(dataset.values[i,j]) for j in range(0, 4)])
transactions

#creating model

from apyori import apriori


rules = apriori(transactions = transactions, min_support = 0.002, min_confidence = 0.1,
min_lift = 2, min_length = 2, max_length = 2)
print(rules)
P a g e | 27

#visualizing the result

results = list(rules)

def inspect(results):
lhs = [tuple(result[2][0][0])[0] for result in results]
rhs = [tuple(result[2][0][1])[0] for result in results]
supports = [result[1] for result in results]
return list(zip(lhs, rhs, supports))
resultsinDataFrame = pd.DataFrame(inspect(results), columns = ['Product 1', 'Product 2',
'Support'])
P a g e | 28

resultsinDataFrame

#displaying the results sorted by descending lifts


resultsinDataFrame.nlargest(n = 10, columns = 'Lift')
P a g e | 29

Criteria Maximum Marks Marks Obtained

Aim and Algorithm 05


Program and Output 15
Viva 05
Total 25

RESULT:
Thus, the Python program to implement E-Clat algorithm was implemented
successfully and the output is verified.
P a g e | 30

Exp No : 06 Naïve bayes Ensemble


Date : 11.07.22

AIM:
To Write a Python Program to implement Naïve Bayes Ensemble

ALGORITHM:
1. Start the program
2. Import the necessary packages like numpy,matplotlib,seaborn,
3. Import the dataset
4. Assign the independent and dependant variables
5. Split the datasets using train_test_split
6. Preprocess the data using fit_transform function
7. Import module to perform naive bayes using the command
from sklearn.naive_bayes import BernoulliNB
8. Fit the model with training datasets
9.Predict the output
10.Compute the accuracy of the model
11.Perform ensemble by importing Adaboost using the command
from sklearn.ensemble import AdaBoostClassifier
and Naive bayes using GaussanNB()
12.Assign the model to a variable nb and pass it to the AdaBoostClassifier model
13.Fit the model with training datasets
14. Predict the output

PROGRAM:
#importing required packages

# importing the libraries


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
P a g e | 31

#importing Dataset

dataset = pd.read_csv('NaiveBayes.csv')

# split the data into inputs and outputs

X = dataset.iloc[:, [0,1]].values
y = dataset.iloc[:, 2].values
# split the data into training and testing data

from sklearn.model_selection import train_test_split


# assign test data size 25%
X_train, X_test, y_train, y_test =train_test_split(X,y,test_size= 0.25, random_state=0)

#Preprocessing

# importing standard scaler


from sklearn.preprocessing import StandardScaler

# scalling the input data


sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.fit_transform(X_test)

#Model-Naïve bayes

from sklearn.naive_bayes import BernoulliNB

# initializaing the NB
classifer = BernoulliNB()

# training the model


classifer.fit(X_train, y_train)

# testing the model


y_pred = classifer.predict(X_test)
P a g e | 32

#accuracy

# importing accuracy score


from sklearn.metrics import accuracy_score

# printing the accuracy of the model


print(accuracy_score(y_pred, y_test))

#Ensemble

from sklearn.ensemble import AdaBoostClassifier


from sklearn.naive_bayes import GaussianNB

nb = GaussianNB()
model = AdaBoostClassifier(base_estimator=nb, n_estimators=10)
model.fit(X_train, y_train)

#Prediction

model.predict(X_test)
P a g e | 33

Criteria Maximum Marks Marks Obtained

Aim and Algorithm 05


Program and Output 15
Viva 05
Total 25

RESULT:
Thus, the Python program to implement Naïve Bayes Ensemble was implemented
successfully and the output is verified.
P a g e | 34

Exp No : 07 Random Forest


Date : 18.07.22

AIM:
To Write a Python Program to implement Random Forest algorithm.

ALGORITHM:
1.First, start with the selection of random samples from a given dataset.
2.Next, this algorithm will construct a decision tree for every sample. Then it will
get the prediction result from every decision tree.
3.In this step, voting will be performed for every predicted result.
4.At last, select the most voted prediction result as the final prediction result.

PROGRAM:
#importing dataset

import pandas as pd
features = pd.read_csv('temps.csv')
features.head(5)

#checking shape

print('The shape of our features is:', features.shape)


P a g e | 35

#describe function
features.describe()

#preprocessing data

features = pd.get_dummies(features)
features
P a g e | 36

#splittting train test data

import numpy as np
labels = np.array(features['actual'])
features= features.drop('actual', axis = 1)
feature_list = list(features.columns)
features = np.array(features)
from sklearn.model_selection import train_test_split
train_features, test_features, train_labels, test_labels = train_test_split(features,
labels, test_size = 0.25,
random_state = 42)
P a g e | 37

#train test data shapes

print('Training Features Shape:', train_features.shape)


print('Training Labels Shape:', train_labels.shape)
print('Testing Features Shape:', test_features.shape)
print('Testing Labels Shape:', test_labels.shape)

#baseline error

baseline_preds = test_features[:, feature_list.index('average')]


baseline_errors = abs(baseline_preds - test_labels)
print('Average baseline error: ', round(np.mean(baseline_errors), 2))

#creating model

from sklearn.ensemble import RandomForestRegressor


rf = RandomForestRegressor(n_estimators = 1000, random_state = 42)
rf.fit(train_features, train_labels);
rf

#prediction

predictions = rf.predict(test_features)
predictions
P a g e | 38

#mean absolute error

predictions = rf.predict(test_features)
errors = abs(predictions - test_labels)
print('Mean Absolute Error:', round(np.mean(errors), 2), 'degrees.')

#accuracy

mape = 100 * (errors / test_labels)


accuracy = 100 - np.mean(mape)
print('A

ccuracy:', round(accuracy, 2), '%.')


P a g e | 39

Criteria Maximum Marks Marks Obtained

Aim and Algorithm 05


Program and Output 15
Viva 05
Total 25

RESULT:
Thus, the Python program to implement Random Forest algorithm was implemented
successfully and the output is verified.
P a g e | 40

Exp No : 08 Ada Boost


Date : 25.07.22

AIM:
To Write a Python Program to implement Ada Boost algorithm.

ALGORITHM:
1. Creating the First Base Learner
2. Calculating the Total Error (TE)
3. Updating Weights
4. Creating a New Dataset
5. Final Predictions

PROGRAM:

#import required packages

import pandas as pd

#import dataset

credit = pd.read_csv("CreditCardDefault.csv")
credit.drop(["ID"], axis=1, inplace=True)
credit
P a g e | 41

#splitting dependent and independent variables

X = credit.iloc[:, 0:23]
y = credit.iloc[:, -1]

#splitting train test data

from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size= 0.3, stratify = y)
P a g e | 42

#train test data shapes

print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)

#creating model

from sklearn import preprocessing


scaler = preprocessing.StandardScaler().fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
from sklearn.ensemble import AdaBoostClassifier
ada = AdaBoostClassifier(n_estimators = 10,
learning_rate = 1.0,
random_state = 123)
ada.fit(X_train, y_train)

#prediction

ada_pred = ada.predict(X_test)
ada_pred[1]
P a g e | 43

#accuracy

from sklearn.metrics import accuracy_score


accuracy_score(y_test, ada_pred)

Criteria Maximum Marks Marks Obtained

Aim and Algorithm 05


Program and Output 15
Viva 05
Total 25

RESULT:
Thus, the Python program to implement AdaBoost algorithm was implemented
successfully and the output is verified.
P a g e | 44

Exp No : 09 XG Boost
Date : 01.08.22

AIM:
To Write a Python Program to implement XG Boost algorithm.

ALGORITHM:
1. Load all the libraries
2. Load the dataset
3. Data Cleaning & Feature Engineering
4. Tune and Run the model

PROGRAM:
#import required packages

import pandas as pd

#import dataset

df=pd.read_csv("diabetes.csv")
df
P a g e | 45

#splitting dependent and independent variables

X = df.iloc[:, 0:8]
y = df.iloc[:, 8]

#splitting train test data

from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size= 0.3,
random_state=42)
#train test data shapes

print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)

#scaler transform

from sklearn import preprocessing


scaler = preprocessing.StandardScaler().fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)

#creating model

import xgboost
from xgboost import XGBClassifier
(XGBClassifier)
P a g e | 46

xgbt = XGBClassifier(max_depth = 2,
learning_rate = 0.2,
objective = "multi:softmax",
num_class = 2,
booster = "gbtree",
n_estimarors = 10,
random_state = 123)
model=xgbt.fit(X_train, y_train)
model
P a g e | 47

#prediction

xgbt_pred = xgbt.predict(X_test)

#accuracy

print(accuracy_score(y_test, xgbt_pred))
print(xgbt.score(X_train, y_train))
print(xgbt.score(X_test, y_test))

#plotting

from xgboost import plot_importance


import matplotlib.pyplot as plt
plot_importance(xgbt)
P a g e | 48

Criteria Maximum Marks Marks Obtained

Aim and Algorithm 05


Program and Output 15
Viva 05
Total 25

RESULT:
Thus, the Python program to implement XG Boost algorithm was implemented
successfully and the output is verified.
P a g e | 49

Exp No : 10 Simple Neural Network


Date : 08.08.22

AIM:
To Write a Python Program to implement Simple Neural Network.

ALGORITHM:
1. Start the program
2. Import the necessary packages like numpy,matplotlib,keras and PCA
3.Import the dataset
4. Plot the graph
5. Split the datasets using train_test_split
6.Plot the numbers
7.Flatten the data
8.Import keras module to define the Neural Network
9.Create the model and display the summary
10.Fit the model with training datasets
11.Plot the history
12.Compute the accuracy of the model
13.Predict the output

PROGRAM:
#import required packages

%matplotlib inline

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow import keras
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
P a g e | 50

#import dataset

iris = load_iris()
df = pd.DataFrame(iris.data, columns = iris.feature_names)
df['class'] = iris.target_names[iris.target]

#splitting dependent and independent variables

(X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()

# Normalize
X_train = X_train / 255
X_test = X_test / 255

#Plotting

plt.figure(figsize=(10, 3))
for i in range(30):
plt.subplot(3, 10, i + 1)
plt.imshow(X_train[i], cmap='gray')
plt.axis('off')
plt.show()

# To flatten data

X_train_flat = X_train.reshape((X_train.shape[0], 28*28))


X_test_flat = X_test.reshape((X_test.shape[0], 28*28))

y_train_one_hot = np.eye(10)[y_train]
y_test_one_hot = np.eye(10)[y_test]
P a g e | 51

#define the NN architecture using Keras

import tensorflow.keras as keras


from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam

#Model

model = keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[28, 28]))
model.add(keras.layers.Dense(64, activation="relu"))
model.add(keras.layers.Dense(32, activation="relu"))
model.add(keras.layers.Dense(10, activation="softmax"))
model.compile(loss="sparse_categorical_crossentropy",
optimizer="sgd",
metrics=["accuracy"])
model.summary()

# fit the model

loss = model.fit(X_train, y_train, epochs=20, validation_split=0.2)


P a g e | 52

# Plot the history

pd.DataFrame(loss.history).plot()

# Accuracy

pred = model.predict(X_test).argmax(axis=1)
print('Accuracy on test set - {0:.02%}'.format((pred == y_test).mean()))
P a g e | 53

# Prediction

plt.figure(figsize=(10, 10))
for label in range(10):
for i in range(10):
plt.subplot(10, 10, label * 10 + i + 1)
plt.imshow(X_test[pred == label][i], cmap='gray')
plt.axis('off')
plt.show()
P a g e | 54

Criteria Maximum Marks Marks Obtained

Aim and Algorithm 05


Program and Output 15
Viva 05
Total 25

RESULT:
Thus, the Python program to implement Simple neural network was implemented
successfully and the output is verified.

You might also like