Scikit learn 在三维空间中分别绘制从sklearn.decomposition.PCA导出的PCA组件

Scikit learn 在三维空间中分别绘制从sklearn.decomposition.PCA导出的PCA组件,scikit-learn,pca,nilearn,Scikit Learn,Pca,Nilearn,在我的项目中,我使用三维MRI数据,其中第四维代表不同的对象(我使用这个软件包)。我正在使用sklearn.decomposition.PCA从数据中提取给定数量的主成分。现在我想在一张大脑图像上分别绘制这些成分,也就是说,我想用我提取的成分(在本例中为2)以不同的颜色显示一张大脑图像 下面是使用OASIS数据集的示例代码,可通过以下方式下载: 使用nilearn.input_data.NiftiMasker掩蔽,它将我的4维数据转换为2维数组(n_主题x n_体素) 使用StandardSca

在我的项目中,我使用三维MRI数据,其中第四维代表不同的对象(我使用这个软件包)。我正在使用
sklearn.decomposition.PCA
从数据中提取给定数量的主成分。现在我想在一张大脑图像上分别绘制这些成分,也就是说,我想用我提取的成分(在本例中为2)以不同的颜色显示一张大脑图像

下面是使用OASIS数据集的示例代码,可通过以下方式下载:

  • 使用nilearn.input_data.NiftiMasker掩蔽,它将我的4维数据转换为2维数组(n_主题x n_体素)
  • 使用
    StandardScaler标准化数据矩阵
  • 使用
    sklearn.decomposition.PCA运行PCA

  • 据我所知,运行PCA后获得的是PCA加载量?不幸的是,我不知道如何从这两张图像中获取数据,每个图像包含一个PCA组件。

    要将数据恢复为图像格式,您需要执行NiftiMasker.inverse_transform()。为此,需要在体素空间中保留尺寸

    因此,管道现在的工作方式是,在体素空间上使用降维。如果您想减少主题空间中的维度,您可以更改以下内容:

    pipe = Pipeline([('niftimasker',niftimasker),
                 ('scaler',scaler),
    #                  ('pca',pca)
                ])
    
    X = pipe.fit_transform(imgs)
    X_reduced = pca.fit_transform(X.T).T
    
    X = pipe.fit_transform(imgs)
    
    components = pca.components_
    #In your case 2, but replace range(2) with range(n_components)
    most_important = [np.abs(components[i]).argmax() for i in range(2)]
    
    然后,您将应用逆变换,如下所示:

    component_image = niftimasker.inverse_transform(X_reduced)
    
    然后,要获得每个单独的主题组件图像,您将使用nilearn.image中的index_图像。例如,这是第一个对象组件的图像:

    component1_image = index_img(component_image,0)
    
    component1_image = niftimasker.inverse_transform(comp1)
    component2_image = niftimasker.inverse_transform(comp2)
    
    但是,我认为您对减少体素空间上的死亡感兴趣。因此,为了保留逆变换的体素维度,需要获得PCA维度缩减中选择的每个体素特征的索引。保持管道的原始状态,并执行以下操作:

    pipe = Pipeline([('niftimasker',niftimasker),
                 ('scaler',scaler),
    #                  ('pca',pca)
                ])
    
    X = pipe.fit_transform(imgs)
    X_reduced = pca.fit_transform(X.T).T
    
    X = pipe.fit_transform(imgs)
    
    components = pca.components_
    #In your case 2, but replace range(2) with range(n_components)
    most_important = [np.abs(components[i]).argmax() for i in range(2)]
    
    然后用x个对象和y个体素平铺nan阵列:(在您的示例中为30 x 229007)

    然后对每个组件应用反向变换:

    component1_image = index_img(component_image,0)
    
    component1_image = niftimasker.inverse_transform(comp1)
    component2_image = niftimasker.inverse_transform(comp2)
    
    现在,您将有2个图像,每个图像具有30个主题和1个表示所选组件的有效体素值。如何在30个对象上聚合组件体素取决于您,在这种情况下,我将使用nilearn.image中的平均图像函数:

    mean_component1_image = mean_img(component1_image)
    mean_component2_image = mean_img(component2_image)
    
    最后,在这两种情况下,绘制各自的图像。在体素缩减版本中,您将看到X维(第二张图)中的两幅图像有一点变化,但Y和Z几乎没有变化。我使用的是nilearn的plot_glass_brain。plotting:

    plotting.plot_glass_brain(mean_component1_image)
    plotting.plot_glass_brain(mean_component2_image)
    
    要使用覆盖,请调整颜色贴图以使其更易于可视化,其他打印选项请参阅本手册和其他nilearn打印指南:


    如果您还有任何问题,请告诉我。

    要将数据恢复为图像格式,您需要执行NiftiMasker.inverse_transform()。为此,需要在体素空间中保留尺寸

    因此,管道现在的工作方式是,在体素空间上使用降维。如果您想减少主题空间中的维度,您可以更改以下内容:

    pipe = Pipeline([('niftimasker',niftimasker),
                 ('scaler',scaler),
    #                  ('pca',pca)
                ])
    
    X = pipe.fit_transform(imgs)
    X_reduced = pca.fit_transform(X.T).T
    
    X = pipe.fit_transform(imgs)
    
    components = pca.components_
    #In your case 2, but replace range(2) with range(n_components)
    most_important = [np.abs(components[i]).argmax() for i in range(2)]
    
    然后,您将应用逆变换,如下所示:

    component_image = niftimasker.inverse_transform(X_reduced)
    
    然后,要获得每个单独的主题组件图像,您将使用nilearn.image中的index_图像。例如,这是第一个对象组件的图像:

    component1_image = index_img(component_image,0)
    
    component1_image = niftimasker.inverse_transform(comp1)
    component2_image = niftimasker.inverse_transform(comp2)
    
    但是,我认为您对减少体素空间上的死亡感兴趣。因此,为了保留逆变换的体素维度,需要获得PCA维度缩减中选择的每个体素特征的索引。保持管道的原始状态,并执行以下操作:

    pipe = Pipeline([('niftimasker',niftimasker),
                 ('scaler',scaler),
    #                  ('pca',pca)
                ])
    
    X = pipe.fit_transform(imgs)
    X_reduced = pca.fit_transform(X.T).T
    
    X = pipe.fit_transform(imgs)
    
    components = pca.components_
    #In your case 2, but replace range(2) with range(n_components)
    most_important = [np.abs(components[i]).argmax() for i in range(2)]
    
    然后用x个对象和y个体素平铺nan阵列:(在您的示例中为30 x 229007)

    然后对每个组件应用反向变换:

    component1_image = index_img(component_image,0)
    
    component1_image = niftimasker.inverse_transform(comp1)
    component2_image = niftimasker.inverse_transform(comp2)
    
    现在,您将有2个图像,每个图像具有30个主题和1个表示所选组件的有效体素值。如何在30个对象上聚合组件体素取决于您,在这种情况下,我将使用nilearn.image中的平均图像函数:

    mean_component1_image = mean_img(component1_image)
    mean_component2_image = mean_img(component2_image)
    
    最后,在这两种情况下,绘制各自的图像。在体素缩减版本中,您将看到X维(第二张图)中的两幅图像有一点变化,但Y和Z几乎没有变化。我使用的是nilearn的plot_glass_brain。plotting:

    plotting.plot_glass_brain(mean_component1_image)
    plotting.plot_glass_brain(mean_component2_image)
    
    要使用覆盖,请调整颜色贴图以使其更易于可视化,其他打印选项请参阅本手册和其他nilearn打印指南:


    如果您还有任何问题,请告诉我。

    谢谢您的回答。关于你的第一个问题:是的,我想在体素空间中绘制组件,所以我想在主体上运行PCA,而不是针对每个主体。我试图遵循你的代码,但我不确定结果图像显示了什么?当你创造最重要的东西时,会发生什么?对不起,这方面我是个新手。我在期待一个像nilearn.decomposition.CanICA那样的大脑图:PCA和CanICA非常不同。请在此处查看CANICA的描述:。CANICA在执行ICA之前识别组可再现数据子空间。换句话说,在空间上对体素进行分组。PCA只是假设每个体素是独立的,并将每个体素视为一个特征。这将导致200000多个组件中每个组件显示1个体素,而之前的每个组件中很可能显示更多的体素。由于结果没有意义,您能解释一下为什么选择PCA进行降维吗?谢谢您的回答。关于你的第一个问题:是的,我想在体素空间中绘制组件,所以我想在主体上运行PCA,而不是针对每个主体。我试图遵循你的代码,但我不确定结果图像显示了什么?当您创建最重要的