<s> import itertools <EOL> import matplotlib . pyplot as plt <EOL> import matplotlib . image as mpimg <EOL> from mpl_toolkits . mplot3d import Axes3D <EOL> from sklearn . preprocessing import StandardScaler <EOL> from sklearn . decomposition import PCA <EOL> from sklearn . pipeline import Pipeline <EOL> from sklearn . cluster import KMeans <EOL> import numpy as np <EOL> import scipy . io as sio <EOL> from ex7_pca import project_data , recover_data , display_data <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> data = sio . loadmat ( '<STR_LIT>' ) <EOL> X = data [ '<STR_LIT:X>' ] <EOL> x1 , x2 = X . T <EOL> plt . plot ( x1 , x2 , '<STR_LIT>' ) <EOL> plt . show ( ) <EOL> ss = StandardScaler ( ) <EOL> pca = PCA ( ) <EOL> pipeline = Pipeline ( [ ( "<STR_LIT>" , ss ) , <EOL> ( "<STR_LIT>" , pca ) ] ) <EOL> pipeline . fit ( X ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % pca . components_ [ <NUM_LIT:0> , : ] ) <EOL> print ( '<STR_LIT>' ) <EOL> X_norm = ss . transform ( X ) <EOL> K = <NUM_LIT:1> <EOL> U = pca . components_ . T <EOL> Z = project_data ( X_norm , U , K ) <EOL> print ( '<STR_LIT>' % Z [ <NUM_LIT:0> ] ) <EOL> print ( '<STR_LIT>' ) <EOL> X_rec = recover_data ( Z , U , K ) <EOL> print ( '<STR_LIT>' % X_rec [ <NUM_LIT:0> ] ) <EOL> print ( '<STR_LIT>' ) <EOL> x1 , x2 = X_norm . T <EOL> plt . plot ( x1 , x2 , '<STR_LIT>' ) <EOL> x1 , x2 = X_rec . T <EOL> plt . plot ( x1 , x2 , '<STR_LIT>' ) <EOL> plt . axis ( '<STR_LIT>' ) <EOL> for x_rec , x_norm in zip ( X_rec , X_norm ) : <EOL> plt . plot ( ( x_rec [ <NUM_LIT:0> ] , x_norm [ <NUM_LIT:0> ] ) , ( x_rec [ <NUM_LIT:1> ] , x_norm [ <NUM_LIT:1> ] ) , '<STR_LIT>' ) <EOL> plt . show ( ) <EOL> data = sio . loadmat ( '<STR_LIT>' ) <EOL> X = data [ '<STR_LIT:X>' ] <EOL> display_data ( X [ : <NUM_LIT:100> , : ] , '<STR_LIT>' ) <EOL> pipeline . fit ( X ) <EOL> X_norm = ss . transform ( X ) <EOL> U = pca . components_ . T <EOL> display_data ( U [ : , : <NUM_LIT> ] . T , '<STR_LIT>' ) <EOL> K = <NUM_LIT:100> <EOL> Z = project_data ( X_norm , U , K ) <EOL> X_rec = recover_data ( Z , U , K ) <EOL> display_data ( X_norm [ : <NUM_LIT:100> , : ] , '<STR_LIT>' ) <EOL> display_data ( X_rec [ : <NUM_LIT:100> , : ] , '<STR_LIT>' ) <EOL> img = mpimg . imread ( '<STR_LIT>' ) <EOL> img_size = img . shape <EOL> X = img . reshape ( img_size [ <NUM_LIT:0> ] * img_size [ <NUM_LIT:1> ] , img_size [ <NUM_LIT:2> ] ) <EOL> K = <NUM_LIT:16> <EOL> max_iters = <NUM_LIT:10> <EOL> km = KMeans ( K , max_iter = max_iters ) <EOL> km . fit ( X ) <EOL> to_plot = range ( len ( X ) ) <EOL> np . random . shuffle ( to_plot ) <EOL> to_plot = to_plot [ : <NUM_LIT:1000> ] <EOL> X_plot = X [ to_plot , : ] <EOL> idx_plot = km . labels_ [ to_plot ] <EOL> fig = plt . figure ( ) <EOL> ax = fig . add_subplot ( <NUM_LIT> , projection = '<STR_LIT>' ) <EOL> for k , color in zip ( xrange ( K ) , itertools . cycle ( [ '<STR_LIT:r>' , '<STR_LIT:b>' , '<STR_LIT:g>' , '<STR_LIT:k>' , '<STR_LIT:m>' ] ) ) : <EOL> x , y , z = X_plot [ idx_plot == k , : ] . T <EOL> ax . scatter ( x , y , z , c = color ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> plt . show ( ) <EOL> pipeline . fit ( X ) <EOL> X_norm = ss . transform ( X ) <EOL> U = pca . components_ . T <EOL> Z = project_data ( X_norm , U , <NUM_LIT:2> ) <EOL> Z_plot = Z [ to_plot , : ] <EOL> for k , color in zip ( xrange ( K ) , itertools . cycle ( [ '<STR_LIT:r>' , '<STR_LIT:b>' , '<STR_LIT:g>' , '<STR_LIT:k>' , '<STR_LIT:m>' ] ) ) : <EOL> x1 , x2 = Z_plot [ idx_plot == k , : ] . T <EOL> plt . plot ( x1 , x2 , color + '<STR_LIT:o>' ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> plt . show ( ) </s>
