2016-06-21 17 views
0

我從文件中加載一些數據,進行轉換,然後提取實數部分,但是,np.real函數似乎不起作用。它打印:在python中提取數組的實數部分

[[(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

實際應作爲numpy的陣列obj功能

a = numpy.array([1+2j, 3+4j]) 
print a.real 

array([ 1., 3.]) 
+0

謝謝galaxyan,但也不能正常工作。 –

+0

@KamyarGhofrani在您轉換爲真實數據之前什麼是數據? – galaxyan

+0

我正在讀取一個.mat文件,它會返回一個字典。然後我讀取字典中的值並將它們放入列表中。然後我使用np.asarray將列表轉換爲np數組。 –

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.]) 

還有的陣列中的Nonenan是一個有效的浮點數,而不是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 

轉換到複雜的(可能切掉了Nonedata[:-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]) 
+0

.astype(複雜)是沒有它的關鍵部分.real不起作用。偉大的工作指出。 – atmaere