def vq(obs, code_book): """ Assign codes from a code book to observations. Assigns a code from a code book to each observation. Each observation vector in the 'M' by 'N' `obs` array is compared with the centroids in the code book and assigned the code of the closest centroid. The features in `obs` should have unit variance, which can be acheived by passing them through the whiten function. The code book can be created with the k-means algorithm or a different encoding algorithm. Parameters ---------- obs : ndarray Each row of the 'N' x 'M' array is an observation. The columns are the "features" seen during each observation. The features must be whitened first using the whiten function or something equivalent. code_book : ndarray The code book is usually generated using the k-means algorithm. Each row of the array holds a different code, and the columns are the features of the code. >>> # f0 f1 f2 f3 >>> code_book = [ ... [ 1., 2., 3., 4.], #c0 ... [ 1., 2., 3., 4.], #c1 ... [ 1., 2., 3., 4.]]) #c2 Returns ------- code : ndarray A length N array holding the code book index for each observation. dist : ndarray The distortion (distance) between the observation and its nearest code. Notes ----- This currently forces 32-bit math precision for speed. Anyone know of a situation where this undermines the accuracy of the algorithm? Examples -------- >>> from numpy import array >>> from scipy.cluster.vq import vq >>> code_book = array([[1.,1.,1.], ... [2.,2.,2.]]) >>> features = array([[ 1.9,2.3,1.7], ... [ 1.5,2.5,2.2], ... [ 0.8,0.6,1.7]]) >>> vq(features,code_book) (array([1, 1, 0],'i'), array([ 0.43588989, 0.73484692, 0.83066239])) """ try: import _vq ct = common_type(obs, code_book) c_obs = obs.astype(ct) c_code_book = code_book.astype(ct) if ct is single: results = _vq.vq(c_obs, c_code_book) elif ct is double: results = _vq.vq(c_obs, c_code_book) else: results = py_vq(obs, code_book) except ImportError: results = py_vq(obs, code_book) return results
def vq(obs, code_book): """ Vector Quantization: assign codes from a code book to observations. Assigns a code from a code book to each observation. Each observation vector in the M by N obs array is compared with the centroids in the code book and assigned the code of the closest centroid. The features in obs should have unit variance, which can be acheived by passing them through the whiten function. The code book can be created with the k-means algorithm or a different encoding algorithm. Parameters ---------- obs : ndarray Each row of the NxM array is an observation. The columns are the "features" seen during each observation. The features must be whitened first using the whiten function or something equivalent. code_book : ndarray The code book is usually generated using the k-means algorithm. Each row of the array holds a different code, and the columns are the features of the code. :: # f0 f1 f2 f3 code_book = [[ 1., 2., 3., 4.], #c0 [ 1., 2., 3., 4.], #c1 [ 1., 2., 3., 4.]]) #c2 Returns ------- code : ndarray A length N array holding the code book index for each observation. dist : ndarray The distortion (distance) between the observation and its nearest code. Notes ----- This currently forces 32-bit math precision for speed. Anyone know of a situation where this undermines the accuracy of the algorithm? Examples -------- >>> from numpy import array >>> from scipy.cluster.vq import vq >>> code_book = array([[1.,1.,1.], ... [2.,2.,2.]]) >>> features = array([[ 1.9,2.3,1.7], ... [ 1.5,2.5,2.2], ... [ 0.8,0.6,1.7]]) >>> vq(features,code_book) (array([1, 1, 0],'i'), array([ 0.43588989, 0.73484692, 0.83066239])) """ try: import _vq ct = common_type(obs, code_book) c_obs = obs.astype(ct) c_code_book = code_book.astype(ct) if ct is single: results = _vq.vq(c_obs, c_code_book) elif ct is double: results = _vq.vq(c_obs, c_code_book) else: results = py_vq(obs, code_book) except ImportError: results = py_vq(obs, code_book) return results
def vq(obs, code_book): """ Vector Quantization: assign features sets to codes in a code book. Vector quantization determines which code in the code book best represents an observation of a target. The features of each observation are compared to each code in the book, and assigned the one closest to it. The observations are contained in the obs array. These features should be "whitened," or nomalized by the standard deviation of all the features before being quantized. The code book can be created using the kmeans algorithm or something similar. :Parameters: obs : ndarray Each row of the array is an observation. The columns are the "features" seen during each observation The features must be whitened first using the whiten function or something equivalent. code_book : ndarray. The code book is usually generated using the kmeans algorithm. Each row of the array holds a different code, and the columns are the features of the code. :: # f0 f1 f2 f3 code_book = [[ 1., 2., 3., 4.], #c0 [ 1., 2., 3., 4.], #c1 [ 1., 2., 3., 4.]]) #c2 :Returns: code : ndarray If obs is a NxM array, then a length N array is returned that holds the selected code book index for each observation. dist : ndarray The distortion (distance) between the observation and its nearest code Notes ----- This currently forces 32 bit math precision for speed. Anyone know of a situation where this undermines the accuracy of the algorithm? Examples -------- >>> from numpy import array >>> from scipy.cluster.vq import vq >>> code_book = array([[1.,1.,1.], ... [2.,2.,2.]]) >>> features = array([[ 1.9,2.3,1.7], ... [ 1.5,2.5,2.2], ... [ 0.8,0.6,1.7]]) >>> vq(features,code_book) (array([1, 1, 0],'i'), array([ 0.43588989, 0.73484692, 0.83066239])) """ try: import _vq ct = common_type(obs, code_book) c_obs = obs.astype(ct) c_code_book = code_book.astype(ct) if ct is single: results = _vq.vq(c_obs, c_code_book) elif ct is double: results = _vq.vq(c_obs, c_code_book) else: results = py_vq(obs, code_book) except ImportError: results = py_vq(obs, code_book) return results