# carving 2d numpy array by index

• Last Update :
• Techknowledgy :
```In[1]: import numpy as np

In[2]: k = 2

In[3]: i, j = np.ogrid[0: 5, 0: 5]

In[4]: mask = (j - i - k < 0)

In[5]: mask
Out[5]:
array([
[True, True, False, False, False],
[True, True, True, False, False],
[True, True, True, True, False],
[True, True, True, True, True],
[True, True, True, True, True]
], dtype = bool)

In[6]: mask.shape
Out[6]: (5, 5)

In[7]: mask.dtype
Out[7]: dtype('bool')```
```xdim, ydim = data.shape
k = 2
a, b = np.meshgrid(range(ydim), range(xdim))
mask = (b - a - k) < 0

new_data = data[mask]

new_data2 = np.array(data) # to force a copy
new_data2[~mask] = 0```

Here's yet another way using `np.indices`:

```>>>
import numpy as np
>>>
a = np.arange(90).reshape(10, 9) >>>
b = np.indices(a.shape) >>>
k = 2 >>>
i = b[1] - b[0] - k >>>
a[i < 0]
array([0, 1, 9, 10, 11, 18, 19, 20, 21, 27, 28, 29, 30, 31, 36, 37, 38,
39, 40, 41, 45, 46, 47, 48, 49, 50, 51, 54, 55, 56, 57, 58, 59, 60,
61, 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
])```

Suggestion : 2

The simplest case of indexing with N integers returns an array scalar representing the corresponding item. As in Python, all indices are zero-based: for the i-th index \(n_i\), the valid range is \(0 \le n_i < d_i\) where \(d_i\) is the i-th element of the shape of the array. Negative indices are interpreted as counting from the end of the array (i.e., if \(n_i < 0\), it means \(n_i + d_i\)).,Integer array indexing allows selection of arbitrary items in the array based on their N-dimensional index. Each integer array represents a number of indices into that dimension.,Indexing with multidimensional index arrays tend to be more unusual uses, but they are permitted, and they are useful for some problems. We’ll start with the simplest multidimensional case:,If a zero-dimensional array is present in the index and it is a full integer index the result will be a scalar and not a zero-dimensional array. (Advanced indexing is not triggered.)

```>>> x = np.arange(10) >>>
x[2]
2
>>>
x[-2]
8```
```>>> x.shape = (2, 5) # now x is 2 - dimensional >>>
x[1, 3]
8
>>>
x[1, -1]
9```
```>>> x[0]
array([0, 1, 2, 3, 4])```
```>>> x[0][2]
2```
```>>> x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>>
x[1: 7: 2]
array([1, 3, 5])```
```>>> x[-2: 10]
array([8, 9]) >>>
x[-3: 3: -1]
array([7, 6, 5, 4])```

Suggestion : 3

For two-dimensional numpy arrays, you need to specify both a row index and a column index for the element (or range of elements) that you want to access.,To slice elements from two-dimensional arrays, you need to specify both a row index and a column index as [row_index, column_index].,For one-dimensional numpy arrays, you only need to specify one index value, which is the position of the element in the numpy array (e.g. arrayname[index]).,On this page, you will use indexing to select elements within one-dimensional and two-dimensional numpy arrays, a selection process referred to as slicing.

`avg_monthly_precip = numpy.array([0.70, 0.75, 1.85])`
```precip_2002_2013 = numpy.array([
[1.07, 0.44, 1.5],
[0.27, 1.13, 1.72]
])```
```# Import necessary packages
import os

import numpy as np
import earthpy as et```
```# Download.txt with avg monthly precip(inches)
monthly_precip_url = 'https://ndownloader.figshare.com/files/12565616'
et.data.get_data(url = monthly_precip_url)

# Download.csv of precip data
for 2002 and 2013(inches)
precip_2002_2013_url = 'https://ndownloader.figshare.com/files/12707792'
et.data.get_data(url = precip_2002_2013_url)```
`'/root/earth-analytics/data/earthpy-downloads/monthly-precip-2002-2013.csv'`
```# Set working directory to earth - analytics
os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))```