0

A well known function for this in Python is random.sample()

However, my dataset consist of multiple columns, and i need the 'lat' and 'lng' coordinates to be sampled. As these two are related, i cannot use the random.sample() separately to get some random lat coordinates + some non corresponding lng coordinates.

What would be the most elegant solution for this?

Perhaps first making a third column, in which i combine lat&lng Then sample Then unmerge?

If so, how should i do this, the fact that both lat and lng values are floats with different lengts doesn't make it easier. Probably by adding a'-' in between?

Cornelis
  • 445
  • 3
  • 11
  • 27

2 Answers2

1

Essentially, you're talking about sampling an entire row which has values [lat_i, lng_i]. This leads to a very simple (but perhaps too verbose) solution:

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

If you have a Pandas dataframe, simply use DataFrame.sample.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • I do use Pandas, df.sample seems to work perfectly! Thanks! What is the difference with "random.sample()" though? isn't the df.sample() random? – Cornelis May 27 '22 at 15:42
  • 1
    @Cornelis, `df.sample` is Pandas-specific. You can sample your dataframe in less than 20 characters of code, while with `random.sample` you'd have to fiddle with a random index, like I show in my answer, which could look unnecessarily complicated. But indeed, `random.sample`, `numpy.random.sample` and `df.sample` essentially accomplish the same task, so they're similar. – ForceBru May 27 '22 at 15:51
0

That is what train_test_split is made for: 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)
Nohman
  • 444
  • 2
  • 9