0

I'm trying to perform pairs bootstrap and I have some problems when indexing the bootstrap samples. I will show a brief piece of code that matches the problem I am facing. How should I index the sample to get the bootstrap sample?

This is the data:

Y = [1,4,2,5,3,2,4,6]
X = [1,2,3,4,3,1,5,2]

First of all I create the index:

ind = np.arange(len(X))

Secondly I perform bootstrap on the index:

ind_b = np.random.choice(ind, len(ind))

And finally I try to filter both variables with the bootstrap index:

Y_b = Y[ind_b] 
X_b = X[ind_b]

Doing this I get an error message:

TypeError: only integer scalar arrays can be converted to a scalar index

Could someone explain how I can do it correctly?

2
  • 2
    Y and X are only a Python list, they do not know how to index based off np.array. You can instead change Y = np.array(Y) and then you can index like that Commented Apr 3, 2019 at 20:52
  • Thank you very much. It works perfectly. Commented Apr 4, 2019 at 21:05

1 Answer 1

1

The problem is that X and Y are only Python list's in this case. If you were to do type(Y) you would get list. Since you are indexing into a list with np.array, Python has no idea what that is and throws the error because you are doing invalid operations on a list. Instead, you need X and Y to be type np.array as well

>>> ind_b
array([6, 2, 7, 4, 0, 5, 7, 0])
>>> np.array(Y)[ind_b]
array([4, 2, 6, 3, 1, 2, 6, 1])
>>> Y = np.array(Y)
>>> X = np.array(X)
>>> Y_b = Y[ind_b]
>>> Y_b
array([4, 2, 6, 3, 1, 2, 6, 1])
>>> X_b = X[ind_b]
>>> X_b
array([5, 3, 2, 3, 1, 1, 2, 1])

A quick fix is to change it so the assignments are:

Y = np.array([1,4,2,5,3,2,4,6])
X = np.array([1,2,3,4,3,1,5,2])
Sign up to request clarification or add additional context in comments.

2 Comments

Great oberservation. +1
Thank you for your time and help. It works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.