问题
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:
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...
回答2:
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
回答3:
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.
回答4:
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'))
来源:https://stackoverflow.com/questions/18280489/numpy-read-csv-with-complex-number