I want to randomly extract data from the list and create a new list of data.

Asked 2 months ago, Updated 2 months ago, 4 views

I have a matrix list of 100x3 as shown below.

Enter a description of the image here

There are two things I would like to do for this matrix:

·I would like to randomly extract two lines at any number of times (e.g., 3 times).
ex)[[a1, a2, a3],
] [b1, b2, b3]
·I want to change the extracted two rows to become one row and create a matrix of the number of attempts x the set of rows taken out.
[[a1, a2, a3, b1, b2, b3],
[c1, c2, c3, d1, d2, d3],
[e1, e2, e2, f1, f2, f3]

The first 100x3 matrix treats it as a block of values for one line, or three values.

python numpy

2022-09-30 11:17

2 Answers

First of all, the appropriate matrix

 a=np.array ([m*3+n for n in range(3)] for min range(20)])

When there was

b=a [np.random.choice(a.shape[0], 8, replace=False), :]

Yes, a.shape[0] takes the row size of a and creates an index random number with np.random.choice where replace=False is the option to have no duplication.You can now extract the line in the form a[n,:].

2022-09-30 11:17

I wrote it sequentially with reference to the person who answered.
If you change X and Y, I think you can create them with some free values.

import numpy as np
import random

a=np.array([m*3+n for n in range(3)] for min range(100)])

# determine an arbitrary number of times
X = 10
# determine the number of rows to extract
Y = 2

for i in range (X):
    an_list = np.array([])
    for_in range(Y):
        an_list=np.append(ans_list, a[random.randrange(100)])


2022-09-30 11:17

If you have any answers or tips

© 2022 OneMinuteCode. All rights reserved.