Created
October 10, 2012 00:44
-
-
Save masayang/3862463 to your computer and use it in GitHub Desktop.
Naive Bayes (Iris)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data | |
上記にあるデータをiris.dataとして保存 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from sklearn.naive_bayes import GaussianNB | |
def iris_index(s): | |
names = {'Iris-setosa':1.0, | |
'Iris-versicolor':2.0, | |
'Iris-virginica':3.0} | |
if names.has_key(s): return names[s] | |
else: return -1.0 | |
iris = np.genfromtxt("iris.data", | |
delimiter = ",", | |
dtype = float, | |
converters = {4: iris_index}) | |
x = iris[:, 0:4] | |
y = iris[:, 4] | |
gnb = GaussianNB() | |
y_pred = gnb.fit(x, y).predict(x) | |
print "Number of mislabeled points: %d" % (y != y_pred).sum() | |
print "mean accuracy: %f" % gnb.fit(x, y).score(x, y) | |
print y | |
print y_pred |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment