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

yipeiwu_com5年前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设计】。

相关文章

妙用itchat! python实现久坐提醒功能

本文实例为大家分享了python久坐提醒的具体实现代码,供大家参考,具体内容如下 #!/usr/bin/envy python3 #-*- coding:utf-8 -*- impo...

Python对List中的元素排序的方法

首先定义一个compare函数: def compare(sf1, sf2): if (sf1.value > sf2.value): return -1; e...

对pandas读取中文unicode的csv和添加行标题的方法详解

pandas这个库就是这么智能。有了dateframe格式一切都好办了。相比csv库对中文支持就渣了。 reader = pd.read_csv(leg2CsvReadFile, del...

Python3匿名函数用法示例

本文实例讲述了Python3匿名函数用法。分享给大家供大家参考,具体如下: # -*- coding:utf-8 -*- #!python3 # 匿名函数 # 1.不用给函数取名 #...

Python编程之微信推送模板消息功能示例

本文实例讲述了Python微信推送模板消息功能。分享给大家供大家参考,具体如下: 官方文档:https://mp.weixin.qq.com/wiki?t=resource/res_ma...