Slicing Arrays in Python

Including Cython, Jython, IronPython, PyPy, Django framework, and interpreters: Ipython, IPython Jupyter/Notebook, CPython


Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5303
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

The standard rules of Python slicing apply to arrays. Basic slicing for example on lists, extends Python’s basic concept of slicing to N dimensions. Here are a number of Python array slicing examples.

  1. import numpy as np
  2. #Create a 10x10  array:
  3. a = np.arange(100).reshape(10, 10)
  4. In [1]: a
  5. Out[1]:
  6. array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
  7.        [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
  8.        [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
  9.        [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
  10.        [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
  11.        [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
  12.        [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
  13.        [70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
  14.        [80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
  15.        [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
  16. #Even-indexed elements along all columns
  17. a[::2,:]
  18. #Even-indexed elements along column 1
  19. a[::2,0]
  20. #All columns except the first
  21. a[::,1:]
  22. #First column:
  23. a[::,0] #Also a[::1,0]
  24. #Last column
  25. a[::, -1]
  26. #All columns except the first and the last
  27. a[::, 1:-1]
  28. #All columns except the last
  29. a[::, :-1]
  30. #Firts two columns
  31. a[::,0:2]
  32. #Last two columns
  33. a[::,-2:]
  34. #Last column but one
  35. a[::,-2]
  36. #All except last two columns
  37. a[::,:-2]
  38. #or
  39. a[::,:-2:]
  40. #Odd-indexed elements along the column 0
  41. a[1::2,0]
  42. #Odd-indexed elements along all columns
  43. a[1::2,:]
  44. #First row:
  45. a[0] # Also a[0,::]
  46. #Second row
  47. a[1,::] # Also a[1]
  48. #Last row
  49. a[-1,::] #Also a[-1], a[-1:,::]
  50. #Last two rows
  51. a[-2:,::]
  52. #All rows except last
  53. a[:-1,::]
  54. #All rows except the first
  55. a[1:,::]
  56. #All between the first and last rows
  57. a[1:-1,::]
  58. #First element of the array
  59. a[0,0]
  60. #or
  61. a[0][0]  #Element in the first row and first column
  62. #Last element of the array
  63. a[-1,-1] #Also a[-1][-1]
  64. #Try
  65. a[::2]
  66. #Change the order or rows, from last to first
  67. a[::-1]
  68. #Reverse all columns
  69. a[::-1,:]
  70. #Reverse elements in the first column
  71. a[::-1,0]
  72. #Reverse elements in the first row
  73. a[0,::-1]
  74. #Reverse all rows
  75. a[:,::-1]
  76. #Sum chunks of two elements along column 0
  77. a[::2,0] + a[1::2,0]
  78. #sum chunks of two elements along all columns
  79. a[::2,:] + a[1::2,:]
  80. #Sum chunks of two elements along all rows
  81. a[:,::2] + a[:,1::2]
  82. #Sum chunks of two elements along the first row
  83. a[0,::2] + a[0,1::2]
  84. #Try to sum chunks of five elements along columns
  85. #One way to do this is as follows:
  86. In [2]: r = a[::,0]   #We want to use the column length
  87. In [3]: b = [sum(a[values: values+5]) for values in xrange(0, len(c), 5)] #Remember to use range in Python3+ instead of xrange
  88. In [3]: b  #Return b as array of sums
  89. Out[3]:
  90. [array([100, 105, 110, 115, 120, 125, 130, 135, 140, 145]),
  91.  array([350, 355, 360, 365, 370, 375, 380, 385, 390, 395])]
  92. #For the first row (choose any row):
  93. In [4]: c = a[::,0]
  94. In [5]: b = [sum(c[values: values+5]) for values in xrange(0, len(c), 5)]
  95. In [6]: b
  96. Out[6]: [100, 350]
  97. #You can however use, the (http://docs.scipy.org/doc/numpy/reference/generated/numpy.ufunc.reduceat.html#numpy-ufunc-reduceat)add unfunc with reduceat method:
  98. In [7]: np.add.reduceat(a, [0,5])
  99. Out[8]:
  100. array([[100, 105, 110, 115, 120, 125, 130, 135, 140, 145],
  101.        [350, 355, 360, 365, 370, 375, 380, 385, 390, 395]])
  102. #And for the first row:
  103. In [9]: c = a[::,0]
  104. In [10]: np.add.reduceat(c, [0,5])
  105. Out[10]: array([100, 350])
  106. #You can include 5 as step:
  107. In [11]: b = np.arange(100)
  108. In [12]:  np.add.reduceat(b, range(0, 100, 5))
  109. Out[12]:
  110. array([ 10,  35,  60,  85, 110, 135, 160, 185, 210, 235, 260, 285, 310,
  111.        335, 360, 385, 410, 435, 460, 485])

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5303
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#2

If you have an N-dimensional dataset, you can drop either of the dimension using Numpy indexing/slicing. Consider the following piece of code where we create a 3-dimensional dataset and then drop the first dimension:

  1. import numpy as np
  2. data = np.zeros((10, 100, 500))
  3. new_data = data[0, :, :] # Drop the first dimension
  4. print(new_data.shape)


Our new dataset is reduced to two dimensions, i.e., (100, 500).
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5303
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#3

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Return to “Python Programming”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 0 guests