首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >scRNA-seq:如何使用预先计算的PCA分数/负载来使用TSNE python实现?

scRNA-seq:如何使用预先计算的PCA分数/负载来使用TSNE python实现?
EN

Stack Overflow用户
提问于 2021-03-18 00:24:41
回答 1查看 76关注 0票数 1

来自此资源的Python sne实现:https://lvdmaaten.github.io/tsne/

顺便说一句,我是个初学者。

我要做的是:使用scRNA-seq数据集并在其上运行scRNA,但使用以前计算过的PCAs (我有PCA.score和PCA.load文件)。

Q1:我应该能够在tSNE中使用所选的计算出的PCA,但是在运行Y= tsne.tsne(X)?时,使用pca.score还是pca.load呢?

Q2:我尝试删除/替换部分PCA计算代码,以尝试删除PCA预处理,但似乎总是会出现错误。为了正确地使用我已经使用的PCA数据,并且不再从它中计算PCA,我应该更改什么?

PCA处理代码的一部分是它的原始形式:

代码语言:javascript
复制
def pca(X=np.array([]), no_dims=50):
    """
        Runs PCA on the NxD array X in order to reduce its dimensionality to
        no_dims dimensions.
    """

    print("Preprocessing the data using PCA...")
    (n, d) = X.shape
    X = X - np.tile(np.mean(X, 0), (n, 1))
    (l, M) = X  #np.linalg.eig(np.dot(X.T, X))
    Y = np.dot(X, M[:, 0:no_dims])

    return Y
EN

回答 1

Stack Overflow用户

发布于 2021-11-26 15:10:19

你应该用PCA评分。

至于不运行pca,您只需注释一下这一行:

代码语言:javascript
复制
X = pca(X, initial_dims).real

我所做的是添加一个参数do_pca并编辑如下函数:

代码语言:javascript
复制
def tsne(X=np.array([]), no_dims=2, initial_dims=50, perplexity=30.0,do_pca=True):
    """
        Runs t-SNE on the dataset in the NxD array X to reduce its
        dimensionality to no_dims dimensions. The syntaxis of the function is
        `Y = tsne.tsne(X, no_dims, perplexity), where X is an NxD NumPy array.
    """

    # Check inputs
    if isinstance(no_dims, float):
        print("Error: array X should have type float.")
        return -1
    if round(no_dims) != no_dims:
        print("Error: number of dimensions should be an integer.")
        return -1

    # Initialize variables
    if do_pca:
        X = pca(X, initial_dims).real
    (n, d) = X.shape
    max_iter = 50
    [.. rest stays the same..]

使用示例数据集,而不注释该行:

代码语言:javascript
复制
import numpy as np
from sklearn.manifold import TSNE
from sklearn.datasets import load_digits
import matplotlib.pyplot as plt
import sys
import os
from tsne import *

X,y = load_digits(return_X_y=True,n_class=3)

如果我们运行默认值:

代码语言:javascript
复制
res = tsne(X=X,initial_dims=20,do_pca=True)
plt.scatter(res[:,0],res[:,1],c=y)

如果我们通过pca:

代码语言:javascript
复制
pc = pca(X)[:,:20]
res = tsne(X=pc,initial_dims=20,do_pca=False)
plt.scatter(res[:,0],res[:,1],c=y)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66683129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档