Python Forum
From theory to practice
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
From theory to practice
#5
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]])
Reply


Messages In This Thread
From theory to practice - by bertibott - Feb-24-2017, 01:12 PM
RE: From theory to practice - by stranac - Feb-24-2017, 01:51 PM
RE: From theory to practice - by zivoni - Feb-24-2017, 02:19 PM
RE: From theory to practice - by bertibott - Feb-24-2017, 03:53 PM
RE: From theory to practice - by zivoni - Feb-24-2017, 04:55 PM
RE: From theory to practice - by Mekire - Feb-24-2017, 04:36 PM
RE: From theory to practice - by bertibott - Feb-24-2017, 04:51 PM
RE: From theory to practice - by zivoni - Feb-24-2017, 05:22 PM
RE: From theory to practice - by Mekire - Feb-24-2017, 05:07 PM
RE: From theory to practice - by bertibott - Feb-24-2017, 05:46 PM
RE: From theory to practice - by sparkz_alot - Feb-24-2017, 08:32 PM
RE: From theory to practice - by zivoni - Feb-24-2017, 09:37 PM
RE: From theory to practice - by sparkz_alot - Feb-25-2017, 12:10 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  .xls processing with Pandas - my struggle moving from theory to practical use prolle 0 1,587 May-21-2020, 06:57 PM
Last Post: prolle
  Help with Data Match Theory randor 2 2,065 Dec-25-2019, 05:57 PM
Last Post: randor

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020