How to understand "reshape(-1)"
2019-02-09 本文已影响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)