我正在从文件加载一些数据,进行转换然后提取真实部分,但是,np.real函数似乎不起作用。它打印:
[[(0.99023793104890667 + 0.034016079376604003j)0.9905214315431583 0.99033818364852688 ...,0.86609847609098078 0.87048246890045189无]]
显然第一个元素很复杂。
import numpy as np
import scipy.io as sio
import os.path
import PLVHilbertImplementation as pet
import matplotlib.pyplot as plt
import Likelihood_gen as lg
#for the interictal files
for j in range(1, 2, 1):
filename = '/home/kam/Documents/kaggle/Dog_5/Dog_5_interictal_segment_' + str(j).zfill(4) + '.mat'
PLVs=[]
if os.path.isfile(filename):
#For the files that exist do this
data=sio.loadmat(filename)['interictal_segment_1'][0][0][0][1:1024]
numchannels, numpoints = data.shape
label=np.ones((1,numpoints))
for i in range(0, 2, 1):
for j in range(i+1, 2, 1):
PLVs.append(np.asarray((pet.PLV(data[i,:],np.transpose(data[j,:]), 1024, 5, 128))))
print(np.real(PLVs)) #this is where the problem is
# Metric=np.sum(np.exp(np.real(np.asarray(PLVs)),1))
# plt.plot(Metric)
# plt.show
else:
print('no', filename)
break
#for the preictal files
for j in range(1, 1, 1):
filename = 'Dog_1_preictal_segment_' + str(j).zfill(4) + '.mat'
if os.path.isfile(filename):
data=sio.loadmat(filename)[0][0][0]
numchannels, numpoints = shape(data)
print(numchannels)
else:
print('no', filename)
break
答案 0 :(得分:1)
[[(0.99023793104890667+0.034016079376604003j) 0.9905214315431583
0.99033818364852688 ..., 0.86609847609098078 0.87048246890045189 None]]
有一些线索表明这不是普通的复杂浮点数组,这就是.real
方法的用途。
复数包含在()
不喜欢:
In [1011]: np.arange(5)+np.arange(2,7)*1j
Out[1011]: array([ 0.+2.j, 1.+3.j, 2.+4.j, 3.+5.j, 4.+6.j])
In [1013]: (np.arange(5)+np.arange(2,7)*1j).real
Out[1013]: array([ 0., 1., 2., 3., 4.])
阵列中有一个None
。 nan
是有效的浮点数,而不是None
。
我猜测形状是2d(但你应该打印出来),而且dtype是对象 - 但是你需要显示它。
我可以用以下方式创建像这样打印的东西:
In [1014]: data=np.empty((5,),dtype=object) # 1d, could be 2
In [1015]: data # default empty fill None
Out[1015]: array([None, None, None, None, None], dtype=object)
In [1016]: data[0]=1+3.j
In [1017]: data[1:4]=[1.2,3,4.2]
In [1018]: data # the repr display
Out[1018]: array([(1+3j), 1.2, 3, 4.2, None], dtype=object)
In [1019]: print(data) # the str display
[(1+3j) 1.2 3 4.2 None]
In [1021]: data.real
Out[1021]: array([(1+3j), 1.2, 3, 4.2, None], dtype=object)
In [1022]: data[0].real
Out[1022]: 1.0
转换为复合形式(可以使用None
切割data[:-1]
)
In [1027]: data.astype(complex)
Out[1027]: array([ 1.0 +3.j, 1.2 +0.j, 3.0 +0.j, 4.2 +0.j, nan+nanj])
In [1028]: data.astype(complex).real
Out[1028]: array([ 1. , 1.2, 3. , 4.2, nan])
答案 1 :(得分:0)
real应该用作numpy数组obj函数
a = numpy.array([1+2j, 3+4j])
print a.real
array([ 1., 3.])