0% found this document useful (0 votes)
31 views2 pages

Experiment No.: 8: T. Y. B. Tech (CSE) - II Subject: Open Source Lab-II

This document describes an experiment to compare different classification algorithms. It discusses importing libraries and reading data, splitting the data into training and test sets, creating an array of classifiers using algorithms like SVM, decision trees, and random forests. Each algorithm is fit to the training data and evaluated on the test data by measuring accuracy and confusion matrix. Comparing the results across algorithms allows one to select the best performing model. The key is to evaluate algorithms on held-out test data to identify the winner for a given problem.

Uploaded by

ASHISH MALI
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)
31 views2 pages

Experiment No.: 8: T. Y. B. Tech (CSE) - II Subject: Open Source Lab-II

This document describes an experiment to compare different classification algorithms. It discusses importing libraries and reading data, splitting the data into training and test sets, creating an array of classifiers using algorithms like SVM, decision trees, and random forests. Each algorithm is fit to the training data and evaluated on the test data by measuring accuracy and confusion matrix. Comparing the results across algorithms allows one to select the best performing model. The key is to evaluate algorithms on held-out test data to identify the winner for a given problem.

Uploaded by

ASHISH MALI
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/ 2

T. Y. B.

Tech (CSE) - II Subject: Open Source Lab-II

Experiment No.: 8

Title: Write a program to compare different classification algorithms.


Objectives:
1. To learn how to compare different classification algorithms.

Theory:
In machine learning no one algorithm works well for every problem. This is widely
applicable in Prediction Models where we train our dataset on an algorithm and later use the
trained model for predictions on new data. As a result, you should try many different
algorithms for your problem, while using a hold-out “test set” of data to evaluate
performance and select the winner.
Scikit-learn machine learning library can be implement different classification algorithms.
Scikit-learn is a free machine learning library for Python. It features various algorithms like
support vector machine, random forests, and k-neighbours, and it also supports Python
numerical and scientific libraries like NumPy and SciPy
#Import Libraries and Read the data
import pandas as pd
import numpy as np
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.ensemble import RandomForestClassifier
from sklearn import svm, tree
from sklearn.model_selection import train_test_split
data = pd.read_csv("Iris.csv")
#Create Dependent and Independent Datasets based on our #Dependent #and
Independent features
X = data[['SepalLengthCm','SepalWidthCm','PetalLengthCm']]
y= data['Species']
#Split the Data into Training and Testing sets with test size as #30%
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3, shuffle=True)

Now, we will create an array of Classifiers and append different classification models to our
array.

Department of Computer Science & Engineering,


Textile and Engineering Institute, Ichalkaranji. Page 1
T. Y. B. Tech (CSE) - II Subject: Open Source Lab-II

Model1 = svm.SVC()
classifiers.append(model1)
Model2 = tree.DecisionTreeClassifier()
classifiers.append(model2)
Model3 = RandomForestClassifier()
classifiers.append(model3)

We will fit our algorithms in our classifiers array on Train dataset and check the accuracy and
confusion matrix for our test dataset prediction given by different algorithms

for clf in classifiers:


clf.fit(X_train, y_train)
y_pred= clf.predict(X_test)
acc = accuracy_score(y_test, y_pred)
print("Accuracy of %s is %s"%(clf, acc))
cm = confusion_matrix(y_test, y_pred)
print("Confusion Matrix of %s is %s"%(clf, cm)

You can apply above technique to any other prediction model to find out the best algorithm
among a pool of different algorithms.
Key concepts: comparing classification algorithms
Algorithm:

 Read data set into Pandas Dataframes.


 Split the Data into Training and Testing sets
 Train Model for different Classification Algorithms namely Decision Tree, Logistic
Regression, Naïve Bayes, Stochastic Gradient Descent, Support Vector Machine
(SVM) Classifier, Random Forest Classifier.
 Select the Best Algorithm

Department of Computer Science & Engineering,


Textile and Engineering Institute, Ichalkaranji. Page 2

You might also like