Python实现随机取一个矩阵数组的某几行

yipeiwu_com6年前Python基础

废话不多说了,直接上代码吧!

import numpy as np
 
array = np.array([0, 0])
for i in range(10):
  array = np.vstack((array, [i+1, i+1]))
print(array)
# [[ 0 0]
# [ 1 1]
# [ 2 2]
# [ 3 3]
# [ 4 4]
# [ 5 5]
# [ 6 6]
# [ 7 7]
# [ 8 8]
# [ 9 9]
# [10 10]]
 
rand_arr = np.arange(array.shape[0])
 
np.random.shuffle(rand_arr)
print(array[rand_arr[0:5]])
# [[9 9]
# [4 4]
# [1 1]
# [5 5]
# [8 8]]
np.random.shuffle(rand_arr)
print(array[rand_arr[0:5]])
# [[10 10]
# [ 3 3]
# [ 4 4]
# [ 8 8]
# [ 5 5]]

以上这篇Python实现随机取一个矩阵数组的某几行就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python字符类型的一些方法小结

int 数字类型 class int(object): """ int(x=0) -> int or long int(x, base=10) -> int...

Python 3.8新特征之asyncio REPL

前言 我最近都在写一些Python 3.8的新功能介绍的文章,在自己的项目中也在提前体验新的Python版本。为什么我对这个Python 3.8这么有兴趣呢?主要是因为在Python 2...

Python中Continue语句的用法的举例详解

Python中Continue语句的用法的举例详解

 Python continue语句返回while循环的开始。Continue语句拒绝在该循环的当前迭代中的其余语句执行并移动控制返回到循环的顶部(开始位置)。 continu...

pandas把所有大于0的数设置为1的方法

如下所示: df = pd.read_csv(‘hahaha.csv') df[df>0] = 1 print(df) 以上这篇pandas把所有大于0的数设置为1的方法...

python使用zip将list转为json的方法

zip()函数将可迭代对象作为参数,并打包成元组,返回的是一个个zip对象,可以使用list或dict转换返回结果,使用*zip可以将打包的对象分解成列表 >>>...