<s> import numpy as np <EOL> from pylearn2 . datasets . dense_design_matrix import DenseDesignMatrix <EOL> from pylearn2 . datasets . dense_design_matrix import DenseDesignMatrixPyTables <EOL> from pylearn2 . datasets . dense_design_matrix import DefaultViewConverter <EOL> from pylearn2 . datasets . dense_design_matrix import from_dataset <EOL> from pylearn2 . utils import serial <EOL> def test_init_with_X_or_topo ( ) : <EOL> rng = np . random . RandomState ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> topo_view = rng . randn ( <NUM_LIT:5> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> d1 = DenseDesignMatrix ( topo_view = topo_view ) <EOL> X = d1 . get_design_matrix ( ) <EOL> d2 = DenseDesignMatrix ( X = X , view_converter = d1 . view_converter ) <EOL> topo_view_2 = d2 . get_topological_view ( ) <EOL> assert np . allclose ( topo_view , topo_view_2 ) <EOL> X = rng . randn ( * X . shape ) <EOL> topo_view_3 = d2 . get_topological_view ( X ) <EOL> X2 = d2 . get_design_matrix ( topo_view_3 ) <EOL> assert np . allclose ( X , X2 ) <EOL> def test_convert_to_one_hot ( ) : <EOL> rng = np . random . RandomState ( [ <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT> ] ) <EOL> m = <NUM_LIT:11> <EOL> d = DenseDesignMatrix ( <EOL> X = rng . randn ( m , <NUM_LIT:4> ) , <EOL> y = rng . randint ( low = <NUM_LIT:0> , high = <NUM_LIT:10> , size = ( m , ) ) ) <EOL> d . convert_to_one_hot ( ) <EOL> def test_init_with_vc ( ) : <EOL> rng = np . random . RandomState ( [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) <EOL> d = DenseDesignMatrix ( <EOL> X = rng . randn ( <NUM_LIT:12> , <NUM_LIT:5> ) , <EOL> view_converter = DefaultViewConverter ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) ) <EOL> def get_rnd_design_matrix ( ) : <EOL> rng = np . random . RandomState ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> topo_view = rng . randn ( <NUM_LIT:10> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> d1 = DenseDesignMatrix ( topo_view = topo_view ) <EOL> return d1 <EOL> def test_split_datasets ( ) : <EOL> ddm = get_rnd_design_matrix ( ) <EOL> ( train , valid ) = ddm . split_dataset_holdout ( train_prop = <NUM_LIT:0.5> ) <EOL> assert valid . shape [ <NUM_LIT:0> ] == np . ceil ( ddm . get_num_examples ( ) * <NUM_LIT:0.5> ) <EOL> assert train . shape [ <NUM_LIT:0> ] == ( ddm . get_num_examples ( ) - valid . shape [ <NUM_LIT:0> ] ) <EOL> def test_split_nfold_datasets ( ) : <EOL> ddm = get_rnd_design_matrix ( ) <EOL> folds = ddm . split_dataset_nfolds ( <NUM_LIT:10> ) <EOL> assert folds [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> ] == np . ceil ( ddm . get_num_examples ( ) / <NUM_LIT:10> ) <EOL> def test_pytables ( ) : <EOL> """<STR_LIT>""" <EOL> x = np . ones ( ( <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> y = np . ones ( <NUM_LIT:2> ) <EOL> ds = DenseDesignMatrixPyTables ( X = x , y = y ) <EOL> it = ds . iterator ( mode = '<STR_LIT>' , batch_size = <NUM_LIT:1> ) <EOL> it . next ( ) <EOL> def test_init_pytables_with_labels ( ) : <EOL> """<STR_LIT>""" <EOL> rng = np . random . RandomState ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> X = rng . randn ( <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> y = rng . randint ( low = <NUM_LIT:0> , high = <NUM_LIT:5> , size = ( <NUM_LIT:2> , ) ) <EOL> ds = DenseDesignMatrixPyTables ( <EOL> X = X , <EOL> y = y , <EOL> X_labels = len ( np . unique ( X ) . flat ) , <EOL> y_labels = np . max ( y ) + <NUM_LIT:1> <EOL> ) <EOL> def test_from_dataset ( ) : <EOL> """<STR_LIT>""" <EOL> rng = np . random . RandomState ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> topo_view = rng . randn ( <NUM_LIT:12> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> y = rng . randint ( <NUM_LIT:0> , <NUM_LIT:5> , ( <NUM_LIT:12> , <NUM_LIT:1> ) ) <EOL> d1 = DenseDesignMatrix ( topo_view = topo_view ) <EOL> slice_d = from_dataset ( d1 , <NUM_LIT:5> ) <EOL> assert slice_d . X . shape [ <NUM_LIT:1> ] == d1 . X . shape [ <NUM_LIT:1> ] <EOL> assert slice_d . X . shape [ <NUM_LIT:0> ] == <NUM_LIT:5> <EOL> d2 = DenseDesignMatrix ( topo_view = topo_view , y = y ) <EOL> slice_d = from_dataset ( d2 , <NUM_LIT:5> ) <EOL> assert slice_d . X . shape [ <NUM_LIT:1> ] == d2 . X . shape [ <NUM_LIT:1> ] <EOL> assert slice_d . X . shape [ <NUM_LIT:0> ] == <NUM_LIT:5> <EOL> assert slice_d . y . shape [ <NUM_LIT:0> ] == <NUM_LIT:5> <EOL> x = topo_view . reshape ( <NUM_LIT:12> , <NUM_LIT> ) <EOL> d3 = DenseDesignMatrix ( X = x , y = y ) <EOL> slice_d = from_dataset ( d3 , <NUM_LIT:5> ) <EOL> assert slice_d . X . shape [ <NUM_LIT:1> ] == d3 . X . shape [ <NUM_LIT:1> ] <EOL> assert slice_d . X . shape [ <NUM_LIT:0> ] == <NUM_LIT:5> <EOL> assert slice_d . y . shape [ <NUM_LIT:0> ] == <NUM_LIT:5> </s>
