24.07.2016 Views

www.allitebooks.com

Learning%20Data%20Mining%20with%20Python

Learning%20Data%20Mining%20with%20Python

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Extracting Features with Transformers<br />

We then create our transformer instance and fit it using this test data:<br />

mean_discrete = MeanDiscrete()<br />

mean_discrete.fit(X_test)<br />

Next, we check whether the internal mean parameter was correctly set by <strong>com</strong>paring<br />

it with our independently verified result:<br />

assert_array_equal(mean_discrete.mean, np.array([13.5, 15.5]))<br />

We then run the transform to create the transformed dataset. We also create an<br />

(independently <strong>com</strong>puted) array with the expected values for the output:<br />

X_transformed = mean_discrete.transform(X_test)<br />

X_expected = np.array([[ 0, 0],<br />

[ 0, 0],<br />

[ 0, 0],<br />

[ 0, 0],<br />

[ 0, 0],<br />

[ 1, 1],<br />

[ 1, 1],<br />

[ 1, 1],<br />

[ 1, 1],<br />

[ 1, 1]])<br />

Finally, we test that our returned result is indeed what we expected:<br />

assert_array_equal(X_transformed, X_expected)<br />

We can run the test by simply running the function itself:<br />

test_meandiscrete()<br />

If there was no error, then the test ran without an issue! You can verify this by<br />

changing some of the tests to deliberately incorrect values, and seeing that the test<br />

fails. Remember to change them back so that the test passes.<br />

If we had multiple tests, it would be worth using a testing framework called nose to<br />

run our tests.<br />

[ 102 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!