pandas:sample函数解释
目錄
1、函數定義
2、作用:
3、舉個栗子
4、參數解釋
1、函數定義
DataFrame.sample(self: ~ FrameOrSeries, n=None, frac=None, replace=False, weights=None, random_state=None, axis=None)2、作用:
從所選的數據的指定 axis?上返回隨機抽樣結果,類似于random.sample()函數。
3、舉個栗子
1、首先定義一個數據,結構如下:
import pandas as pd # 定義一組數據 df = pd.DataFrame({'num_legs': [2, 4, 8, 0],'num_wings': [2, 0, 0, 0],'num_specimen_seen': [10, 2, 1, 8]},index=['falcon', 'dog', 'spider', 'fish']) print(df)""" -----------------結果-----------------"""num_legs num_wings num_specimen_seen falcon 2 2 10 dog 4 0 2 spider 8 0 1 fish 0 0 82、從Series?df['num_legs']中隨機提取3個元素。注意我們使用random_state(類似于random庫中隨機種子的作用)確保示例的可復現性。可以看出,結果是在上述數據的“num_legs”項中隨機抽取三個。
extract = df['num_legs'].sample(n=3, random_state=1) print(extract)"""------------運行結果----------""" fish 0 spider 8 falcon 2 Name: num_legs, dtype: int643、replace=True時表示有放回抽樣,設置frac=0.5表示隨機抽取50%的數據,默認對行數據進行操作。栗子如下。
extract2 = df.sample(frac=0.5, replace=True, random_state=1) print(extract2)"""-----------運行結果-----------"""num_legs num_wings num_specimen_seen dog 4 0 2 fish 0 0 84、一個上采樣的栗子。設置?frac=2。注意,當frac>1時必須設置replace=True,默認對行數據進行操作。
extract3 = df.sample(frac=2, replace=True, random_state=1) print(extract3)"""-----------運行結構-----------"""num_legs num_wings num_specimen_seen dog 4 0 2 fish 0 0 8 falcon 2 2 10 falcon 2 2 10 fish 0 0 8 dog 4 0 2 fish 0 0 8 dog 4 0 25、使用數據中的某列的數據值作為權重的栗子。對num_availen_seen列數據進行操作,該列數據中值較大的行更容易被采樣。可以看出,num_availen_seen列中的數據為[10,?2,?1,?8],則[10,?8]兩列更易被抽到。抽樣結果即說明了這一點。
extract4 = df.sample(n=2, weights='num_specimen_seen', random_state=1) print(extract4)"""------------運行結果------------"""num_legs num_wings num_specimen_seen falcon 2 2 10 fish 0 0 84、參數解釋
n?:int, optional
隨機抽樣返回的items個數。當frac?= None時不可用。
Number of items from axis to return. Cannot be used with?frac. Default = 1 if?frac?= None.
frac?:float, optional
要返回的 axis items 數量的小數(比例)表示。不能與n一起使用。
Fraction of axis items to return. Cannot be used with?n.
Note:If?frac?> 1,?replacement?should be set to?True.
replace :bool, default False
是否是有放回取樣。
Allow or disallow sampling of the same row more than once.
weights :str or ndarray-like, optional
默認的“None”將導致相等的概率權重。如果傳遞了一個序列,將與目標對象上的索引對齊。權重中未被采樣對象發現的索引值將被忽略,權重中未被采樣對象的索引值將被賦值為零。如果在DataFrame上調用,將在axis = 0時接受列的名稱。除非權重是一個序列,否則權重必須與被采樣的軸長度相同。如果權重的和不是1,它們將被規范化為和為1。weights列中缺少的值將被視為零。不允許無限值。
Default ‘None’ results in equal probability weighting. If passed a Series, will align with target object on index. Index values in weights not found in sampled object will be ignored and index values in sampled object not in weights will be assigned weights of zero. If called on a DataFrame, will accept the name of a column when axis = 0. Unless weights are a Series, weights must be same length as axis being sampled. If weights do not sum to 1, they will be normalized to sum to 1. Missing values in the weights column will be treated as zero. Infinite values not allowed.
random_state :int or numpy.random.RandomState, optional
用于隨機數生成器(如果是int類型的參數)或numpy RandomState對象的種子。
Seed for the random number generator (if int), or numpy RandomState object.
axis :{0 or ‘index’, 1 or ‘columns’, None}, default None
采樣的軸。可以是axis的編號或名稱。
Axis to sample. Accepts axis number or name. Default is stat axis for given data type (0 for Series and DataFrames).
Returns :Series or DataFrame
與調用數據相同類型的新對象,包含從調用數據對象中隨機取樣的n項。
A new object of same type as caller containing?n?items randomly sampled from the caller object.
參考官網:?https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sample.html
總結
以上是生活随笔為你收集整理的pandas:sample函数解释的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用vite创建单页应用
- 下一篇: Oracle WITH AS 用法