Reputation: 263
I want to insert zero and one at the end of last column of my whole data.
These are actually my labels which I have to convert in one hot vector later. But when I insert them at the end of column and when I print that column it prints as
[0., 0., 1.,...]
rather it should be [0,0,1,....]
.
What should I do to overcome this error? Because with 0.
and 1.
I can't get one hot representation and with these numbers I get error of unsupported iterator index
. I need the last column as 0
and 1s
.
Can someone please guide me what should I do to fix this error?
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
When I print insert_label of above function I get following result. zero and one is appearing as 0.0000e+00
and 1.0000e+00
. I want it to be 0
and 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]]
Here in the input data for calling above function.
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]]`
Function call "set_label(data_all,Labels,2)
Upvotes: 1
Views: 164
Reputation: 1319
you can convert your data type to object like this :
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
and your output would be like this :
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)
or when you want to select your label and convert it to one hot convert labels type to integer :
np.array('your selected array',dtype = np.int)
Upvotes: 1