Skip to content

numpy version #4

@biopzhang

Description

@biopzhang

I found the code useful. I rewrote the function using 2D numpy array as input in my use case. I'm posting it here if it's of any help to others or as an addition to your code.

import numpy as np
import pandas as pd

def qnorm_np(arr):
    sorted_arr = arr.astype(float)
    sorted_arr.sort(axis=0)
    rank = sorted_arr.mean(axis=1)
    for col in range(arr.shape[1]):
        t = np.searchsorted(np.sort(arr[:, col]), arr[:,col])
        sorted_arr[:,col] = rank[t]
    return sorted_arr

#Test
df_input =  pd.DataFrame({'C1': {'A': 5, 'B': 2, 'C': 3, 'D': 4},
                   'C2': {'A': 4, 'B': 1, 'C': 4, 'D': 2},
                   'C3': {'A': 3, 'B': 4, 'C': 6, 'D': 8}})
arr_norm = qnorm_np(df_input.values)
  • P

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions