Slicing a 2 dimensional array - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Slicing a 2 dimensional array (/thread-36039.html) |
Slicing a 2 dimensional array - Scott - Jan-12-2022 Hi guys, I have the array below: [[1.6727031 1.5464988 1.6836944 1.8492563 1.968533 2.2368639 2.6653275 2.7314425 2.8197284 2.969603 2.8251243 2.086564 2.274447 2.2914152 2.2962196 2.381342 ]]How do I slice it so I have just the last 8 numbers? Thanks RE: Slicing a 2 dimensional array - Scott - Jan-12-2022 y_pred2 = y_pred[0:,8:16] RE: Slicing a 2 dimensional array - paul18fr - Jan-12-2022 Commas are missing in your list. Here after another 2 ways. Note that in your example you've a single row and that's why flatten has been used. ### from a list MyArray = [[1.6727031, 1.5464988, 1.6836944, 1.8492563, 1.968533, 2.2368639, 2.6653275, 2.7314425, 2.8197284, 2.969603, 2.8251243, 2.086564, 2.274447, 2.2914152, 2.2962196, 2.381342 ]] MyArray = np.asarray(MyArray).flatten() Extract = MyArray[-8:] #### from an array MyArray2 = np.array([[1.6727031, 1.5464988, 1.6836944, 1.8492563, 1.968533, 2.2368639, 2.6653275, 2.7314425, 2.8197284, 2.969603, 2.8251243, 2.086564, 2.274447, 2.2914152, 2.2962196, 2.381342 ]]) MyArray2 = MyArray.flatten() Extract2 = MyArray2[-8:] |