0

Basic question but i am unfamiliar with Python, how can I count how many of each class are in an .npy image if there are 2 channels?

Each label is stored in a 1000x1000x2 array, where I know the "1st channel" is the instance map (integer for each instance) and the "2nd channel" is the class map (represents the category). But what exactly is a channel of an array?

There are seven classes so I want an output like

class count
1:    20
2:    45
3:    6
4:    67
5:    35
6:    135
7:    89

My code is as follows

    np.array(np.unique(a[:,:,1], return_counts=True)).T 

out: array([[0.00000e+00, 8.14725e+05],
       [1.00000e+00, 1.32500e+03],
       [2.00000e+00, 1.75300e+04],
       [4.00000e+00, 9.71580e+04],
       [5.00000e+00, 6.92620e+04]])

The problem with this is I know that my whole dataset of all my images has 25,000 data samples. In this one image the count far exceeds that! So I am wondering if I have interpreted this wrong and made a wrong code?

CCZ23
  • 151
  • 12
  • Regarding “how many of each class”, you can use the `.value_counts()` function of a pandas Series. For example: `pd.Series(a[:,1]).value_counts()` - which will convert the second column of the `ndarray` to a Series, and return a Series containing the values as the index, and their counts as data. – S3DEV Nov 21 '20 at 19:49
  • What is type of the array? You can check it using `a.dtype` – fdermishin Nov 21 '20 at 19:49
  • @user13044086 float64 – CCZ23 Nov 22 '20 at 04:25
  • @S3DEV Thanks i had to change it to `pd.Series(a[:,1,1]).value_counts()` though. What is the 1 representing? i.e. what does it show me when I type `pd.Series(a[:,2,1]).value_counts()` or `pd.Series(a[:,3,1]).value_counts()` ? As these all differ. – CCZ23 Nov 22 '20 at 05:30
  • If array `a` has 1000 rows, 1000 columns and 2 channels, then `a[:, 3, 1]` represents second channel of fourth column, as indexing in Python starts with zero. To get 1000x1000 array of the first channel you can write `a[:, :, 0]`. See https://numpy.org/doc/stable/reference/arrays.indexing.html for more examples – fdermishin Nov 22 '20 at 07:52
  • Does this answer your question? [numpy: most efficient frequency counts for unique values in an array](https://stackoverflow.com/questions/10741346/numpy-most-efficient-frequency-counts-for-unique-values-in-an-array) – fdermishin Nov 22 '20 at 07:54
  • @user13044086 i don't think so because the type is float64, these examples are for int it seems. So if I have 7 classes, how can I exactly find out the counts of each of these? If I want the class map then as you say I can do `a[:,:,1]`. But how can I use this array? – CCZ23 Nov 22 '20 at 11:45

0 Answers0