Python Forum

Full Version: Slicing a 2 dimensional array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
y_pred2 = y_pred[0:,8:16]
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:]