Skip to content

Instantly share code, notes, and snippets.

@yjzhang
Created June 9, 2018 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yjzhang/2cdf1e1ec8489124d05b8a4e86a9a317 to your computer and use it in GitHub Desktop.
Save yjzhang/2cdf1e1ec8489124d05b8a4e86a9a317 to your computer and use it in GitHub Desktop.
# simple implementation of quantile normalization?
import numpy as np
def quantile_norm(data):
"""
Source: https://en.wikipedia.org/wiki/Quantile_normalization
Note: this doesn't deal with ties very well.
Args:
data (array): dense array of shape (genes, cells_or_samples)
Returns:
new array with same shape as data, but after quantile normalization
"""
rows, cols = data.shape
normed_data = np.zeros(data.shape)
orders = data.argsort(0).argsort(0)
data_sorted = np.sort(data, 0)
ranks = data_sorted.mean(1)
for c in range(cols):
normed_data[:,c] = ranks[orders[:,c]]
return normed_data
~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment