def identify_marker_genes_rf(self, labels=None, clusters=None,<EOL>n_genes=<NUM_LIT>):
if(labels is None):<EOL><INDENT>try:<EOL><INDENT>keys = np.array(list(self.adata.obs_keys()))<EOL>lbls = self.adata.obs[ut.search_string(<EOL>keys, '<STR_LIT>')[<NUM_LIT:0>][<NUM_LIT:0>]].get_values()<EOL><DEDENT>except KeyError:<EOL><INDENT>print("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>return<EOL><DEDENT><DEDENT>elif isinstance(labels, str):<EOL><INDENT>lbls = self.adata.obs[labels].get_values().flatten()<EOL><DEDENT>else:<EOL><INDENT>lbls = labels<EOL><DEDENT>from sklearn.ensemble import RandomForestClassifier<EOL>markers = {}<EOL>if clusters == None:<EOL><INDENT>lblsu = np.unique(lbls)<EOL><DEDENT>else:<EOL><INDENT>lblsu = np.unique(clusters)<EOL><DEDENT>indices = np.argsort(-self.adata.var['<STR_LIT>'].values)<EOL>X = self.adata.layers['<STR_LIT>'][:, indices[:n_genes]].toarray()<EOL>for K in range(lblsu.size):<EOL><INDENT>print(K)<EOL>y = np.zeros(lbls.size)<EOL>y[lbls == lblsu[K]] = <NUM_LIT:1><EOL>clf = RandomForestClassifier(n_estimators=<NUM_LIT:100>, max_depth=None,<EOL>random_state=<NUM_LIT:0>)<EOL>clf.fit(X, y)<EOL>idx = np.argsort(-clf.feature_importances_)<EOL>markers[lblsu[K]] = self.adata.uns['<STR_LIT>'][idx]<EOL><DEDENT>if clusters is None:<EOL><INDENT>self.adata.uns['<STR_LIT>'] = markers<EOL><DEDENT>return markers<EOL>
Ranks marker genes for each cluster using a random forest
classification approach.

Parameters
----------

labels - numpy.array or str, optional, default None
    Cluster labels to use for marker gene identification. If None,
    assumes that one of SAM's clustering algorithms has been run. Can
    be a string (i.e. 'louvain_clusters', 'kmeans_clusters', etc) to
    specify specific cluster labels in adata.obs.

clusters - int or array-like, default None
    A number or vector corresponding to the specific cluster ID(s)
    for which marker genes will be calculated. If None, marker genes
    will be computed for all clusters.

n_genes - int, optional, default 4000
    By default, trains the classifier on the top 4000 SAM-weighted
    genes.