我想在整个数据的最后一列的末尾插入零和一。
这些实际上是我的标签,以后我必须在一个热矢量中进行转换。但是,当我将它们插入列的末尾并在打印该列时,它会打印为
[0., 0., 1.,...]
,而应该是[0,0,1,....]
。
我该怎么做才能克服此错误?因为使用0.
和1.
无法获得一个热表示,而使用这些数字却会得到unsupported iterator index
的错误。我需要最后一列为0
和1s
。
有人可以指导我如何解决此错误吗?
def set_label(data_all, Labels, hc):
loc_1=len(data_all[0])
labels= Labels
insert_h = np.insert(data_all,loc_1,labels,axis=1)
label_store =[]
for i in range(len(labels)):
hmax = labels[i]
if hmax < hc:
label_store.append(0)
elif hmax > hc:
label_store.append(1)
loc_2= len(insert_h[0])
insert_label =np.insert(insert_h, loc_2, label_store,axis=1)
return insert_label
当我打印上述函数的insert_label时,得到以下结果。零,一个显示为0.0000e+00
和1.0000e+00
。我希望它是0
和1
。
[[1.11886399e-01 2.83619339e+00 3.14394233e+001.00000000e+00 0.00000000e+00]
[2.29317271e-01 1.87929947e+00 3.14713896e+00 1.00000000e+00 0.00000000e+00]
[3.69530173e-02 3.52560100e+00 5.05051823e+00 1.50000000e+00 0.00000000e+00]
[1.56559513e-01 2.10847705e+00 3.88715380e+00 1.50000000e+00 0.00000000e+00]
[2.44876157e-03 6.02824866e+00 1.02350953e+01 5.00000000e+00 1.00000000e+00]
[9.38766706e-03 4.89698950e+00 6.28692713e+00 5.00000000e+00 1.00000000e+00]]
此处输入了用于调用上述函数的数据。
Labels=[1. 1. 1.5 1.5 5. 5. ]
data_all=[[1.11886399e-01, 2.83619339e+00, 3.14394233e+00]
[2.29317271e-01, 1.87929947e+00, 3.14713896e+00]
[3.69530173e-02, 3.52560100e+00, 5.05051823e+00]
[1.56559513e-01, 2.10847705e+00, 3.88715380e+00]
[2.44876157e-03, 6.02824866e+00, 1.02350953e+01]
[9.38766706e-03, 4.89698950e+00, 6.28692713e+00]]`
函数调用 “ set_label(data_all,Labels,2)
答案 0 :(得分:1)
您可以将数据类型转换为这样的对象:
def set_label(data_all, Labels, hc):
loc_1=len(data_all[0])
labels= Labels
insert_h = np.insert(data_all,loc_1,labels,axis=1)
label_store =[]
for i in range(len(labels)):
hmax = labels[i]
if hmax < hc:
label_store.append(0)
elif hmax > hc:
label_store.append(1)
loc_2= len(insert_h[0])
#this is what I added to your code
insert_h = np.insert(np.array(insert_h,dtype='O'),loc_2,np.array(labels,dtype = np.int),axis=1)
return insert_h
,您的输出将如下所示:
array([[0.111886399, 2.83619339, 3.14394233, 1.0, 1],
[0.229317271, 1.87929947, 3.14713896, 1.0, 1],
[0.0369530173, 3.525601, 5.05051823, 1.5, 1],
[0.156559513, 2.10847705, 3.8871538, 1.5, 1],
[0.00244876157, 6.02824866, 10.2350953, 5.0, 5],
[0.00938766706, 4.8969895, 6.28692713, 5.0, 5]], dtype=object)
,或者当您要选择标签并将其转换为一种热转换标签时,将其类型转换为整数:
np.array('your selected array',dtype = np.int)