Skip to content

Commit 2fc8e5f

Browse files
committed
Update sample function in danfojs-node
1 parent b20ff15 commit 2fc8e5f

4 files changed

Lines changed: 849 additions & 850 deletions

File tree

danfojs-node/.eslintrc.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
{ "before": true
3232
}
3333
],
34-
"array-bracket-spacing": [ "error", "always"
35-
],
3634
"space-infix-ops": "error",
3735
"object-curly-spacing": [ "error", "always"
3836
],

danfojs-node/dist/core/frame.js

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -240,32 +240,21 @@ class DataFrame extends _generic.default {
240240
}
241241
}
242242

243-
sample(num = 5) {
244-
if (num > this.values.length || num < 1) {
245-
let config = {
246-
columns: this.column_names
247-
};
248-
return new DataFrame(this.values, config);
249-
} else {
250-
let values = this.values;
251-
let idx = this.index;
252-
let new_values = [];
253-
let new_idx = [];
254-
let counts = [...Array(idx.length).keys()];
255-
256-
let rand_nums = utils.__sample_from_iter(counts, num, false);
243+
async sample(num = -1, seed = 1) {
244+
if (num > this.shape[0]) {
245+
throw new Error("Sample size n cannot be bigger than size of dataset");
246+
}
257247

258-
rand_nums.map(i => {
259-
new_values.push(values[i]);
260-
new_idx.push(idx[i]);
261-
});
262-
let config = {
263-
columns: this.column_names,
264-
index: new_idx
265-
};
266-
let df = new DataFrame(new_values, config);
267-
return df;
248+
if (num < -1 || num == 0) {
249+
throw new Error("Sample size cannot be less than -1 or 0");
268250
}
251+
252+
num = num === -1 ? this.shape[0] : num;
253+
const shuffled_index = await tf.data.array(this.index).shuffle(num, seed).take(num).toArray();
254+
const df = this.iloc({
255+
rows: shuffled_index
256+
});
257+
return df;
269258
}
270259

271260
add(other, axis) {

0 commit comments

Comments
 (0)