Transformer une matrice de nombres réels en nombres entiers avec python

Published: 21 février 2017

DMCA.com Protection Status

Pour transformer une matrice de nombres décimaux en nombres entiers avec python il y a la fonction numpy
astype. Exemple, pour transformer la matrice A de nombres réels:

>>> import numpy as np
>>> A = np.logspace(0.1, 3, 100, endpoint=True)
>>> A
array([    1.25892541,     1.34676844,     1.44074081,     1.54127022,
           1.64881419,     1.76386217,     1.88693776,     2.0186011 ,
           2.1594514 ,     2.3101297 ,     2.47132176,     2.64376119,
           2.82823278,     3.0255761 ,     3.23668929,     3.46253315,
           3.70413553,     3.96259601,     4.23909088,     4.53487851,
           4.85130507,     5.18981068,     5.55193591,     5.93932887,
           6.35375264,     6.79709332,     7.27136862,     7.77873705,
           8.32150772,     8.90215085,     9.52330906,    10.18780932,
          10.89867589,    11.65914401,    12.47267471,    13.34297048,
          14.27399218,    15.26997703,    16.33545792,    17.475284  ,
          18.69464281,    19.99908384,    21.3945438 ,    22.88737364,
          24.48436747,    26.19279345,    28.02042689,    29.97558564,
          32.06716793,    34.30469286,    36.69834378,    39.25901454,
          41.99835916,    44.92884483,    48.06380863,    51.41751828,
          55.00523702,    58.84329312,    62.94915417,    67.34150658,
          72.04034062,    77.06704142,    82.44448627,    88.19714876,
          94.35121014,   100.9346785 ,   107.97751623,   115.51177638,
         123.57174852,   132.19411485,   141.41811708,   151.28573508,
         161.84187792,   173.13458823,   185.21526088,   198.1388769 ,
         211.96425366,   226.75431259,   242.57636554,   259.50242114,
         277.60951248,   296.98004774,   317.70218523,   339.87023462,
         363.58508612,   388.9546697 ,   416.09444628,   445.12793318,
         476.1872663 ,   509.41380148,   544.95875783,   582.98390597,
         623.66230422,   667.17908629,   713.7323038 ,   763.53382765,
         816.81031231,   873.8042273 ,   934.77496078,  1000.        ])

avec astype:

>>> B = A.astype(int)
>>> B
array([   1,    1,    1,    1,    1,    1,    1,    2,    2,    2,    2,
          2,    2,    3,    3,    3,    3,    3,    4,    4,    4,    5,
          5,    5,    6,    6,    7,    7,    8,    8,    9,   10,   10,
         11,   12,   13,   14,   15,   16,   17,   18,   19,   21,   22,
         24,   26,   28,   29,   32,   34,   36,   39,   41,   44,   48,
         51,   55,   58,   62,   67,   72,   77,   82,   88,   94,  100,
        107,  115,  123,  132,  141,  151,  161,  173,  185,  198,  211,
        226,  242,  259,  277,  296,  317,  339,  363,  388,  416,  445,
        476,  509,  544,  582,  623,  667,  713,  763,  816,  873,  934,
       1000])

Références