adata.obsm值改变问题

测试1(adata.obsm[“X_denoised”]改变)

import scanpy as sc 
import numpy as np
adata0=sc.read("./adata0.h5ad")
print(adata0)
print(adata0.obsm["X_denoised"])
print(np.where(adata0.obsm["X_denoised"]<0))
print("=====================================")
temporary = sc.AnnData(adata0.obsm["X_denoised"])
sc.pp.scale(temporary)
print(adata0.obsm["X_denoised"])
print(np.where(adata0.obsm["X_denoised"]<0)) # adata0.obsm["X_denoised"]被改变了

adata.obsm值改变问题_第1张图片可以看到这个adata.obsm["X_denoised]是发生变化了,这个很奇怪的,是不是adata.obsm[“X_denoised”]是某些对象的引用,目前还没有搞清楚

测试2(不改变)

adata0=sc.AnnData(np.random.rand(3,3))
adata0.obsm["X_denoised"]=np.random.rand(3,3)
print(adata0)
print(adata0.obsm["X_denoised"])
print(np.where(adata0.obsm["X_denoised"]<0))
print("=====================================")
temporary = sc.AnnData(adata0.obsm["X_denoised"])
sc.pp.scale(temporary)
print(adata0.obsm["X_denoised"])
print(np.where(adata0.obsm["X_denoised"]<0))
## 这个adata0.obsm["X_denoised"]又没有改变

测试3(不改变-解决办法)

adata0=sc.AnnData(np.random.rand(3,3))
adata0.obsm["X_denoised"]=np.random.rand(3,3)
print(adata0)
print(adata0.obsm["X_denoised"])
print(np.where(adata0.obsm["X_denoised"]<0))
print("=====================================")
temporary = sc.AnnData(adata0.obsm["X_denoised"].copy()) # 加copy这里肯定没有问题
sc.pp.scale(temporary)
print(adata0.obsm["X_denoised"])
print(np.where(adata0.obsm["X_denoised"]<0))
## 这个adata0.obsm["X_denoised"]又没有改变

adata.obsm值改变问题_第2张图片

你可能感兴趣的:(python,杂类,numpy,python,机器学习)