Last active
August 29, 2015 14:14
-
-
Save 3100/067b79489dbf75ab8f8f to your computer and use it in GitHub Desktop.
主成分分析でn次元に縮小させた結果から画像を復元してみる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(Image.open(filePath).convert('L')) | |
for n in ns: | |
pca = PCA(n_components=n) | |
pca.fit(imgAry) | |
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