Python Forum
Slicing a 2 dimensional array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Slicing a 2 dimensional array
#1
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
Reply
#2
y_pred2 = y_pred[0:,8:16]
Reply
#3
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:]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to quantize a 4 dimensional array? PythonNPC 2 1,579 Apr-23-2022, 04:34 PM
Last Post: Gribouillis
  How to create 2 dimensional variables in Python? plumberpy 5 1,791 Mar-31-2022, 03:15 AM
Last Post: plumberpy
  Problem using two-dimensional interpolation. Result looks bad player1682 4 2,451 Oct-12-2021, 09:27 AM
Last Post: player1682
  Index data must be 1-dimensional : Classifier with sklearn Salma 0 4,263 Apr-01-2021, 03:22 PM
Last Post: Salma
  2 Dimensional Arrays Prithak 4 2,553 Mar-21-2021, 09:35 PM
Last Post: deanhystad
  comparing 2 dimensional list glennford49 10 4,037 Mar-24-2020, 05:23 PM
Last Post: saikiran
  Python 2 dimensional array creations sharpe 1 1,940 Nov-24-2019, 10:33 AM
Last Post: ThomasL
  Native support for array-slicing syntax? nxs 1 2,224 Apr-22-2019, 02:34 AM
Last Post: ichabod801
  Python: Creating N Dimensional Grid jf451 0 2,299 Dec-06-2018, 10:53 PM
Last Post: jf451
  Get last tuple from two-dimensional array? Winfried 6 3,782 Aug-27-2018, 01:48 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020