<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> print ( __doc__ ) <EOL> import numpy as np <EOL> import matplotlib . pyplot as plt <EOL> from scipy . ndimage import convolve <EOL> from sklearn import linear_model , datasets , metrics <EOL> from sklearn . model_selection import train_test_split <EOL> from sklearn . neural_network import BernoulliRBM <EOL> from sklearn . pipeline import Pipeline <EOL> def nudge_dataset ( X , Y ) : <EOL> """<STR_LIT>""" <EOL> direction_vectors = [ <EOL> [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ] , <EOL> [ [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ] , <EOL> [ [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ] , <EOL> [ [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] ] ] <EOL> shift = lambda x , w : convolve ( x . reshape ( ( <NUM_LIT:8> , <NUM_LIT:8> ) ) , mode = '<STR_LIT>' , <EOL> weights = w ) . ravel ( ) <EOL> X = np . concatenate ( [ X ] + <EOL> [ np . apply_along_axis ( shift , <NUM_LIT:1> , X , vector ) <EOL> for vector in direction_vectors ] ) <EOL> Y = np . concatenate ( [ Y for _ in range ( <NUM_LIT:5> ) ] , axis = <NUM_LIT:0> ) <EOL> return X , Y <EOL> digits = datasets . load_digits ( ) <EOL> X = np . asarray ( digits . data , '<STR_LIT>' ) <EOL> X , Y = nudge_dataset ( X , digits . target ) <EOL> X = ( X - np . min ( X , <NUM_LIT:0> ) ) / ( np . max ( X , <NUM_LIT:0> ) + <NUM_LIT> ) <EOL> X_train , X_test , Y_train , Y_test = train_test_split ( X , Y , <EOL> test_size = <NUM_LIT> , <EOL> random_state = <NUM_LIT:0> ) <EOL> logistic = linear_model . LogisticRegression ( ) <EOL> rbm = BernoulliRBM ( random_state = <NUM_LIT:0> , verbose = True ) <EOL> classifier = Pipeline ( steps = [ ( '<STR_LIT>' , rbm ) , ( '<STR_LIT>' , logistic ) ] ) <EOL> rbm . learning_rate = <NUM_LIT> <EOL> rbm . n_iter = <NUM_LIT:20> <EOL> rbm . n_components = <NUM_LIT:100> <EOL> logistic . C = <NUM_LIT> <EOL> classifier . fit ( X_train , Y_train ) <EOL> logistic_classifier = linear_model . LogisticRegression ( C = <NUM_LIT> ) <EOL> logistic_classifier . fit ( X_train , Y_train ) <EOL> print ( ) <EOL> print ( "<STR_LIT>" % ( <EOL> metrics . classification_report ( <EOL> Y_test , <EOL> classifier . predict ( X_test ) ) ) ) <EOL> print ( "<STR_LIT>" % ( <EOL> metrics . classification_report ( <EOL> Y_test , <EOL> logistic_classifier . predict ( X_test ) ) ) ) <EOL> plt . figure ( figsize = ( <NUM_LIT> , <NUM_LIT:4> ) ) <EOL> for i , comp in enumerate ( rbm . components_ ) : <EOL> plt . subplot ( <NUM_LIT:10> , <NUM_LIT:10> , i + <NUM_LIT:1> ) <EOL> plt . imshow ( comp . reshape ( ( <NUM_LIT:8> , <NUM_LIT:8> ) ) , cmap = plt . cm . gray_r , <EOL> interpolation = '<STR_LIT>' ) <EOL> plt . xticks ( ( ) ) <EOL> plt . yticks ( ( ) ) <EOL> plt . suptitle ( '<STR_LIT>' , fontsize = <NUM_LIT:16> ) <EOL> plt . subplots_adjust ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> plt . show ( ) </s>
