0

I am very new to Python (previous Matlab user).

I have the array

y_pred =  [None] * 128

test_idx is an array of indeces

array([  3,   4,   5,  19,  28,  30,  38,  39,  47,  49,  50,  51,  54,
        64,  74,  81,  84,  85,  90,  91,  93,  97, 102, 103, 106, 107,
       109, 111, 115, 121], dtype=int64)

I would like to replace the values of y_pred corresponding to the test_idx with the array results

array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
       0, 0, 0, 0, 0, 0, 0])

if I try

y_pred[test_idx] = results

I get the error: TypeError: only integer arrays with one element can be converted to an index

4
  • 2
    y_pred is not an array, but a list, which can't be indexed like that. Perhaps use an array instead? Commented Nov 10, 2016 at 9:27
  • 2
    Also, you should really use the numpy tag is you are going to ask a question about numpy arrays. Commented Nov 10, 2016 at 9:37
  • Do you really want y_pred`` to be a list of None`s? Not array of zeros? What's the Matlab equivalent? Commented Nov 10, 2016 at 13:36
  • Show exactly what you expect. There's too much ambiguity in the current example. Commented Nov 10, 2016 at 13:59

4 Answers 4

1

Your y_pred is a native Python list and you cannot index it by anything other than integers. You should create it as a numpy array.

Try to initialize it the way below:

import numpy as np
y_pred = np.array([None] * 128)
Sign up to request clarification or add additional context in comments.

7 Comments

You have to consider carefully whether or not to use None, which will force y_pred to have object dtype, which kills performance.
True, but it is a consideration worth mentioning, especially if the OP is new to python/numpy.
yup, thanks for your comments, this is important thing to mention
@pt12lol: I believe that you are adressing completely different issue. Indexing with something different than integer is not a problem here.
@woockashek No, the question is clearly related to numpy.
|
1

I see you are doing a lot of matlab here ;)

If you want to do it the pythonic way! you need to loop. Like:

for index, value in zip(test_idx, results):
    y_pred[index] = value

Comments

0

Short version: replace y_pred = [None] * 128 with y_pred = np.full(128, np.NaN). Everything else will work once you do that.

Long version:

The problem as others have said, is that y_pred is a list, not an array. Lists do not support getting or setting multiple indexes at a time, you need to use a numpy array for that.

The simplest approach to what you want is to use a numpy array of what is called a "sentinel" value, a value that indicates that nothing was written there. The most obvious choice for this value is probably either 0 or NaN, but depending on what you want to do with the results it could be just about anything. NaN is the closets to None you will get in a numerical numpy array, but 0 can make some later calculations easier (for example NaN != NaN, you need to use np.isnan to identify NaN values later).

To do this with zeros, you can replace y_pred = [None] * 128 with y_pred = np.zeros(128). To use NaN (or any other value), you can use y_pred = np.full(128, np.NaN) (there is also a np.ones but I don't think it will help you here). The rest of your code can then be used as-is.

Comments

0

you could do it like this:

for i in xrange(len(test_idx)):
    y_pred[test_idx[i]] = results[i]

or:

for key, val in zip(test_idx, results):
    y_pred[key] = val

I assume your both lists have the same length.

Comments

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.