随机取几行

python代码如下

import numpy as np

array = 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 np

array = 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

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐