欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺析pandas隨機(jī)排列與隨機(jī)抽樣

 更新時(shí)間:2021年01月22日 11:46:34   作者:元小瘋  
這篇文章主要介紹了pandas隨機(jī)排列與隨機(jī)抽樣的相關(guān)資料,幫助大家更好的利用pandas進(jìn)行數(shù)據(jù)分析,感興趣的朋友可以了解下

隨機(jī)排列

利用 numpy.random.permutation() 函數(shù),可以返回一個(gè)序列的隨機(jī)排列。將此隨機(jī)排列作為 take() 函數(shù)的參數(shù),通過應(yīng)用 take() 函數(shù)就可實(shí)現(xiàn)按此隨機(jī)排列來調(diào)整 Series 對(duì)象或 DataFrame 對(duì)象各行的順序。
其示例代碼 example1.py 如下:

import numpy as np
import pandas as pd
#創(chuàng)建DataFrame
df = pd.DataFrame(np.arange(12).reshape(4,3))
print(df)
 0 1 2
0 0 1 2
1 3 4 5
2 6 7 8
3 9 10 11

#創(chuàng)建隨機(jī)排列
order = np.random.permutation(4)
#通過隨機(jī)排列調(diào)整DataFrame各行順序
newDf = df.take(order)
print(newDf)
 0 1 2
2 6 7 8
3 9 10 11
0 0 1 2
1 3 4 5

隨機(jī)抽樣

隨機(jī)抽樣是指隨機(jī)從數(shù)據(jù)中按照一定的行數(shù)或者比例抽取數(shù)據(jù)。隨機(jī)抽樣的函數(shù)如下:

numpy.random.randint(start,end,size)

函數(shù)中的參數(shù)說明如下:

  • start:隨機(jī)數(shù)的開始值;
  • end:隨機(jī)數(shù)的終止值;
  • size:抽樣個(gè)數(shù)。

通過 numpy.random.randint() 函數(shù)產(chǎn)生隨機(jī)抽樣的數(shù)據(jù),通過應(yīng)用 take() 函數(shù)就可實(shí)現(xiàn)隨機(jī)抽取 Series 對(duì)象或 DataFrame 對(duì)象中的數(shù)據(jù)。其示例代碼 example2.py 如下

import numpy as np
import pandas as pd
#創(chuàng)建DataFrame
df = pd.DataFrame(np.arange(12).reshape(4,3))
print(df)
0 1 2
0 0 1 2
1 3 4 5
2 6 7 8
3 9 10 11

#隨機(jī)抽樣
order = np.random.randint(0,len(df),size=3)
#通過隨機(jī)抽樣抽取DataFrame中的行
newDf = df.take(order)
print(newDf)
0 1 2
0 0 1 2
1 3 4 5
1 3 4 5

以上就是詳解pandas隨機(jī)排列與隨機(jī)抽樣的詳細(xì)內(nèi)容,更多關(guān)于pandas隨機(jī)排列與隨機(jī)抽樣的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論