Pages

Friday, February 28, 2014

[Python] Cannot write data into multiple array

In this case, I created a 2-D array via numpy, initiated with 0, and update them with float number.
However, without any error message, all of them are 0.0.

It is because I initiated them array with 0 (int), and my data are always between 0 to 1 so that all of them are casted to 0.0 (float)

Make sure initiate your array with the same type you will use.

Below are some codes to generate the 2-D array and update them

multiDimArray.py
import numpy as np nCV = 5 nFeat = 3
# generate a 5-by-3 array initiate with 0.0 result_list = [[0.0 for j in range(0, nFeat)] for i in range(0, 5)] """ Below is what drove me crazy result_list = [[0 for j in range(0, nFeat)] for i in range(0, 5)] """ # convert to array via numpy result_array = np.array(result_list) print 'Before:', result_array # update (Python starts at 0) result[2][2] = 0.8 print 'After: ', result_array """ if you want to use list rather than array afterward, you can use either result_list = result_array.tolist() or result__list = list(result_array) """

No comments:

Post a Comment