#!/usr/bin/python import numpy as np import sys import matplotlib.pyplot as plt ## height, weight, shoe size X = np.array([ [6, 180, 12], [5.92, 190, 11], [5.58, 170, 12], [5.92, 165, 10], [5, 100, 6], [5.5, 150, 8], [5.42, 130, 7], [5.75, 150, 9]]) ## 1 = male, 2 = female Y = np.array([1, 1, 1, 1, 2, 2, 2, 2]) from sklearn.naive_bayes import GaussianNB clf = GaussianNB() clf.fit(X, Y) print(clf.predict([[6, 130, 8]])) ## the posterior probabilities: [[posterior (male), posterior (female)]] print (clf.predict_proba([[6, 130, 8]])) print(clf.predict([[5, 180, 11]])) print(clf.predict_proba([[5, 180, 11]])) print(clf.predict([[5.3, 180, 11]])) print(clf.predict_proba([[5.3, 180, 11]])) ## Incremental fit on a batch of samples. ## This method is expected to be called several times consecutively on ## different chunks of a dataset so as to implement out-of-core or ## online learning. #clf_pf = GaussianNB() #clf_pf.partial_fit(X, Y, np.unique(Y)) #print(clf_pf.predict([[-0.8, -1]])) XX = [n[0] for n in X] YY = [n[1] for n in X] ZZ = [n[2] for n in X] #plt.plot(X, color="blue") plt.scatter(XX, YY) plt.ylabel('some points') plt.savefig("gender.pdf") # plt.show() colors = ["g", "b", "r"] for ii, pp in enumerate(X): plt.scatter(X[ii][0], X[ii][1], color = colors[Y[ii]]) plt.savefig("gender2.pdf") for x in dir(clf): print (x, getattr(clf, x)) ''' note theta_ is the mean sigma_ is the variance - but which variance? ''' ######################################################### from matplotlib import pyplot import pylab from mpl_toolkits.mplot3d import Axes3D import random fig = pylab.figure() ax = Axes3D(fig) ''' sequence_containing_x_vals = range(0,100) sequence_containing_y_vals = range(0,100) sequence_containing_z_vals = range(0,100) random.shuffle(sequence_containing_x_vals) random.shuffle(sequence_containing_y_vals) random.shuffle(sequence_containing_z_vals) ''' ## ax.scatter(sequence_containing_x_vals, sequence_containing_y_vals, sequence_containing_z_vals) ax.scatter(XX, YY, ZZ) plt.savefig("gender3.pdf") fig = pylab.figure() ax = Axes3D(fig) colors = ["g", "b", "r"] for ii, pp in enumerate(XX): ax.scatter(XX[ii], YY[ii], ZZ[ii], color = colors[Y[ii]]) plt.savefig("gender4.pdf") # pyplot.show()