ValueError:预期的2D数组,取而代之的是1D数组/信号处理

发布于 2025-01-31 14:39:58 字数 3051 浏览 1 评论 0 原文

有人可以帮助解决此错误:我是一个初学者,发现很难弄清楚如何修复它。

这是我遇到的错误: valueerror:预期的2D数组,取而代之的是1D数组: array = [282 561 837 ... 649442 649701 649957]。 如果您的数据具有单个功能或Array.Reshape(1,-1),则使用array.reshape(-1,1)重塑数据,如果它包含一个示例。

class MyDataset(Dataset):
    def __init__(self, patient_ids,bih2aami=True):
        self.patient_ids = patient_ids # list of patients ID
        #self.directory=""
        self.nb_qrs = 99 #number of beats 
        self.idx_tuples = flatten([[(patient_idx, rpeak_idx) for rpeak_idx in range(self.nb_qrs)] 
                                   for patient_idx in range(len(patient_ids))])
        self.bih2aami=bih2aami
   
    def __len__(self):#returns the size of the data set.
        return len(self.idx_tuples) # length of the dataset 
 
    def __getitem__(self, idx): # get one sample from the dataset 
        patient_idx, rpeak_idx = self.idx_tuples[idx] 
        patient_id = self.patient_ids[patient_idx] 
        file = self.directory + patient_id
        signal, normal_qrs_pos = get_signal(file)
        
        # Create a range of windows positions

        if (idx//2 == idx/2):
          qrs_pos = normal_qrs_pos[rpeak_idx]
        else:
          qrs_pos = normal_qrs_pos[rpeak_idx] + randint(-round(.25*fs),round(.25*fs))

        #win_pos = normal_qrs_pos # FIND CORRECT WIN_POS FOR THIS patient

        beat, label = extract_beat(signal,qrs_pos,normal_qrs_pos)

        if (label == 1):
          print("==== FOUND ONE MATCHING QRS === pos = ", qrs_pos) 
        else:
          print("==== NO MATCH === pos = ", qrs_pos) 
          
   
        X, y  = torch.tensor(beat).float(), torch.tensor(label).float()
        print(y.size())
 
        return X,y

def extract_beat(signal, win_pos, qrs_positions, win_msec=40, fs=360, start_beat=36, end_beat=108):
"""
win_pos position at which you place the window of your beat
qrs_positions (list) the qrs indices from the annotations (read them from the atr file)-->obtained from annotation.sample
win_msec in milliseconds
"""
#extract signal 
signal = np.array(signal)
#print(signal.shape)
#beat_array = np.zeros(start_beat+end_beat)#number of channels
start = int(max(win_pos-start_beat,0))
stop  = start+start_beat+end_beat+1 
#print(beat_array.shape,signal.shape)
beat =  signal[start:stop] 
#print(" =========== BEAT = ",len(beat))


#compute the nearest neighbor of win_pos among qrs_positions
tolerance = (fs*win_msec)//1000 #samples at a distance <tolerance are matched
nbr = NearestNeighbors(n_neighbors=1).fit(qrs_positions)
distances, indices = nbr.kneighbors(np.array([[win_pos]]).reshape(-1,1))

#label
if distances[0][0] <= tolerance:
    label =  1
else: 
    label =  0 
print(distances[0],tolerance,label)
    
return beat, label

Can someone help to fix this error: I am a beginner and finding it difficult to figure out how to fix it.

This is the error I am getting :
ValueError: Expected 2D array, got 1D array instead:
array=[ 282 561 837 ... 649442 649701 649957].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

enter image description here

enter image description here

class MyDataset(Dataset):
    def __init__(self, patient_ids,bih2aami=True):
        self.patient_ids = patient_ids # list of patients ID
        #self.directory=""
        self.nb_qrs = 99 #number of beats 
        self.idx_tuples = flatten([[(patient_idx, rpeak_idx) for rpeak_idx in range(self.nb_qrs)] 
                                   for patient_idx in range(len(patient_ids))])
        self.bih2aami=bih2aami
   
    def __len__(self):#returns the size of the data set.
        return len(self.idx_tuples) # length of the dataset 
 
    def __getitem__(self, idx): # get one sample from the dataset 
        patient_idx, rpeak_idx = self.idx_tuples[idx] 
        patient_id = self.patient_ids[patient_idx] 
        file = self.directory + patient_id
        signal, normal_qrs_pos = get_signal(file)
        
        # Create a range of windows positions

        if (idx//2 == idx/2):
          qrs_pos = normal_qrs_pos[rpeak_idx]
        else:
          qrs_pos = normal_qrs_pos[rpeak_idx] + randint(-round(.25*fs),round(.25*fs))

        #win_pos = normal_qrs_pos # FIND CORRECT WIN_POS FOR THIS patient

        beat, label = extract_beat(signal,qrs_pos,normal_qrs_pos)

        if (label == 1):
          print("==== FOUND ONE MATCHING QRS === pos = ", qrs_pos) 
        else:
          print("==== NO MATCH === pos = ", qrs_pos) 
          
   
        X, y  = torch.tensor(beat).float(), torch.tensor(label).float()
        print(y.size())
 
        return X,y

The code for beat extraction

def extract_beat(signal, win_pos, qrs_positions, win_msec=40, fs=360, start_beat=36, end_beat=108):
"""
win_pos position at which you place the window of your beat
qrs_positions (list) the qrs indices from the annotations (read them from the atr file)-->obtained from annotation.sample
win_msec in milliseconds
"""
#extract signal 
signal = np.array(signal)
#print(signal.shape)
#beat_array = np.zeros(start_beat+end_beat)#number of channels
start = int(max(win_pos-start_beat,0))
stop  = start+start_beat+end_beat+1 
#print(beat_array.shape,signal.shape)
beat =  signal[start:stop] 
#print(" =========== BEAT = ",len(beat))


#compute the nearest neighbor of win_pos among qrs_positions
tolerance = (fs*win_msec)//1000 #samples at a distance <tolerance are matched
nbr = NearestNeighbors(n_neighbors=1).fit(qrs_positions)
distances, indices = nbr.kneighbors(np.array([[win_pos]]).reshape(-1,1))

#label
if distances[0][0] <= tolerance:
    label =  1
else: 
    label =  0 
print(distances[0],tolerance,label)
    
return beat, label

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

归途 2025-02-07 14:39:58

正如Sklearn Docs所说,

您应该发送一个2d array(n_samples,n_featers)适合方法。

当您的错误写入时,您只需重塑数组的使用:

#compute the nearest neighbor of win_pos among qrs_positions
colerance = (fs*win_msec)//1000 #samples at a distance <tolerance are matched
nbr = NearestNeighbors(n_neighbors=1).fit(qrs_positions.reshape(-1,1))
distances, indices = nbr.kneighbors(np.array([[win_pos]]).reshape(-1,1))

As sklearn docs says in: https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.NearestNeighbors.html#sklearn.neighbors.NearestNeighbors.fit

You should send a 2d array ( of shape (n_samples, n_features) ) to fit method.

And as your error write you can just reshape the array use:

#compute the nearest neighbor of win_pos among qrs_positions
colerance = (fs*win_msec)//1000 #samples at a distance <tolerance are matched
nbr = NearestNeighbors(n_neighbors=1).fit(qrs_positions.reshape(-1,1))
distances, indices = nbr.kneighbors(np.array([[win_pos]]).reshape(-1,1))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文