- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Which function of scipy.cluster.vq module is used to assign codes from a code book to observations?

Before implementing k-means algorithms, the **scipy.cluster.vq.vq(obs, code_book, check_finite = True)** used to assign codes to each observation from a code book. It first compares each observation vector in the ‘M’ by ‘N’ obs array with the centroids in the code book. Once compared, it assigns the code to the closest centroid. It requires unit variance features in the obs array, which we can achieve by passing them through the **scipy.cluster.vq.whiten(obs, check_finite = True)**function.

## Parameters

Below are given the parameters of the function **scipy.cluster.vq.vq(obs, code_book, check_finite = True)** −

**obs− ndarray**

It is an ‘M’ by ‘N’ array where each row is an observation, and the columns are the features seen during each observation. The example is given below −

obs = [[ 1., 1., 1.], [ 2., 2., 2.], [ 3., 3., 3.], [ 4., 4., 4.]]

**code_book**− ndarray

It is also an ‘M’ by ‘N’ array, usually generated by using k-means algorithm, where each row holds a different code, and the columns are the features of that code.

The example is given below −

code_book = [ [ 1., 2., 3., 4.], [ 1., 2., 3., 4.], [ 1., 2., 3., 4.]]

**check_finite**− bool,optional

This parameter is used to check whether the input matrices contain only finite numbers. Disabling this parameter may give you a performance gain but it may also result in some problems like crashes or non-termination if the observations do contain infinites. The default value of this parameter is **True**.

## Returns

**code**− ndarray

It returns a ‘M’ array which holds the code book index for each observation.

**dist**− ndarray

It also returns the distance, which is also called distortion, between each observation and its nearest code.

## Example

import numpy as np from scipy.cluster.vq import vq code_book = np.array([[1.,1.,1.], [2.,2.,2.]]) observations = np.array([[2.9, 1.3, 1.9], [1.7, 3.2, 1.1], [1.0, 0.2, 1.7,]]) vq(observations, code_book)

## Output

(array([1, 1, 0]), array([1.14455231, 1.52970585, 1.06301458]))

- Related Questions & Answers
- Which function of scipy.cluster.vq module is used to normalize observations on each feature dimension?
- Which linear function of SciPy is used to solve a banded matrix equation?
- Which linear function of SciPy is used to solve triangular matrix equations?
- Which SciPy package is used to implement Clustering?
- Which linear function of SciPy is used to solve the circulant matrix equation?
- Which linear function of SciPy is used to solve Toeplitz matrix using Levinson Recursion?
- Which linear function of SciPy is used to solve Hermitian positive-definite banded matrix equation?
- Which PHP function is used to disconnect from MySQL database connection?
- Which PHP function is used to create a new database?
- Which PHP function is used to select a MySQL database?
- Which PHP function is used to create a MySQL table?
- Which MySQL function is used to find first non-NULL value from a list of values?
- What is scipy cluster hierarchy? How to cut hierarchical clustering into flat clustering?
- Which function in MySQL is used to reverse a particular string?
- Which PHP function is used to delete an existing database?