Feb-24-2017, 04:36 PM
Quote:what is the best way to convert the datatype into float? So that I can work with a range of values from -1 to 1.You can use
astype
to convert numpy arrays.>>> a = np.random.randint(0, 255, size=(5,5), dtype=np.uint8) >>> a array([[ 87, 184, 85, 57, 195], [129, 227, 1, 14, 9], [168, 157, 167, 9, 247], [ 6, 136, 98, 70, 214], [105, 221, 123, 55, 54]], dtype=uint8) >>> b = a.astype(np.float) >>> b array([[ 87., 184., 85., 57., 195.], [ 129., 227., 1., 14., 9.], [ 168., 157., 167., 9., 247.], [ 6., 136., 98., 70., 214.], [ 105., 221., 123., 55., 54.]])Now, you can probably find an appropriate function in some library to normalize for you but normalizing yourself isn't that bad anyway.
For example:
>>> 2 * (a / 255.0) - 1 array([[-0.31764706, 0.44313725, -0.33333333, -0.55294118, 0.52941176], [ 0.01176471, 0.78039216, -0.99215686, -0.89019608, -0.92941176], [ 0.31764706, 0.23137255, 0.30980392, -0.92941176, 0.9372549 ], [-0.95294118, 0.06666667, -0.23137255, -0.45098039, 0.67843137], [-0.17647059, 0.73333333, -0.03529412, -0.56862745, -0.57647059]])