天天看點

python筆記: numpy matrix 随機抽取幾行或幾列随機取幾行随機取幾列tips

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