From 01fbbeb69c196c6f77663867fc511316da7f7eae Mon Sep 17 00:00:00 2001 From: rasbt Date: Tue, 23 Sep 2014 13:31:37 -0400 Subject: [PATCH] make bitstring func --- howtos_as_py_files/make_bitstring.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 howtos_as_py_files/make_bitstring.py diff --git a/howtos_as_py_files/make_bitstring.py b/howtos_as_py_files/make_bitstring.py new file mode 100644 index 0000000..fc2baa4 --- /dev/null +++ b/howtos_as_py_files/make_bitstring.py @@ -0,0 +1,14 @@ +# Generating a bitstring from a Python list or numpy array +# where all postive values -> 1 +# all negative values -> 0 + +def make_bitstring(ary) + return np.where(ary > 0, 1, 0) + + +### Example: + +ary1 = np.array([1, 2, 0.3, -1, -2]) +make_bitstring(ary1) + +# returns array([1, 1, 1, 0, 0]) \ No newline at end of file