This tutorial is prepared by Dr. Sanasam Ranbir Singh
Classification using scikit-learn python library
Scikit-learn (or sklearn) is a free software machine learning library for the Python programming language. It supports various machine learning methods such as feature selection, classification, regression, and clustering.
In this lesson, we learn how to build various classification models using sklearn library.
How to install sklearn library? install.
Naive Bayes Classifier
In [7]:
from sklearn.naive_bayes import GaussianNB # import Naive Bayes classifier with Gaussian Kernal.
import numpy as np # import numpy for performing various mathematical functions
#Define dataset
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]) # Sample Vector
Y = np.array([1, 1, 1, 2, 2, 2]) # labels
#Define the classification Model
model = GaussianNB()
# fit the dataset into the model
# Reader is also advise to check (https://scikit-learn.org/0.15/modules/scaling_strategies.html), if you are using large dataset
model.fit(X, Y)
# predict the output of a random sample [-0.8, -1]
print(model.predict([[-0.8, -1]]))
[1]
K Nearest Neighbors
In [4]:
from sklearn.neighbors import KNeighborsClassifier
import numpy as np # import numpy for performing various mathematical functions
#Define dataset
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
Y = np.array([1, 1, 1, 2, 2, 2])
#Define the classification Model
model = KNeighborsClassifier(n_neighbors=3)
# fit the dataset into the model
model.fit(X, Y)
# predict the output of a random sample [-0.8, -1]
print(model.predict([[-0.8, -1]]))
[1]
Decision Tree
In [5]:
from sklearn.tree import DecisionTreeClassifier
import numpy as np # import numpy for performing various mathematical functions
#Define dataset
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
Y = np.array([1, 1, 1, 2, 2, 2])
#Define the classification Model
model = KNeighborsClassifier(n_neighbors=3)
# fit the dataset into the model
model.fit(X, Y)
# predict the output of a random sample [-0.8, -1]
print(model.predict([[-0.8, -1]]))
[1]
Support Vector Machine
In [6]:
from sklearn import svm
import numpy as np # import numpy for performing various mathematical functions
#Define dataset
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
Y = np.array([1, 1, 1, 2, 2, 2])
#Define the classification Model
model = svm.SVC()
# fit the dataset into the model
model.fit(X, Y)
# predict the output of a random sample [-0.8, -1]
print(model.predict([[-0.8, -1]]))
[1]
In [ ]:
from sklearn import tree
model = tree.DecisionTreeClassifier()