测试sc.pp.normalize_per_cell
import numpy as np
import scanpy as sc
x=np.array([[1,2,3],[2,3,4],[1,1,3],[0,2,10]])
adata=sc.AnnData(x)
adata.raw=adata.copy()
ff=np.median(np.sum(x,axis=1))/np.sum(x,axis=1)
print(np.dot(np.diag(ff),x))
sc.pp.normalize_per_cell(adata)
print(adata.X)
结果如下 注意这个
测试sc.pp.normalize_total
import numpy as np
import scanpy as sc
x=np.array([[1,2,3],[2,3,4],[1,1,3],[0,2,10]])
adata=sc.AnnData(x)
sc.pp.normalize_total(adata)
print(adata.X)
结果如下
验证sc.pp.normalize_per_cell
x=np.random.rand(5,3)
adata=sc.AnnData(x)
ff=np.median(np.sum(x,axis=1))/np.sum(x,axis=1)
print(np.dot(np.diag(ff),x))
print("====================")
sc.pp.normalize_per_cell(adata)
print(adata.X)
记录python的操作
按列操作实现1
import numpy as np
m = np.array([[1,2,3],[4,5,6],[7,8,9],[2,3,-10]])
c = np.array([-1,1,4])
m * c
按列操作实现2
np.dot(m,np.diag(c))
按行操作实现1
import numpy as np
m = np.array([[1,2,3],[4,5,6],[7,8,9],[2,3,-10]])
c = np.array([-1,1,4,2])
m * c[:, np.newaxis]
结果如下
按行操作实现2
(m.T * c).T
按行操作实现3
m * c[:, None]
按行操作实现4
np.dot(np.diag(c),m)
结果如下
|