This is most basic classifier, It uses concept of probability in order to predict the class of input feature data . Read More
We gonna see how to use Naive Bayes Classifier very easily using python.
We are going to use sklearn python package, we use inbuilt function in sklearn for naive bayes classifier. Fist train the our classifier then predict class for your input, then calculate accuracy of result.
Fallow code comments for better understanding.
Output :
We gonna see how to use Naive Bayes Classifier very easily using python.
Bayes rule |
Prerequisite:
- Python 2
- sklearn [Python Module]
- Numpy [Python Module]
We are going to apply this algorithm on Iris dataset .
Iris dataset : There are total 150 training set in this dataset. and each training set consist of 4 feature and corresponding class ( There 3 class in Iris dataset. represented by 0 for setosa ,1 for versicolor ,2 for virginica ) .
These 4 feature used to classify data in 3 classes of flowers .Read More
We are going to use sklearn python package, we use inbuilt function in sklearn for naive bayes classifier. Fist train the our classifier then predict class for your input, then calculate accuracy of result.
Fallow code comments for better understanding.
Naive Bayes Classifier [ Python ] [Github Link]
- __auther__='www.codecops.in'
- # Classify Iris data Using Naive Bayes
- #An Example Of Supervised Learning
- from sklearn.datasets import load_iris
- from sklearn.naive_bayes import GaussianNB
- import numpy as np
- #{{ Getting Iris Dataset
- data=load_iris()
- X=data.data #input Features
- Y=data.target #Class of corresponding Input
- #}}
- if __name__=='__main__':
- # clsf == classifire :)
- clsf=GaussianNB()
- #{{training our classifire using Naive_bayes
- #!for IRis data Classification
- clsf.fit(X,Y)
- #}}
- # Test fot All data
- print ''' _______Input_______\
- __Real Class__\
- Prediction class '''
- pred=[]
- for i in range(len(X)):
- pred.append(clsf.predict(X[i])) #storing Pred result
- print X[i],'\t\t\t',Y[i],'\t\t\t',pred[-1]
- from sklearn.metrics import accuracy_score
- print "Accuracy Test Using accuracy_score function"
- pred=np.array(pred)
- print accuracy_score(pred,Y)*100,'% Wow _/\_ Not Bad'
Output :
Naive Bayes classifire accuracy on Iris data |
No comments:
Post a Comment
THANKS FOR UR GREAT COMMENT