Hi, I'm trying to run the following code
import nimpy
let randrange = pyImport("random.randrange")
proc cross_validation_split(dataset:seq[float], n_folds:int): seq[float]=
var dataset_split : seq[float]
var dataset_copy = dataset
var fold_size = int(dataset.len/n_folds)
for i in 0..n_folds:
var fold : seq[float]
while fold.len < fold_size:
var index = randrange(dataset_copy.len)
fold.append(dataset_copy.pop(index))
dataset_split.append(fold)
return dataset_split
As you can see I call randrange by importing it from the random module of python. however it gives me an error in calling the randrange routine saying it found randrange of kind let?
to solve the error you are asking for you can try this but it'll still have other errors after that
import nimpy
let rnd = pyImport("random")
proc cross_validation_split(dataset:seq[float], n_folds:int): seq[float]=
var dataset_split : seq[float]
var dataset_copy = dataset
var fold_size = int(dataset.len/n_folds)
for i in 0..n_folds:
var fold : seq[float]
while fold.len < fold_size:
var index = rnd.randrange(dataset_copy.len)
fold.append(dataset_copy.pop(index))
dataset_split.append(fold)
return dataset_split
doing this however should get you the result you are looking for
import random
proc cross_validation_split(dataset:seq[float], n_folds:int): seq[float]=
var dataset_split : seq[float]
var dataset_copy = dataset
var fold_size = int(dataset.len/n_folds)
for i in 0..n_folds:
var fold : seq[float]
while fold.len < fold_size:
var index = rand(dataset_copy.len)
fold.add dataset_copy[index]
dataset_copy.delete(index)
dataset_split.add(fold)
return dataset_split
File "<frozen importlib._bootstrap>", line 991, in _find_and_load File "<frozen importlib._bootstrap>", line 971, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 914, in _find_spec
IndexError: list index out of range
How to solve this problem,Please