-1

I am trying to add zeros to an array at desired indices.

newerror = []
    for i in range(40):
        temp = []
        for j in range(20):
            output = numpy.zeros((196)).astype(numpy.float64) # 196*1

            x=index2[i][j].astype(int) # 40*20*49 
            # filling 49 elements into 196 elements
            # at desired indices

            y=error[i][j] # 40*20*49
            for (ind, rep) in zip(x, y):
                output[ind] = rep
            temp.append(output)
        newerror.append(temp) #40*20*196

I have achieved it using the above code. For each array of inputs: the zeros in output array are replaced with input values at the the desired index.

but my result looks like this: with a random array showing up in scientific notation while there is no requirement.

(piece of ouput;Total output size:40*20*196)

  0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
 7.69098301e-02   0.00000000e+00   0.00000000e+00   1.51987125e-01
 0.00000000e+00   8.44965872e-02   0.00000000e+00  -1.59399264e-04
 1.44000233e-01   0.00000000e+00   1.07718190e-01   0.00000000e+00
 0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
 0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
 0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
 0.00000000e+00   0.00000000e+00   0.00000000e+00  -3.70872988e-02
 0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
 9.31264783e-02   0.00000000e+00   0.00000000e+00   0.00000000e+00
-5.27716619e-02   0.00000000e+00   0.00000000e+00   0.00000000e+00
 0.00000000e+00   0.00000000e+00  -8.66752459e-02   0.00000000e+00
 1.60625907e-01   0.00000000e+00   0.00000000e+00   0.00000000e+00
-5.99051582e-02   0.00000000e+00   0.00000000e+00   0.00000000e+00
 0.00000000e+00   4.35048696e-02   0.00000000e+00  -4.90880002e-02
 0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
 0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
 0.00000000e+00  -9.75583410e-03   0.00000000e+00   0.00000000e+00
 0.00000000e+00   0.00000000e+00   0.00000000e+00  -5.85866895e-02
-4.12372907e-02   0.00000000e+00   3.39738431e-02   0.00000000e+00
 0.00000000e+00  -3.28913870e-02   0.00000000e+00   0.00000000e+00
 0.00000000e+00  -6.56889122e-02   0.00000000e+00   1.12313472e-01
 0.00000000e+00   0.00000000e+00  -1.28921454e-02   0.00000000e+00
 2.64238752e-02   0.00000000e+00   0.00000000e+00  -3.83728496e-02
 0.00000000e+00   0.00000000e+00   0.00000000e+00   5.22824327e-03
 0.00000000e+00   0.00000000e+00   1.60543359e-01   0.00000000e+00
 0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
 0.00000000e+00   0.00000000e+00   7.20355685e-02   0.00000000e+00

I 'think' this is affecting my code in a later stage which i am unable to share. Any thoughts?

uttejh
  • 184
  • 1
  • 14
  • Possible duplicate of [How to pretty-printing a numpy.array without scientific notation and with given precision?](https://stackoverflow.com/questions/2891790/how-to-pretty-printing-a-numpy-array-without-scientific-notation-and-with-given) – eyllanesc Nov 18 '17 at 16:37
  • That does not affect the data, it is only printing in scientific notation. – eyllanesc Nov 18 '17 at 16:38
  • Oh! Can you explain why this random array is being printed in scientific notation when the values are same other values? @eyllanesc – uttejh Nov 18 '17 at 16:55
  • I have considered that solution before asking, it is printing in scientific notation because the values are relatively large. Please down vote if there is a good reason and please try to read and understand the difference. Thank you – uttejh Nov 18 '17 at 16:59
  • what is `index`: `zip(index[i], input[i])`?, You must provide an MVCE, if you do not provide it, I will only interpret your comments and try to implement an example based on it. – eyllanesc Nov 18 '17 at 17:03
  • Index as in indices -_- My bad! I have edited – uttejh Nov 18 '17 at 17:06
  • `for i in range(10): ..... index[i]` but the index length is 4. – eyllanesc Nov 18 '17 at 17:09
  • 1
    Take the time to post a valid example as I am taking the time to read and try to help you in your question. – eyllanesc Nov 18 '17 at 17:10
  • thank you @eyllanesc for your patience . give me 5. I will paste the real code. I just took an example because the real problem was with scientific notation where the answer was correct. – uttejh Nov 18 '17 at 17:16

1 Answers1

1

If the range of values in an array is wide enough, especially if some are small enough, numpy displays the whole thing with scientific notation:

In [410]: input = [0.03302, 0.232323, 0.22222, 0.232344] 
In [411]: np.array(input)
Out[411]: array([ 0.03302 ,  0.232323,  0.22222 ,  0.232344])
In [412]: input[2] *= 1e-6
In [413]: input
Out[413]: [0.03302, 0.232323, 2.2222e-07, 0.232344]

One item of this list is small enough to use scientific notation

In [414]: np.array(input)
Out[414]: 
array([  3.30200000e-02,   2.32323000e-01,   2.22220000e-07,
         2.32344000e-01])

All elements of the array equivalent are displayed the same way.

The zip step has nothing to do with this (you don't need to iterate to do the assignment):

In [415]: arr = np.zeros(10)
In [416]: arr.dtype
Out[416]: dtype('float64')
In [417]: index= [0, 2, 6, 7]
In [418]: arr[index] = input
In [419]: arr
Out[419]: 
array([  3.30200000e-02,   0.00000000e+00,   2.32323000e-01,
         0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
         2.22220000e-07,   2.32344000e-01,   0.00000000e+00,
         0.00000000e+00])

Scientific notation in the display should not produce problems with later calculations. It's just a display issue, and doesn't affect numerical values.


A copy-n-paste from your scientific notation line:

In [420]: alist =[  0.00000000e+00,  -1.88939223e-01,  -1.99170532e-01,   0.0000
     ...: 0000e+00, 0.00000000e+00,   3.66637802e-02,   0.00000000e+00,   8.0379
     ...: 8680e-02, -1.46634700e-01,   0.00000000e+00]
In [421]: alist
Out[421]: 
[0.0,
 -0.188939223,
 -0.199170532,
 0.0,
 0.0,
 0.0366637802,
 0.0,
 0.080379868,
 -0.1466347,
 0.0]
In [422]: np.array(alist)
Out[422]: 
array([ 0.        , -0.18893922, -0.19917053,  0.        ,  0.        ,
        0.03666378,  0.        ,  0.08037987, -0.1466347 ,  0.        ])

In your latest display, -1.59399264e-04 is small enough.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • As I have mentioned in an earlier comment, there are no real small/big values. All vaues are of size -10^2 – uttejh Nov 18 '17 at 17:18
  • What is that display with a mix of notations? Just separate prints? It can't be a 2d array. But without commas it can't lists. – hpaulj Nov 18 '17 at 17:25
  • I had similar issue with datatype mismatch earlier which produced garbage results of this sort which then resulted in errors. So, i am making sure that does not affect the calculations in any way. – uttejh Nov 18 '17 at 17:26
  • 1
    What about the `-1.59399264e-04`? – hpaulj Nov 18 '17 at 18:13