-
Notifications
You must be signed in to change notification settings - Fork 3
/
pca.py
executable file
·58 lines (43 loc) · 1.58 KB
/
pca.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/python
import os
import sys
import numpy
from code_folder.utils import tile_raster_images
import PIL.Image
from sklearn.decomposition import PCA
def read_json_bz2(path2data):
import bz2,json,contextlib
with contextlib.closing(bz2.BZ2File(path2data, 'rb')) as f:
return numpy.array(json.load(f))
def get_pcs(dataset='numbers.x.bz2',
output_folder='dA_plots',
n_comps = (10, 10)):
print "Reading Data"
x_data = read_json_bz2(dataset)
print x_data.shape
if not os.path.isdir(output_folder):
os.makedirs(output_folder)
os.chdir(output_folder)
n_features = x_data.shape[1]
side = int(n_features ** .5)
assert(side ** 2 == n_features) # Need perfect square
image2 = PIL.Image.fromarray(tile_raster_images(
X=x_data, scale_rows_to_unit_interval=False,
img_shape=(side, side), tile_shape=n_comps,
tile_spacing=(1, 1)))
image2.save('sample_x.png')
print "PCAing"
pca = PCA(n_components=n_comps[0]*n_comps[1])
pca.fit(x_data)
print(pca.explained_variance_ratio_)
print "Saving images"
image = PIL.Image.fromarray(tile_raster_images(
X=pca.components_,
img_shape=(side, side), tile_shape=n_comps,
tile_spacing=(1, 1)))
image.save(dataset+'pca.png')
os.chdir('../')
print "Raster saved"
if __name__ == '__main__':
kwargs = dict([arg.split('=', 1) for arg in sys.argv[1:]])
get_pcs(**kwargs)