python笔记: numpy matrix 随机抽取几行或几列
python筆記: numpy matrix 隨機(jī)抽取幾行或幾列
- 隨機(jī)取幾行
- 隨機(jī)取幾列
- tips
- 1.生成array
- 2.array的大小
- 3.打亂array的2種類似方法, 矩陣為多行時(shí)默認(rèn)打亂行
- (1) np.random.shuffle(array)
- (2) np.random.permutation(array)
- (3) permutation比shuffle在使用上要多注意一個(gè)小細(xì)節(jié)
隨機(jī)取幾行
python代碼如下
import numpy as nparray = np.arange(15).reshape((3,5))#看心情隨便產(chǎn)生一個(gè)3行5列的matrix print(array)#應(yīng)該長這樣:[[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] 相當(dāng)于行的index 表示有3行np.random.shuffle(row_rand_array)#將3行對應(yīng)的3個(gè)index [0 1 2] 打亂 row_rand = array[row_rand_array[0:2]]#3個(gè)index打亂后取前2個(gè),也就相當(dāng)于matrix行數(shù)打亂后取前2行 print(row_rand)#可能長這樣:[[5 6 7 8 9],[0 1 2 3 4]],因?yàn)殡S機(jī)所以每次都是不一樣的2行 '''[[5 6 7 8 9][0 1 2 3 4]] '''隨機(jī)取幾列
import numpy as nparray = np.arange(15).reshape((3,5))#看心情隨便產(chǎn)生一個(gè)3行5列的matrix print(array)#應(yīng)該長這樣:[[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] 相當(dāng)于列的index 表示有5列np.random.shuffle(col_rand_array)#將5列對應(yīng)的5個(gè)index [0 1 2 3 4] 打亂 col_rand = array[:,col_rand_array[0:2]]#5個(gè)index打亂后取前2個(gè),也就相當(dāng)于matrix列數(shù)打亂后取前2列 print(col_rand)#可能長這樣:[[ 4 2],[ 9 7],[14 12]],因?yàn)殡S機(jī)所以每次都是不一樣的2列 '''[[ 4 2][ 9 7][14 12]] '''tips
1.生成array
a = np.arange(3)#起點(diǎn)默認(rèn)為0,參數(shù)3為終點(diǎn),步長默認(rèn)為1 print(a)#長這樣:[0 1 2]or
b = np.arange(2,8)#參數(shù)2為起點(diǎn),參數(shù)8為終點(diǎn),步長默認(rèn)為1 print(b)#長這樣:[2 3 4 5 6 7]or
c = np.arange(4,5,0.2)#參數(shù)4為起點(diǎn),參數(shù)5為終點(diǎn),步長為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
#一個(gè)好玩的函數(shù)numpy.eye # 有 N 等于 int, 沒 N 等于 float, M=None 等于 int, # k是對角線指數(shù), k = 0 對角線上數(shù)字為1, k = 1 矩陣整體向右平移1個(gè)單位, k = -1 矩陣整體向左平移1個(gè)單位, # 可以自己把一些參數(shù)拿掉試試看, 真的很好玩欸 ~ ~ 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)# 整個(gè)矩陣向左移動一格 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種類似方法, 矩陣為多行時(shí)默認(rèn)打亂行
(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)#默認(rèn)打亂行,每一行的順序不變 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], 因?yàn)槊看蝦un都不一樣b = np.random.permutation([1,2,3,4,5]) print(b)# 可能長這樣:[2 4 5 1 3], 因?yàn)槊看蝦un都不一樣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]], 因?yàn)槊看蝦un都不一樣 '''[[0 1 2][6 7 8][3 4 5]] '''(3) permutation比shuffle在使用上要多注意一個(gè)小細(xì)節(jié)
要給np.random.permutation指定一個(gè)參數(shù)下文才能繼續(xù)使用,不然打亂效果會無效,只是看到這個(gè)現(xiàn)象了,具體為什么我還不知道…覺得麻煩的日常用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) 一樣
以上是我總結(jié)其它篇文章然后自己練習(xí)過一遍的例子, 感謝可愛的原創(chuàng)們!附上鏈接:
python庫numpy——隨機(jī)抽取二維矩陣中多行或多列
https://blog.csdn.net/qq_38261075/article/details/103645209?utm_source=app&app_version=4.5.8
Python隨機(jī)取一個(gè)矩陣數(shù)組的某幾行
https://blog.csdn.net/kane7csdn/article/details/83989882?utm_source=app&app_version=4.5.8
總結(jié)
以上是生活随笔為你收集整理的python笔记: numpy matrix 随机抽取几行或几列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu下使用gcc和makefil
- 下一篇: python wifi模块