Module Expr Str ImportFrom alias Expr Call NameLoad NameLoad Import alias identifier Import alias identifier ImportFrom alias ImportFrom alias alias alias ImportFrom alias ImportFrom alias ImportFrom alias FunctionDef arguments args NameParam NameParam defaults body Expr Str Assign NameStore ListLoad ListLoad ListLoad Num Num Num ListLoad Num Num Num ListLoad Num Num Num ListLoad ListLoad Num Num Num ListLoad Num Num Num ListLoad Num Num Num ListLoad ListLoad Num Num Num ListLoad Num Num Num ListLoad Num Num Num ListLoad ListLoad Num Num Num ListLoad Num Num Num ListLoad Num Num Num Assign NameStore Lambda arguments args NameParam NameParam defaults Call AttributeLoad Call NameLoad Call AttributeLoad NameLoad attr TupleLoad Num Num keyword Str keyword NameLoad attr Assign NameStore Call AttributeLoad NameLoad attr BinOpAdd ListLoad NameLoad ListComp Call AttributeLoad NameLoad attr NameLoad Num NameLoad NameLoad comprehension NameStore NameLoad Assign NameStore Call AttributeLoad NameLoad attr ListComp NameLoad comprehension NameStore Call NameLoad Num keyword Num Return TupleLoad NameLoad NameLoad decorator_list Assign NameStore Call AttributeLoad NameLoad attr Assign NameStore Call AttributeLoad NameLoad attr AttributeLoad NameLoad attr Str Assign TupleStore NameStore NameStore Call NameLoad NameLoad AttributeLoad NameLoad attr Assign NameStore BinOpDiv BinOpSub NameLoad Call AttributeLoad NameLoad attr NameLoad Num BinOpAdd Call AttributeLoad NameLoad attr NameLoad Num Num Assign TupleStore NameStore NameStore NameStore NameStore Call NameLoad NameLoad NameLoad keyword Num keyword Num Assign NameStore Call AttributeLoad NameLoad attr Assign NameStore Call NameLoad keyword Num keyword NameLoad Assign NameStore Call NameLoad keyword ListLoad TupleLoad Str NameLoad TupleLoad Str NameLoad Assign AttributeStore NameLoad attr Num Assign AttributeStore NameLoad attr Num Assign AttributeStore NameLoad attr Num Assign AttributeStore NameLoad attr Num Expr Call AttributeLoad NameLoad attr NameLoad NameLoad Assign NameStore Call AttributeLoad NameLoad attr keyword Num Expr Call AttributeLoad NameLoad attr NameLoad NameLoad Expr Call NameLoad Expr Call NameLoad BinOpMod Str Call AttributeLoad NameLoad attr NameLoad Call AttributeLoad NameLoad attr NameLoad Expr Call NameLoad BinOpMod Str Call AttributeLoad NameLoad attr NameLoad Call AttributeLoad NameLoad attr NameLoad Expr Call AttributeLoad NameLoad attr keyword TupleLoad Num Num For TupleStore NameStore NameStore Call NameLoad AttributeLoad NameLoad attr body Expr Call AttributeLoad NameLoad attr Num Num BinOpAdd NameLoad Num Expr Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr TupleLoad Num Num keyword AttributeLoad AttributeLoad NameLoad attr attr keyword Str Expr Call AttributeLoad NameLoad attr TupleLoad Expr Call AttributeLoad NameLoad attr TupleLoad Expr Call AttributeLoad NameLoad attr Str keyword Num Expr Call AttributeLoad NameLoad attr Num Num Num Num Num Num Expr Call AttributeLoad NameLoad attr
N/A N/A 
==============================================================
Restricted Boltzmann Machine features for digit classification
==============================================================

For greyscale image data where pixel values can be interpreted as degrees of
blackness on a white background, like handwritten digit recognition, the
Bernoulli Restricted Boltzmann machine model (:class:`BernoulliRBM
<sklearn.neural_network.BernoulliRBM>`) can perform effective non-linear
feature extraction.

In order to learn good latent representations from a small dataset, we
artificially generate more labeled data by perturbing the training data with
linear shifts of 1 pixel in each direction.

This example shows how to build a classification pipeline with a BernoulliRBM
feature extractor and a :class:`LogisticRegression
<sklearn.linear_model.LogisticRegression>` classifier. The hyperparameters
of the entire model (learning rate, hidden layer size, regularization)
were optimized by grid search, but the search is not reproduced here because
of runtime constraints.

Logistic regression on raw pixel values is presented for comparison. The
example shows that the features extracted by the BernoulliRBM help improve the
classification accuracy.
 __future__ print_function N/A N/A print __doc__ N/A numpy np N/A matplotlib.pyplot plt scipy.ndimage convolve sklearn linear_model datasets metrics sklearn.model_selection train_test_split sklearn.neural_network BernoulliRBM sklearn.pipeline Pipeline nudge_dataset N/A N/A X Y N/A N/A N/A 
    This produces a dataset 5 times bigger than the original one,
    by moving the 8x8 images in X around by 1px to left, right, down, up
     N/A direction_vectors N/A N/A N/A 0 1 0 N/A 0 0 0 N/A 0 0 0 N/A N/A 0 0 0 N/A 1 0 0 N/A 0 0 0 N/A N/A 0 0 0 N/A 0 0 1 N/A 0 0 0 N/A N/A 0 0 0 N/A 0 0 0 N/A 0 1 0 N/A shift N/A N/A N/A x w N/A N/A N/A N/A convolve N/A N/A x reshape N/A 8 8 mode constant weights w ravel N/A X N/A N/A np concatenate N/A N/A X N/A N/A N/A np apply_along_axis shift 1 X vector N/A vector direction_vectors N/A Y N/A N/A np concatenate N/A Y N/A _ N/A range 5 axis 0 N/A N/A X Y N/A N/A digits N/A N/A datasets load_digits N/A X N/A N/A np asarray N/A digits data float32 N/A N/A X Y N/A nudge_dataset X N/A digits target N/A X N/A N/A X N/A N/A np min X 0 N/A N/A N/A np max X 0 0.0001 N/A N/A X_train X_test Y_train Y_test N/A train_test_split X Y test_size 0.2 random_state 0 N/A logistic N/A N/A linear_model LogisticRegression N/A rbm N/A BernoulliRBM random_state 0 verbose True N/A classifier N/A Pipeline steps N/A N/A rbm rbm N/A logistic logistic N/A N/A rbm learning_rate 0.06 N/A N/A rbm n_iter 20 N/A N/A rbm n_components 100 N/A N/A logistic C 6000.0 N/A N/A N/A classifier fit X_train Y_train N/A logistic_classifier N/A N/A linear_model LogisticRegression C 100.0 N/A N/A N/A logistic_classifier fit X_train Y_train N/A N/A print N/A N/A print N/A Logistic regression using RBM features:
%s
 N/A N/A metrics classification_report Y_test N/A N/A classifier predict X_test N/A N/A print N/A Logistic regression using raw pixel features:
%s
 N/A N/A metrics classification_report Y_test N/A N/A logistic_classifier predict X_test N/A N/A N/A plt figure figsize N/A 4.2 4 N/A N/A i comp N/A enumerate N/A rbm components_ N/A N/A N/A N/A plt subplot 10 10 N/A i 1 N/A N/A N/A plt imshow N/A N/A comp reshape N/A 8 8 cmap N/A N/A plt cm gray_r interpolation nearest N/A N/A N/A plt xticks N/A N/A N/A N/A plt yticks N/A N/A N/A N/A plt suptitle 100 components extracted by RBM fontsize 16 N/A N/A N/A plt subplots_adjust 0.08 0.02 0.92 0.85 0.08 0.23 N/A N/A N/A plt show