Experiment No.: 8: T. Y. B. Tech (CSE) - II Subject: Open Source Lab-II
Experiment No.: 8: T. Y. B. Tech (CSE) - II Subject: Open Source Lab-II
Experiment No.: 8
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.
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
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: