简体   繁体   中英

How can I sample every other value in a grid while ensuring it alternates each row to create an offset?

I would like to take a grid of evenly spaced points and sample every other value while ensuring that each row is offset from the one before. I have been able to make this fairly easily when the number of x points in the grid is odd, but not when they are even.

As an Example the original grid looks like:

import pandas as pd
import numpy as np

x = np.array(range(1, 6))
y = np.array(range(1, 6))
df = pd.DataFrame(np.array(np.meshgrid(x, y, )).T.reshape(-1, 2), columns= {'x', 'y'})
df.plot.scatter('x', 'y')

在此处输入图像描述

I have used

df_2 = df[::2]
df_2.plot.scatter('x', 'y')

在此处输入图像描述

But I cannot figure out how to make it work when the number of x values is even.

For background, I am new to python (coming from R) and I am trying to sample geospatial data at even longitudinal intervals and offset for every change in latitude for even spatial coverage. It is hard to guarantee that each point grid will start with an odd number of x values as they are already random samples of spatial data.

We can create a new map/boolean column to say whether the point should be included in the plot. The pattern for filling that column is adding x and y values for a point and taking the result's modulus 2 and comparing that to 0 .

Then when we plot we restrict the DF to the new map/boolean column.

import pandas as pd
import numpy as np

x = np.array(range(1, 6))
y = np.array(range(1, 6))
df = pd.DataFrame(np.array(np.meshgrid(x, y, )).T.reshape(-1, 2), columns= {'x', 'y'})    
df['includepoint'] = (df.y  + df.x) % 2 == 0   
df[df.includepoint].plot.scatter('x', 'y')

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM