本文由公众号《湾区人工智能》提供,如果觉得不错,请扫码关注,了解更多Python和人工智能知识
sklearn对三维矩阵降维
思路:
把三维矩阵reshape成二维,然后对二维矩阵降维,然后把二维矩阵再次reshape成三维,实现如下数组
(640, 640, 128) --(640, 640, 3)
代码:
#https://blog.csdn.net/u012162613/article/details/42192293
#https://blog.csdn.net/puredreammer/article/details/52255025
#每次降维后结果不同呢:属于无监督学习,类似聚类一样;fit()可以说是scikit-learn中通用的方法,每个需要训练的算法都会有fit()方法,它其实就是算法中的“训练”这一步骤。因为PCA是无监督学习算法,此处y自然等于None。
#LDA,有监督学习 in contrast to PCA, is a supervised method, using known class labels.
from sklearn.decomposition import PCA
import numpy as np
import matplotlib.pyplot as plt
import scipy.io as sio
data = sio.loadmat('docia.mat') # docia.mat其实是128个640*640的矩阵的叠加,当只有一个通道的时候就是黑白图片,3个通道是RGB图片,128个通道就是128个图片的叠加
print(data.keys()) # 即可知道Mat文件中存在数据名,embedmap
#print(data['embedmap'])
print(data['embedmap'].shape)
reshaped_data = data['embedmap'].reshape((409600, 128)) #reshape()是数组对象中的方法,用于改变数组的形状。
pca = PCA(n_components=3) #n_components返回所保留的成分个数n。
#pca.fit(reshaped_data) #fit(X),表示用数据X来训练PCA模型;fit()可以说是scikit-learn中通用的方法,每个需要训练的算法都会有fit()方法,它其实就是算法中的“训练”这一步骤。因为PCA是无监督学习算法,此处y自然等于None。
PCA(copy=True, n_components=3, whiten=True) #whiten=True使得每个特征具有相同的方差。copy=True表示在运行算法时,将原始训练数据复制一份
pcaData=pca.fit_transform(reshaped_data) #用reshaped_data来训练PCA模型,同时返回降维后的数据。
res_pcaData = pcaData.reshape((640,640,3))
print(res_pcaData.shape)
print(type(res_pcaData)) #里面有负值,在-9到5之间
输出:
dict_keys(['__version__', '__header__', 'embedmap', '__globals__'])
(640, 640, 128)
(640, 640, 3)
<class 'numpy.ndarray'>
[Finished in 7.0s]