* Gets n number of random rows in a dataframe. Sample is reproducible if seed is provided. * @param num The number of rows to return. Default to 5. * @param options.seed An integer specifying the random seed that will be used to create the distribution. * @example * ``` * co
(num = 5, options?: { seed?: number })
| 576 | * ``` |
| 577 | */ |
| 578 | async sample(num = 5, options?: { seed?: number }): Promise<DataFrame> { |
| 579 | const { seed } = { seed: 1, ...options } |
| 580 | |
| 581 | if (num > this.shape[0]) { |
| 582 | throw new Error("ParamError: Sample size cannot be bigger than number of rows"); |
| 583 | } |
| 584 | if (num <= 0) { |
| 585 | throw new Error("ParamError: Sample size cannot be less than 1"); |
| 586 | } |
| 587 | |
| 588 | const shuffledIndex = await tensorflow.data.array(this.index).shuffle(num, `${seed}`).take(num).toArray(); |
| 589 | const df = this.iloc({ rows: shuffledIndex }); |
| 590 | return df; |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * Return Addition of DataFrame and other, element-wise (binary operator add). |