Python Replacing every imaginary value in array by random

106 views Asked by At

I got an

array([[ 0.01454911+0.j,  0.01392502+0.00095922j,
         0.00343284+0.00036535j, 0.00094982+0.0019255j ,
         0.00204887+0.0039264j , 0.00112154+0.00133549j,  0.00060697+0.j],
       [ 0.02179418+0.j,  0.01010125-0.00062646j,
         0.00086327+0.00495717j, 0.00204473-0.00584213j,
         0.00159394-0.00678094j, 0.00121372-0.0043044j , 0.00040639+0.j]])

I need a solution which gives me the possibility to replace just the imaginary components by an random value generated by:

numpy.random.vonmises(mu, kappa, size=size)

The resulting array needs to be in the same form as the first one.

3

There are 3 answers

1
Hao Jiang On

Try using this approach:

  • Store your numbers into a 2-D array: Real-part and Imaginary-part.
  • Then replace the Imaginary-part with the randomly chosen numbers.
2
jhoepken On

Loop over the numbers and just set them to a value you like. The parameters mu and kappa for the numpy.random.vonmises function need to be defined, since in they are undefined in the below example.

import numpy as np

data = np.array([[ 0.01454911+0.j,  0.01392502+0.00095922j,
         0.00343284+0.00036535j, 0.00094982+0.0019255j ,
         0.00204887+0.0039264j , 0.00112154+0.00133549j,  0.00060697+0.j],
       [ 0.02179418+0.j,  0.01010125-0.00062646j,
         0.00086327+0.00495717j, 0.00204473-0.00584213j,
         0.00159394-0.00678094j, 0.00121372-0.0043044j , 0.00040639+0.j]])

def setRandomImag(c):
    c.imag = np.random.vonmises(mu, kappa, size=size)
    return c

data = [ setRandomImag(i) for i in data]
0
Daniel Velden On
n_epochs = 2
n_freqs = 7
# form giving parameters for the array

data2 = np.zeros((n_epochs, n_freqs), dtype=complex)
for i in range(0,n_epochs):
     data2[i] = np.real(data[i]) + np.random.vonmises(mu, kappa) * complex(0,1)

It gives my whole n_epoch the same imaginary value. Not exactly what I was asking for, but solves my problem.