来自此资源的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处理代码的一部分是它的原始形式:
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发布于 2021-11-26 15:10:19
你应该用PCA评分。
至于不运行pca,您只需注释一下这一行:
X = pca(X, initial_dims).real我所做的是添加一个参数do_pca并编辑如下函数:
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..]使用示例数据集,而不注释该行:
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)如果我们运行默认值:
res = tsne(X=X,initial_dims=20,do_pca=True)
plt.scatter(res[:,0],res[:,1],c=y)

如果我们通过pca:
pc = pca(X)[:,:20]
res = tsne(X=pc,initial_dims=20,do_pca=False)
plt.scatter(res[:,0],res[:,1],c=y)

https://stackoverflow.com/questions/66683129
复制相似问题