1

Recently I read a line of code that confused me:

pointsInCurrCluster = dataSet[nonzero(clusterAssment[:, 0].A == i)[0], :]  

The author did not define the function for A, so I assume that .A is some kind of built-in function. Does anyone know what it is?

3
  • And what is clusterAssment[:, 0]? It can be user defined class/container returning some objects that have A as a parameter. Commented Nov 30, 2014 at 23:27
  • clusterAssment[:, 0]is a 2-d array. I looked through his coding, i can not find any related class or container returning some objects that have A as parameter.That is why I assume it is some kind of built in function. Commented Nov 30, 2014 at 23:52
  • For a clearer example: stackoverflow.com/q/57867223/562769 Commented Sep 10, 2019 at 9:06

4 Answers 4

1

Ok, so in this way I think its is proparty of scipy matrix returning nparray:

In [115]: mtx = sp.matrix([1,2,3])    
In [116]: mtx.A
Out[116]: array([[1, 2, 3]])

A is getter/shortcut for getA.

Sign up to request clarification or add additional context in comments.

4 Comments

so what is A exactly?Thanks
@user12551 A property is a function that acts like a it's just a regular attribute. It's like a getter or setter method in Java with a little bit less boilerplate.
@user12551 I amended the answer.
To be picky - np.matrix is a function that creates a numpy subclass, Matrix. To create a sparse matrix you use sparse.csr_matrix() (or one of the other sparse formats). But both kinds of matrix have this .A property.
1

In https://github.com/skodali1/python-machine-learning/blob/master/kmeansclusteringalgo.py (found by google search for 'python clusterAssment'

from numpy import *
clusterAssment = matrix(zeros((m,2)))
...
ptsInClust = dataSet[nonzero(clusterAssment[:,0].A==cent)[0]]

In this case clusterAssment is a numpy.matrix object. This is like a numpy.ndarray, except it is always 2d, and has MATLAB like matrix operators.

clusterAssment.A

just turns the matrix into a regular numpy.array, probably so it can be passed to numpy.nonzero.

scipy.sparse implements sparse matrices, which also have this .A property. But based on this code, I don't think that applies here.

Comments

0

.A means 'change the data type from a matrix to an array'

For example:
enter image description here

Comments

0

So, in python you can get result of a conditional check on each element of an array by writing a statement like: arr > 3. What it does is, for an array arr = [[1,2,3],[3,4,5]] the output you will have is [[False,False,False],[False,True,True]]. Now having said that you need an array to do this. That is what a .A does in python, it gives you an array representation of the matrix. Now, clusterAssment[:, 0].A == i, gives you conditional check answer for every row and first column against the value i. nonzero(clusterAssment[:, 0].A == i) converts the conditional check to index of rows and columns which Satisfy the condition. More details here: nonZero . Now, since clusterAssment is a 2-D array the nonzero(~)[0] gives the rows which have the value i as 1st element, and dataSet[nonzero(clusterAssment[:, 0].A == i)[0], :] gives all those respective tuples from dataset.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.