python笔记: numpy matrix 随机抽取几行或几列

  • 随机取几行
  • 随机取几列
  • tips
    • 1.生成array
    • 2.array的大小
    • 3.打乱array的2种类似方法, 矩阵为多行时默认打乱行
      • (1) np.random.shuffle(array)
      • (2) np.random.permutation(array)
      • (3) permutation比shuffle在使用上要多注意一个小细节

随机取几行

python代码如下

import numpy as nparray = np.arange(15).reshape((3,5))#看心情随便产生一个3行5列的matrix
print(array)#应该长这样:[[0  1  2  3  4],[5  6  7  8  9],[10  11  12  13  14]]
'''[[ 0  1  2  3  4][ 5  6  7  8  9][10  11  12  13  14]]
'''
row_rand_array = np.arange(array.shape[0])#shape[0]是有几行,shape[1]是有几列
print(row_rand_array)#[0 1 2] 相当于行的index 表示有3行np.random.shuffle(row_rand_array)#将3行对应的3个index [0 1 2] 打乱
row_rand = array[row_rand_array[0:2]]#3个index打乱后取前2个,也就相当于matrix行数打乱后取前2行
print(row_rand)#可能长这样:[[5  6  7  8  9],[0  1  2  3  4]],因为随机所以每次都是不一样的2行
'''[[5  6  7  8  9][0  1  2  3  4]]
'''

随机取几列

import numpy as nparray = np.arange(15).reshape((3,5))#看心情随便产生一个3行5列的matrix
print(array)#应该长这样:[[0  1  2  3  4],[5  6  7  8  9],[10  11  12  13  14]]
'''[[ 0  1  2  3  4][ 5  6  7  8  9][10  11  12  13  14]]
'''
col_rand_array = np.arange(array.shape[1])#shape[0]是有几行,shape[1]是有几列
print(col_rand_array)#[0 1 2 3 4] 相当于列的index 表示有5列np.random.shuffle(col_rand_array)#将5列对应的5个index [0 1 2 3 4] 打乱
col_rand = array[:,col_rand_array[0:2]]#5个index打乱后取前2个,也就相当于matrix列数打乱后取前2列
print(col_rand)#可能长这样:[[ 4  2],[ 9  7],[14 12]],因为随机所以每次都是不一样的2列
'''[[ 4  2][ 9  7][14 12]]
'''

tips

1.生成array

a = np.arange(3)#起点默认为0,参数3为终点,步长默认为1
print(a)#长这样:[0 1 2]

or

b = np.arange(2,8)#参数2为起点,参数8为终点,步长默认为1
print(b)#长这样:[2 3 4 5 6 7]

or

c = np.arange(4,5,0.2)#参数4为起点,参数5为终点,步长为0.2
print(c)#长这样:[4.  4.2 4.4 4.6 4.8]

or

array=np.array([0,0])
for i in range(5):array = np.vstack((array,[i+1,i+1]))
print(array)#长这样:[[0 0],[1 1],[2 2],[3 3],[4 4],[5 5]]
'''[[0 0][1 1][2 2][3 3][4 4][5 5]]
'''

or

#一个好玩的函数numpy.eye
# 有 N 等于 int, 没 N 等于 float, M=None 等于 int,
# k是对角线指数, k = 0 对角线上数字为1, k = 1 矩阵整体向右平移1个单位, k = -1 矩阵整体向左平移1个单位,
# 可以自己把一些参数拿掉试试看, 真的很好玩欸 ~ ~
numpy.eye(N, M=None, k=0, dtype=<class 'float'>, order='C', *, like=None)
d = np.eye(4)# 4乘4矩阵, 对角线为1.0, 其余位置为0.0
print(d)# [[1. 0. 0. 0.],[0. 1. 0. 0.],[0. 0. 1. 0.],[0. 0. 0. 1.]]
'''[[1. 0. 0. 0.][0. 1. 0. 0.][0. 0. 1. 0.][0. 0. 0. 1.]]
'''e = np.eye(4,dtype=int)
print(e)# [[1 0 0 0],[0 1 0 0],[0 0 1 0],[0 0 0 1]]
'''[[1 0 0 0][0 1 0 0][0 0 1 0][0 0 0 1]]
'''f = np.eye(4,k=-1)# 整个矩阵向左移动一格
print(f)# [[0. 0. 0. 0.],[1. 0. 0. 0.],[0. 1. 0. 0.],[0. 0. 1. 0.]]
'''[[0. 0. 0. 0.][1. 0. 0. 0.][0. 1. 0. 0.][0. 0. 1. 0.]]
'''g = np.eye(4,M=3)#只保留前3列
print(g)# [[1. 0. 0.],[0. 1. 0.],[0. 0. 1.],[0. 0. 0.]]
'''[[1. 0. 0.][0. 1. 0.][0. 0. 1.][0. 0. 0.]]
'''

2.array的大小

a = np.array([0,0,0])
for i in range(5):a = np.vstack((a,[i+1,i+1,i+1]))
print(a)#长这样:[[0 0 0],[1 1 1],[2 2 2],[3 3 3],[4 4 4],[5 5 5]]
'''[[0 0 0][1 1 1][2 2 2][3 3 3][4 4 4][5 5 5]]
'''
print(a.shape)# (6, 3),6行3列
print(a.shape[0])# 有6行
print(a.shape[1])# 有3列

3.打乱array的2种类似方法, 矩阵为多行时默认打乱行

(1) np.random.shuffle(array)

arr = np.arange(15).reshape((3,5))
print(arr)# [[ 0  1  2  3  4],[ 5  6  7  8  9],[10 11 12 13 14]]
'''[[ 0  1  2  3  4][ 5  6  7  8  9][10 11 12 13 14]]
'''np.random.shuffle(arr)#默认打乱行,每一行的顺序不变
print(arr)# [[ 5  6  7  8  9],[ 0  1  2  3  4],[10 11 12 13 14]]
'''[[ 5  6  7  8  9][ 0  1  2  3  4][10 11 12 13 14]]
'''

(2) np.random.permutation(array)

a = np.random.permutation(10)
print(a)# 可能长这样:[5 1 2 6 7 3 8 9 4 0], 因为每次run都不一样b = np.random.permutation([1,2,3,4,5])
print(b)# 可能长这样:[2 4 5 1 3], 因为每次run都不一样c = np.arange(9).reshape((3,3))
print(c)# [[0 1 2],[3 4 5],[6 7 8]]
'''[[0 1 2][3 4 5][6 7 8]]
'''d = np.random.permutation(c)
print(d)# 可能长这样:[[0 1 2],[6 7 8],[3 4 5]], 因为每次run都不一样
'''[[0 1 2][6 7 8][3 4 5]]
'''

(3) permutation比shuffle在使用上要多注意一个小细节

要给np.random.permutation指定一个参数下文才能继续使用,不然打乱效果会无效,只是看到这个现象了,具体为什么我还不知道…觉得麻烦的日常用shuffle就好, 效果是一样的, 回到最开始的例子我们一起看一下效果:

row = np.random.permutation(row_rand_array)
row_rand = array[row[0:2]]
print(row_rand)

效果才会和 np.random.shuffle(row_rand_array) 一样

col = np.random.permutation(col_rand_array)
col_rand = array[:,col[0:2]]
print(col_rand)

效果才会和 np.random.shuffle(col_rand_array) 一样

array = np.arange(15).reshape((3,5))#3行10列
print(array)# [[ 0  1  2  3  4],[ 5  6  7  8  9],[10 11 12 13 14]]
'''[[ 0  1  2  3  4][ 5  6  7  8  9][10 11 12 13 14]]
'''row_rand_array = np.arange(array.shape[0])
print(row_rand_array)# [0 1 2]row = np.random.permutation(row_rand_array)#效果和np.random.shuffle(row_rand_array)一样
row_rand = array[row[0:2]]
print(row_rand)# [[10 11 12 13 14],[ 5  6  7  8  9]]
'''[[10 11 12 13 14][ 5  6  7  8  9]]
'''
col_rand_array = np.arange(array.shape[1])
print(col_rand_array)#[0 1 2 3 4]col = np.random.permutation(col_rand_array)#效果和np.random.shuffle(col_rand_array)一样
col_rand = array[:,col[0:2]]
print(col_rand)# [[ 1  4],[ 6  9],[11 14]]
'''[[ 1  4][ 6  9][11 14]]
'''

以上是我总结其它篇文章然后自己练习过一遍的例子, 感谢可爱的原创们!附上链接:
python库numpy——随机抽取二维矩阵中多行或多列
https://blog.csdn.net/qq_38261075/article/details/103645209?utm_source=app&app_version=4.5.8
Python随机取一个矩阵数组的某几行
https://blog.csdn.net/kane7csdn/article/details/83989882?utm_source=app&app_version=4.5.8

python笔记: numpy matrix 随机抽取几行或几列相关推荐

  1. python numpy 子数组_详解:Python 取numpy数组的某几行某几列方法(含对与错示例)...

    前言: 今天为大家带来的内容是Python 取numpy数组的某几行某几列方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,要是喜欢的话记得点赞转发收藏不迷路哦!!! ...

  2. Python使用numpy函数vsplit垂直(行角度)拆分numpy数组(返回拆分后的numpy数组列表)实战:垂直拆分二维numpy数组、split函数垂直拆分二维numpy数组

    Python使用numpy函数vsplit垂直(行角度)拆分numpy数组(返回拆分后的numpy数组列表)实战:垂直拆分二维numpy数组.split函数垂直拆分二维numpy数组 目录

  3. pandas随机抽取1000行

    https://blog.csdn.net/zhengxu25689/article/details/87347700/ data = data.sample(n=1000,random_state= ...

  4. python随机抽号_随手小代码——Python 从集合中随机抽取元素

    =================================版权声明================================= 版权声明:原创文章 谢绝转载 请通过右侧公告中的" ...

  5. 【python】二维数组按照某行或某列排序(numpy lexsort)

    这里介绍的排序方法主要是针对最后一行和最后一列,如果要指定某一行目前没有很好的方法,有个笨办法可以先把排序的该列(或行)换到最后,排序后再换回来 曾经以为排序只能使用list的sort函数,后来发现a ...

  6. python 有放回随机抽取_Python面试宝典之基础篇-08

    原标题:Python面试宝典之基础篇-08 题目36:如何使用random模块生成随机数.实现随机乱序和随机抽样? 点评:送人头的题目,因为Python标准库中的常用模块应该是Python开发者都比较 ...

  7. python 读取txt文件中满足要求的行写到新文件中_python_文本文件里面随机抽取若干行,写入新的文本文件里面...

    原博文 2012-07-25 16:36 − 天一直阴沉沉的,真想早点下班啊...... 1 #encoding=utf-8 2 import random 3 from sets import Se ...

  8. python 有放回随机抽取_Python 随机抽样

    # -*- coding: utf-8 -*- import numpy import pandas data = pandas.read_csv( 'D:\\PDA\\4.9\\data.csv' ...

  9. Python笔记 | 依概率随机产生姓氏案例

    生活中,不同的姓氏出现的频率大不相同.如赵.王.李等姓出现频率很高,而像东方.慕容这样的复姓却很少见到,今天我们就来在python中简单实现一下上述过程. 要求:姓氏全都按行保存在CNames中,越靠 ...

最新文章

  1. thinkphp 整合 swiftmailer 实现邮件发送
  2. 龙果支付 mysql_龙果支付系统搭建与部署
  3. R语言-时间序列-arima模型-forecast、tseries包
  4. tf.control_dependencies()函数用法
  5. 2022.3.14矮牵牛
  6. linux定向查日志_linux日志查找技巧
  7. 【Jetson-Nano】2.Tensorflow object API和Pytorch的安装
  8. 使用ASP.NET MVC对用户组的自定义授权
  9. 2021年双十一大复盘:众人唱衰双十一,我们却发现了这些机会
  10. asp.net性能优化之使用Redis缓存(入门)
  11. 【OpenCV】图像线性混合
  12. 计算机磁盘无法创建文件夹,无法创建文件,小编告诉你无法新建文件夹怎么办...
  13. UE4保存信息到本地
  14. cmd命令查询电脑序列号_如何在Windows10中查找计算机序列号/主板型号
  15. matlab怎么表示二元函数,如何用Matlab画二元函数?
  16. 通过nginx搭建一个基于http-flv的直播流媒体服务器
  17. 独家 | 手把手教随机森林
  18. java计算机毕业设计河南省农村多元化养老服务管理系统设计与实现源码+mysql数据库+系统+lw文档+部署
  19. 这些成人世界的“黑话”,你能听懂多少?
  20. php汉字全是问号_PHP-PHP中文显示问号问题

热门文章

  1. C语言中的指针,指针存在的意义
  2. 概率——2016青岛icpc区域赛 C - Pocky HDU - 5984 公式推导
  3. 世界上最神奇的24堂课-----第一课 内心世界,新的力量
  4. 八人抢答器讲解_8人抢答器的制作原理和过程说明是怎样的?
  5. 《深度学习导论及案例分析》一导读
  6. MTK平台的LCM防静电(esd-check)机制
  7. 计算机组成原理乘法器实验报告,Booth乘法器实验报告
  8. Backup Exec Inventory 与Catalog的含义(转载)
  9. 【香蕉oi】Game(SAM,SG函数,假题)
  10. 一款简单好用的数字温度传感器芯片介绍