日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python numpy 行 列个数_Python 用 numpy 随机抽样选择矩阵的多行或多列

發布時間:2024/3/12 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python numpy 行 列个数_Python 用 numpy 随机抽样选择矩阵的多行或多列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

numpy提供了多種隨機選取的方法,這里放三種方法,效果相同。

以從一個矩陣中隨機抽取n行為例,寫了三個函數,粗鄙代碼,見笑

import numpy as np

#=================================================================================

def rand_row1(array,dim_needed): #array為需要采樣的矩陣,dim_needed為想要抽取的行數

row_total = array.shape[0]

row_sequence = np.arange(row_total)

np.random.shuffle(row_sequence)

return array[row_sequence[0:dim_needed],:]

#=================================================================================

def rand_row2(array,dim_needed): #array為需要采樣的矩陣,dim_needed為想要抽取的行數

row_total = array.shape[0]

row_sequence = np.random.permutation(row_total)

return array[row_sequence[0:dim_needed],:]

#=================================================================================

def rand_row3(array,dim_needed): #array為需要采樣的矩陣,dim_needed為想要抽取的行數

row_total = array.shape[0]

row_sequence= np.random.choice(row_total,dim_needed,replace=False, p=None)

return array[row_sequence,:]

#=================================================================================

#=============================測試

test_data = np.array([0, 0])

for i in range(100):

test_data = np.vstack((test_data, [i+1, i+1]))

# print(test_data)

batch = rand_row1(test_data,20)

# batch = rand_row2(test_data,20)

# batch = rand_row3(test_data,20)

print(batch.shape,batch)

隨機抽取n列與之類似,同志,共勉!

總結

以上是生活随笔為你收集整理的python numpy 行 列个数_Python 用 numpy 随机抽样选择矩阵的多行或多列的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。