ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-04-11 18:00:19  阅读:304  来源: 互联网

标签:rand matrix python print np array numpy col row


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

随机取几行

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

标签:rand,matrix,python,print,np,array,numpy,col,row
来源: https://blog.csdn.net/wwwithin/article/details/115582231

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有