Skip to content

Instantly share code, notes, and snippets.

@aclissold
Created October 7, 2014 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aclissold/930d15cea188c6cddead to your computer and use it in GitHub Desktop.
Save aclissold/930d15cea188c6cddead to your computer and use it in GitHub Desktop.
Gaussian Naive Bayes
#!/usr/bin/env python
from __future__ import print_function
import numpy as np
from sklearn.naive_bayes import GaussianNB
# Problem:
# https://www.dropbox.com/s/ta4sh8cld3sh7rj/Screenshot%202014-10-07%2002.28.13.png?dl=0
#########
# Train #
#########
X = np.array([(-5.01, -8.12, -3.68), (-0.91, -0.18, -0.05), (5.35, 2.26, 8.13),
(-5.43, -3.48, -3.54), (1.3, -2.06, -3.53), (5.12, 3.22, -2.66),
(1.08, -5.52, 1.66), (-7.75, -4.54, -0.95), (-1.34, -5.31, -9.87),
(0.86, -3.78, -4.11), (-5.47, 0.5, 3.92), (4.48, 3.42, 5.19),
(-2.67, 0.63, 7.39), (6.14, 5.72, -4.85), (7.11, 2.39, 9.21),
(4.94, 3.29, 2.08), (3.6, 1.26, 4.36), (7.17, 4.33, -0.98),
(-2.51, 2.09, -2.59), (5.37, -4.63, -3.65), (5.75, 3.97, 6.65),
(-2.25, 2.13, -6.94), (7.18, 1.46, -6.66), (0.77, 0.27, 2.41),
(5.56, 2.86, -2.26), (-7.39, 1.17, 6.31), (0.91, -0.43, -8.71),
(1.03, -3.33, 4.33), (-7.49, -6.32, -0.31), (3.52, -0.36, 6.43)])
y = np.array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0,
1, 2, 0, 1, 2, 0, 1, 2])
clf = GaussianNB()
clf.fit(X, y)
###########
# Predict #
###########
classes = ['Red', 'Blue', 'Green']
Z1 = clf.predict([-1.34, -0.09, -0.12]) # ??? the assignment says -0.1.22
Z2 = clf.predict([2.21, 0.56, -0.79])
print('Object11 is', classes[Z1], 'and Object12 is', classes[Z2] + '.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment