随机抽取 2 列样本

Randomly draw a sample for 2 columns

Python 中一个众所周知的函数是 random.sample()

但是,我的数据集由多列组成,我需要对 'lat' 和 'lng' 坐标进行采样。由于这两个是相关的,我不能单独使用 random.sample() 来获得一些随机的纬度坐标+一些不对应的经度坐标。

最优雅的解决方案是什么?

也许首先制作第三列,我在其中结合经纬度 然后样品 然后取消合并?

如果是这样,我应该怎么做,lat 和 lng 值都是具有不同长度的浮点数这一事实并没有使它变得更容易。大概中间加个'-'吧?

本质上,您是在谈论对具有值 [lat_i, lng_i] 的整个 进行采样。这导致了一个非常简单(但可能过于冗长)的解决方案:

random_row_index = random.randint(0, number_of_rows_in_dataset - 1)
random_row = dataset[randon_row_index, :]

如果您有 Pandas 数据框,只需使用 DataFrame.sample

这就是 train_test_split 的用途:https://realpython.com/train-test-split-python-data/

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y)