Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
What would you like to do?
from sklearn.decomposition import PCA
from PIL import Image
import numpy as np
import scipy as sp
if __name__ == '__main__':
# 画像ファイル名に変えてください
filePath = 'gs.jpg'
# 試行するnの組(画像ピクセルサイズの最大数を超えないこと)
ns = [1,2,5,10,50,100,250]
imgAry = np.asarray('L'))
for n in ns:
pca = PCA(n_components=n)
pca_res = pca.transform(imgAry)
restored = pca.inverse_transform(pca_res)
print sum(pca.explained_variance_ratio_)
sp.misc.imsave('result' + str(n) + '.png', restored)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment