counts of unique values in rows of given 2d array numpy

Solutions on MaxInterview for counts of unique values in rows of given 2d array numpy by the best coders in the world

showing results for - "counts of unique values in rows of given 2d array numpy"
Carolina
25 Sep 2020
1mat5 = np.array([['a', 'g', 'c'], 
2         		 ['a', 'b', 'c'], 
3         		 ['f', 'c', 'c']])
4val, cnts = np.unique(mat5, return_counts=True)
5dict_unique = {k: v for k, v in zip(val, cnts)}
6""" result: {'a': 2, 'b': 1, 'c': 4, 'f': 1, 'g': 1} """
similar questions