ValueError:预期的2D数组,取而代之的是1D数组/信号处理
有人可以帮助解决此错误:我是一个初学者,发现很难弄清楚如何修复它。
这是我遇到的错误: 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如Sklearn Docs所说,
您应该发送一个2d array(n_samples,n_featers)适合方法。
当您的错误写入时,您只需重塑数组的使用:
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: