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 us
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...
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
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.
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'))