Skip to content

Instantly share code, notes, and snippets.

@alfredplpl
Last active December 25, 2015 02:19
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 alfredplpl/6901609 to your computer and use it in GitHub Desktop.
Save alfredplpl/6901609 to your computer and use it in GitHub Desktop.
This is a class of Bag-of-Features by GMM
#see also: http://scikit-learn.org/stable/modules/generated/sklearn.mixture.GMM.html
import numpy as np
from sklearn import mixture,preprocessing
class BagOfFeaturesGMM:
"""This is a class of Bag-of-Features by GMM """
codebookSize=0
classifier=None
def __init__(self, codebookSize):
self.codebookSize=codebookSize
def train(self,features,iterMax=100):
# construct a GMM classifier
gmm = mixture.GMM(n_components=self.codebookSize,n_iter=iterMax)
# train the classifier
self.classifier = gmm.fit(features)
def makeHistograms(self, feature):
histogram=np.zeros(self.codebookSize)
if self.classifier==None :
raise Exception("You need train this instance.")
results=self.classifier.predict(feature)
for idx in results:
idx=int(idx)
histogram[idx]=histogram[idx]+1
histogram=preprocessing.normalize([histogram], norm='l2')[0]
return histogram
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment