Package: topn
topnCython utility functions to be used instead of pandas'SeriesGroupBynlargest()function (sincepandas does it so slowly).Contains 3 functions:awesome_topn(),awesome_hstack_topn(),awesome_hstack(): (for CSR matrices only; at least twice as fast asscipy.sparse.hstackin scipy version 1.6.1)SeeShort Descriptionfor details.This is how it may be done with pandas:importpandasaspdimportnumpyasnpr=np.array([0,1,2,1,2,3,2])c=np.array([1,1,0,3,1,2,3])d=np.array([0.3,0.2,0.1,1.0,0.9,0.4,0.6])rcd=pd.DataFrame({'r':r,'c':c,'d':d})rcdrcd0010.31110.22200.13131.04210.95320.46230.6ntop=2rcd.set_index('c').groupby('r')['d'].nlargest(ntop).reset_index().sort_values(['r','d'],ascending=[True,False])rcd0010.31131.02110.23210.94230.65320.4Usagefromtopnimportawesome_topno_r,o_c,o_d=awesome_topn(r,c,d,ntop,n_jobs=7)pd.DataFrame({'r':o_r,'c':o_c,'d':o_d})rcd0010.31131.02110.23210.94230.65320.4Alternatively, if one had a matrix encoding the above data:fromscipy.sparseimportcsr_matrixcsr=csr_matrix((d,(r,c)),shape=(4,4))then one could use the functionawesome_hstack_topn()instead:fromtopnimportawesome_hstack_topntopn_matrix=awesome_hstack_topn([csr],ntop=ntop)o_r,o_c=topn_matrix.nonzero()o_d=topn_matrix.datapd.DataFrame({'r':o_r,'c':o_c,'d':o_d})rcd0010.31131.02110.23210.94230.65320.4Short DescriptionContains 3 functions:awesome_topn(),awesome_hstack_topn(),awesome_hstack()defawesome_topn(r,c,d,ntop,n_rows=-1,n_jobs=1):"""r, c, and d are 1D numpy arrays all of the same length N.This function will return arrays rn, cn, and dn of length n <= N suchthat the set of triples {(rn[i], cn[i], dn[i]) : 0 < i < n} is a subset of{(r[j], c[j], d[j]) : 0 < j < N} and that for every distinct valuex = rn[i], dn[i] is among the first ntop existing largest d[j]'s whoser[j] = x.Input:r and c: two 1D integer arrays of the same lengthd: 1D array of single or double precision floating point type of thesame length as r or cntop maximum number of maximum d's returnedn_rows: an int. If > -1 it will replace output rn with Rn theindex pointer array for the compressed sparse row (CSR) matrixwhose elements are {C[rn[i], cn[i]] = dn: 0 < i < n}.  This matrixwill have its number of rows = n_rows.  Thus the length of Rn isn_rows + 1n_jobs: number of threads, must be >= 1Output:(rn, cn, dn) where rn, cn, dn are all arrays as described above, or(Rn, cn, dn) where Rn is described above"""defawesome_hstack_topn(blocks,ntop,sort=True,use_threads=False,n_jobs=1):"""Returns, in CSR format, the matrix formed by horizontally stacking thesequence of CSR matrices in parameter 'blocks', with only the largest ntopelements of each row returned.  Also, each row will be sorted indescending order only whenntop < total number of columns in blocks or sort=True,otherwise the rows will be unsorted.:param blocks: list of CSR matrices to be stacked horizontally.:param ntop: int. The maximum number of elements to be returned foreach row.:param sort: bool. Each row of the returned matrix will be sorted indescending order only when ntop < total number of columns in blocksor sort=True, otherwise the rows will be unsorted.:param use_threads: bool. Will use the multi-threaded versions of thisroutine if True otherwise the single threaded version will be used.In multi-core systems setting this to True can lead to acceleration.:param n_jobs: int. When use_threads=True, denotes the number of threadsthat are to be spawned by the multi-threaded routines. Recommendedvalue is number of cores minus one.Output:(scipy.sparse.csr_matrix) matrix in CSR format"""defawesome_hstack(blocks,use_threads=False,n_jobs=1):"""Returns, in CSR format, the matrix formed by horizontally stacking thesequence of CSR matrices in parameter blocks.:param blocks: list of CSR matrices to be stacked horizontally.:param use_threads: bool. Will use the multi-threaded versions of thisroutine if True otherwise the single threaded version will be used.In multi-core systems setting this to True can lead to acceleration.:param n_jobs: int. When use_threads=True, denotes the number of threadsthat are to be spawned by the multi-threaded routines. Recommendedvalue is number of cores minus one.Output:(scipy.sparse.csr_matrix) matrix in CSR format"""