擅长:python、mysql、java
<p>如果要随机选择行,可以使用标准Python库中的<code>random.sample</code>:</p>
<pre><code>import random
population = range(4601) # Your number of rows
choice = random.sample(population, k) # k being the number of samples you require
</code></pre>
<p>无需替换的<code>random.sample</code>示例,因此不必担心重复的行会在<code>choice</code>中结束。给定一个名为<code>matrix</code>的numpy数组,您可以通过切片来选择行,如下所示:<code>matrix[choice]</code>。</p>
<p>当然,<code>k</code>可以等于总体中的总元素数,然后<code>choice</code>将包含行索引的随机顺序。然后你可以根据自己的需要对<code>choice</code>进行分区。</p>