3

stackoverflow,

I have a matrix containing complex numbers (ex. -2.2982235934153075E-11+2.1179547211742553E-9i) that I need to import to a numpy array. I've been using genfromtext(file) to parse all my other, real values, but I'm getting a nan for all complex values. Any ideas?

self.raw = (genfromtxt(self.loc, delimiter=',', skip_header=9, dtype=float))
[m,n] = shape(self.raw)
data = zeros((m, n-3))
data[:, :] = self.raw[:, 3::]

returns:

data = array([nan, nan, nan, ...])
1
  • paste some code that you used and paste an exemplary input (preferably, part of the input file) Commented Aug 16, 2013 at 19:15

4 Answers 4

4

You can do:

import numpy as np
a = np.genfromtxt(filename, converters={0: lambda x: x.replace('i','j')},
                  dtype=str)
a = np.complex_(a)

Note that the converters parameter was required because your text file is using i to denote the imaginary part.

It may be easier to convert your text file externally to replace all the i by j, avoiding a complicated converters argument in case you have many columns.

If your textfile with imaginary numbers had the format:

 (-2.298223593415307508e-11+2.117954721174255306e-09j)
 (-2.298223593415307508e-11+2.117954721174255306e-09j)
 (-2.298223593415307508e-11+2.117954721174255306e-09j)
 (-2.298223593415307508e-11+2.117954721174255306e-09j)
 (-2.298223593415307508e-11+2.117954721174255306e-09j)
 (-2.298223593415307508e-11+2.117954721174255306e-09j)
 (-2.298223593415307508e-11+2.117954721174255306e-09j)

Where you could read using only:

a = np.loadtxt(filename).view(complex)

for example...

Sign up to request clarification or add additional context in comments.

9 Comments

This works for a single column. It appears the data is suppose to be multi-column. I would probably externally convert the 'i'->'j' in the data file, perhaps using sed, awk, or even perl!, then read in the converted file using dtype=np.complex
If you can convert it externally you don't need to use the converters argument for this case...
Right, and you wouldn't have to worry about the actual number of columns in the file. It isn't entirely clear what the constraints from the OP are.
OP here, python seems to be reading the columns of the .csv ([m,n] =[2500, 400]) as strings. Edit: 'Python' <--- "Excel'
@1ifbyLAN2ifbyC yes... if the file is written like (x + yj), including the brackets, it will easily read using loadtxt or genfromtxt, as examplified in the answer...
|
1

The way I ended up having to do this was to first replace('i', 'j') for all cells in the original .csv file and save the new, corrected file. Afterwards, reading the .csv with dtype=str caused errors in subsequent calculations, but it turns out you can parse the .csv with dtype=complex128, which solved all my problems. Thanks for the help on the conversion @Saullo-Castro

Comments

0

The following might be an option to obtain a NumPy array from multi-column complex-numbered .csv file:

Say we have a file.csv containing two rows and three columns of complex numbers:

-0.00034467+0.j,         0.00493246+0.j,         0.00365753-0.00361799j
-0.00782533-0.00081274j,-0.00402968+0.01065282j,-0.01345174+0.00464461j

The following will yield a NumPy array:

filename = 'file.csv'
data = pd.read_csv(filename, sep=",", header=None)
data = data.applymap(lambda s: np.complex(s.replace('i', 'j'))).values

Checking if data is a NumPy array:

>> type(data)
numpy.ndarray

PS: The answer is based on this answer.

Comments

-1

Import the csv file as an array of strings with genfromtxt(...) with dtype='str'. Then you can manipulate each entry with np.vectorize(...).

import numpy as np
from numpy import genfromtxt

# import data as an array of strings using the dtype
temp = genfromtxt('matlab_sim_Z.csv', delimiter=',',dtype='str')

# perform elementwise conversion to complex numpers
mapping = np.vectorize(lambda t:complex(t.replace('i','j')))
data = mapping(temp)

In a single line:

data = np.vectorize(lambda t:complex(t.replace('i','j'))) (genfromtxt('matlab_sim_Z.csv', delimiter=',',dtype='str'))

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.