-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathIrisDatasetNaiveBayes.py
31 lines (25 loc) · 1018 Bytes
/
IrisDatasetNaiveBayes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#import matplotlib.pyplot as plt
import pandas as pd
from sklearn.naive_bayes import GaussianNB
from sklearn.datasets.base import load_iris
from sklearn.metrics import accuracy_score
#load iris dataset
iris=load_iris()
#get data, column names, labels, etc. as list
data=iris["data"]
feature_names=iris["feature_names"]
labels=iris["target"]
#create dataframe with data and insert the labels
df=pd.DataFrame(data, columns=feature_names)
output=pd.DataFrame(labels, columns=['target'])
model=GaussianNB()
#train model
#if getting this error, it is because a matrix with 1 column
#is being passed in when a 1d array is expected. ravel() will work.
#DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). if name == 'main':
model.fit(df, output.values.ravel())
model.score(df, output.values.ravel())
#predict output
predicted=model.predict(df)
print predicted
print "Accuracy: ", accuracy_score(output, predicted)