美文网首页
How to understand "reshape(-1)"

How to understand "reshape(-1)"

作者: 宣雄民 | 来源:发表于2019-02-09 11:56 被阅读0次
    • Python is advantageous in mathematical features over many other languages such as list, array, map(dictionary), dataframe, etc...
    • Sometimes it is pretty confused of many features since other programming languages behave slightly different.
    • Reshape(-1) is a useful technic in python where you can just turn your data object such as list into 1 row or 1 column in which you pretend that you don't yet know how many rows or columns you need to for calculation.
      • The method is performed quite intuitively
      • Check the demo code
    In [1]: import numpy as np                                                                                             
    
    In [2]: orig_list = np.array([[1,2,3,4], [22,33,11,11], ['a', 'b', 'c', 'd']])                                         
    
    In [3]: orig_list                                                                                                      
    Out[3]: 
    array([['1', '2', '3', '4'],
           ['22', '33', '11', '11'],
           ['a', 'b', 'c', 'd']], dtype='<U21')
    
    In [4]: orig_list.shape                                                                                                
    Out[4]: (3, 4)
    
    In [5]: new_list = orig_list.reshape(-1)                                                                               
    
    In [6]: new_list                                                                                                       
    Out[6]: 
    array(['1', '2', '3', '4', '22', '33', '11', '11', 'a', 'b', 'c', 'd'],
          dtype='<U21')
    
    In [7]: new_list.shape                                                                                                 
    Out[7]: (12,)
    
    In [8]:  
    
    
    • Notice that such technic only resides within python(numpy package)

    相关文章

      网友评论

          本文标题:How to understand "reshape(-1)"

          本文链接:https://www.haomeiwen.com/subject/uliasqtx.html