- import numpy as np
- #Create a 10x10 array:
- a = np.arange(100).reshape(10, 10)
- In [1]: a
- Out[1]:
- array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
- [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
- [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
- [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
- [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
- [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
- [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
- [70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
- [80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
- [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
- #Even-indexed elements along all columns
- a[::2,:]
- #Even-indexed elements along column 1
- a[::2,0]
- #All columns except the first
- a[::,1:]
- #First column:
- a[::,0] #Also a[::1,0]
- #Last column
- a[::, -1]
- #All columns except the first and the last
- a[::, 1:-1]
- #All columns except the last
- a[::, :-1]
- #Firts two columns
- a[::,0:2]
- #Last two columns
- a[::,-2:]
- #Last column but one
- a[::,-2]
- #All except last two columns
- a[::,:-2]
- #or
- a[::,:-2:]
- #Odd-indexed elements along the column 0
- a[1::2,0]
- #Odd-indexed elements along all columns
- a[1::2,:]
- #First row:
- a[0] # Also a[0,::]
- #Second row
- a[1,::] # Also a[1]
- #Last row
- a[-1,::] #Also a[-1], a[-1:,::]
- #Last two rows
- a[-2:,::]
- #All rows except last
- a[:-1,::]
- #All rows except the first
- a[1:,::]
- #All between the first and last rows
- a[1:-1,::]
- #First element of the array
- a[0,0]
- #or
- a[0][0] #Element in the first row and first column
- #Last element of the array
- a[-1,-1] #Also a[-1][-1]
- #Try
- a[::2]
- #Change the order or rows, from last to first
- a[::-1]
- #Reverse all columns
- a[::-1,:]
- #Reverse elements in the first column
- a[::-1,0]
- #Reverse elements in the first row
- a[0,::-1]
- #Reverse all rows
- a[:,::-1]
- #Sum chunks of two elements along column 0
- a[::2,0] + a[1::2,0]
- #sum chunks of two elements along all columns
- a[::2,:] + a[1::2,:]
- #Sum chunks of two elements along all rows
- a[:,::2] + a[:,1::2]
- #Sum chunks of two elements along the first row
- a[0,::2] + a[0,1::2]
- #Try to sum chunks of five elements along columns
- #One way to do this is as follows:
- In [2]: r = a[::,0] #We want to use the column length
- 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
- In [3]: b #Return b as array of sums
- Out[3]:
- [array([100, 105, 110, 115, 120, 125, 130, 135, 140, 145]),
- array([350, 355, 360, 365, 370, 375, 380, 385, 390, 395])]
- #For the first row (choose any row):
- In [4]: c = a[::,0]
- In [5]: b = [sum(c[values: values+5]) for values in xrange(0, len(c), 5)]
- In [6]: b
- Out[6]: [100, 350]
- #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:
- In [7]: np.add.reduceat(a, [0,5])
- Out[8]:
- array([[100, 105, 110, 115, 120, 125, 130, 135, 140, 145],
- [350, 355, 360, 365, 370, 375, 380, 385, 390, 395]])
- #And for the first row:
- In [9]: c = a[::,0]
- In [10]: np.add.reduceat(c, [0,5])
- Out[10]: array([100, 350])
- #You can include 5 as step:
- In [11]: b = np.arange(100)
- In [12]: np.add.reduceat(b, range(0, 100, 5))
- Out[12]:
- array([ 10, 35, 60, 85, 110, 135, 160, 185, 210, 235, 260, 285, 310,
- 335, 360, 385, 410, 435, 460, 485])
-
- Active Topics
-
-
- by Eli 21 hours ago Re: What is in Your Mind? View the latest post Replies 703 Views 299983
- by Eli 21 hours ago Russia Invades Ukraine View the latest post Replies 663 Views 235162
- by Eli 2 days ago All in One: YouTube, TED, X, Facebook and Instagram Reels, Videos, Images and Text Posts View the latest post Replies 326 Views 34175
- by Eli 3 days ago PySpark for Large Data Processing View the latest post Replies 1 Views 6866
- by Eli 1 week ago President Museveni's Speech During International Development Association (IDA) Summit View the latest post Replies 1 Views 280
- by Eli 1 week ago From Simple Linear Regression Analysis to Covariance & Correlation to Independent Determinant, and R-Squared View the latest post Replies 11 Views 24839
- by Eli 1 week ago Collection of Greatest Christian Hymns of all Times View the latest post Replies 34 Views 67362
- by Eli 1 week ago Pondering Big Cosmology Questions Through Lectures and Dialogues View the latest post Replies 34 Views 58081
- by Eli 1 week ago Programmatically Manipulate Files: Renaming, Reading, Writing, Deleting, and Moving Files Between Folders View the latest post Replies 7 Views 15758
- by Eli 2 weeks ago Iran Launches Retaliatory Attack Against Israel, and Israel Retaliates by Attacking Iranian Isfahan Millitary Base View the latest post Replies 28 Views 17785
-
Slicing Arrays in Python
- Eli
- Senior Expert Member
- Reactions: 183
- Posts: 5382
- Joined: 9 years ago
- Location: Tanzania
- Has thanked: 75 times
- Been thanked: 88 times
- Contact:
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.
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
- Eli
- Senior Expert Member
- Reactions: 183
- Posts: 5382
- Joined: 9 years ago
- Location: Tanzania
- Has thanked: 75 times
- Been thanked: 88 times
- Contact:
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:
Our new dataset is reduced to two dimensions, i.e., (100, 500).
- import numpy as np
- data = np.zeros((10, 100, 500))
- new_data = data[0, :, :] # Drop the first dimension
- print(new_data.shape)
Our new dataset is reduced to two dimensions, i.e., (100, 500).
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
-
- Similar Topics
- Replies
- Views
- Last post
-
- Information
-
Who is online
Users browsing this forum: No registered users and 2 guests