python populate elements in array -


i new python (previous matlab user).

i have array

y_pred =  [none] * 128 

test_idx 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 replace values of y_pred corresponding test_idx 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 try

y_pred[test_idx] = results 

i error: typeerror: integer arrays 1 element can converted index

short version: replace y_pred = [none] * 128 y_pred = np.full(128, np.nan). else work once that.

long version:

the problem others have said, y_pred list, not array. lists not support getting or setting multiple indexes @ time, need use numpy array that.

the simplest approach want use numpy array of called "sentinel" value, value indicates nothing written there. obvious choice value either 0 or nan, depending on want results anything. nan closets none in numerical numpy array, 0 can make later calculations easier (for example nan != nan, need use np.isnan identify nan values later).

to zeros, can replace y_pred = [none] * 128 y_pred = np.zeros(128). use nan (or other value), can use y_pred = np.full(128, np.nan) (there np.ones don't think here). rest of code can used as-is.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -