How can I make my test harness work like this?

Viewed 48

I'm pretty new to Python and I'm trying to build a test harness:

results = []
results2 = numpy.empty(shape=(0, 2))

for name, model in models:
    kfold = KFold(n_splits=10, random_state=7, shuffle=True)
    cv = cross_val_score(model, X, Y, cv=kfold, scoring='accuracy')
    results.append((name, cv))
    #results2 = numpy.append(results2, [[name, cv]], axis=0)

results.sort(key=lambda result: result[1].mean(), reverse=True)

results3 = numpy.array(results, dtype='object')

print(results3[:, 0])
print(results3[:, 1])

I'm running multiple models and want BOTH a list (sorted by the means()) and a box plot (also sorted by the means).

What I have now works, but I have to convert results into a numpy array to do the slicing to split out the name's and cv's.

I'm was trying to just put [name, cv] directly into a numpy array, but keep getting an error no matter what I try:

Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray

I tried specifying dtype='object' on the numpy.empty, as well as an array of dtypes, but can't seem to get the syntax to work.

If the conversion to a numpy array works at the end, shouldn't it be possible to just insert it directly?

1 Answers

The below removed the error for me, let me know if it works for you, I didn't exactly reproduce your code so not sure:

results2 = np.empty(shape=(0, 2), dtype="object")
models = [("name1","model1"),("name2","model2"),("name3","model3")]
i = 0
for name,model in models:
      cv = [i,i+1,i+2]
      test = np.array([name,cv],dtype="object").reshape((1,2))
      results2 = np.append(results2,test , axis=0)
      i += 1
print(results2)
Related