Reputation: 43
This is what I have done but it does not change what in the list of list(dataset) although able to print out the Label with number encoded for it.
dataset = [[32.3,33.5,34.2,35.3,35.3,35.7,"Light1on"],
[52.3,52.5,53.2,54.8,55.3,55.3,"Light2on"],
[100.3,110.2,112.3,132.5,142.3,153.5,"Fan1on"],
[153.5,142.3,132.5,112.3,110.2,0,"Fan1off"],
[33.2,34.5,34.6,35.3,35.3,35.8,"Light1on"],
[33.2,35.2,35.4,36.0,36.2,42.3,"Light3on"]]
plabel = []
#dataset.to_csv('table.csv', index = None, header=True)
for row in dataset:
print(row[6],'3')
label = row[6]
plabel.append(label)
le = preprocessing.LabelEncoder()
le.fit(plabel)
label = le.transform(plabel)
print(label)
for column in row:
print(column)
I hope it will be able label encode so i can use for my K Nearest neighbor model.
Outcome:
dataset = [[32.3,33.5,34.2,35.3,35.3,35.7,"Light1on"],
[52.3,52.5,53.2,54.8,55.3,55.3,"Light2on"],
[100.3,110.2,112.3,132.5,142.3,153.5,"Fan1on"],
[153.5,142.3,132.5,112.3,110.2,0,"Fan1off"],
[33.2,34.5,34.6,35.3,35.3,35.8,"Light1on"],
[33.2,35.2,35.4,36.0,36.2,42.3,"Light3on"]]
Desired outcome:
dataset = [[32.3,33.5,34.2,35.3,35.3,35.7,0],
[52.3,52.5,53.2,54.8,55.3,55.3,1],
[100.3,110.2,112.3,132.5,142.3,153.5,2],
[153.5,142.3,132.5,112.3,110.2,0,3],
[33.2,34.5,34.6,35.3,35.3,35.8,0],
[33.2,35.2,35.4,36.0,36.2,42.3,4]]
Upvotes: 0
Views: 1333
Reputation: 5372
You question is unclear but if all you want to do is to replace the content of the 7th column in dataset
while iterating over it, try this:
dataset = [[32.3,33.5,34.2,35.3,35.3,35.7,"Light1on"],
[52.3,52.5,53.2,54.8,55.3,55.3,"Light2on"],
[100.3,110.2,112.3,132.5,142.3,153.5,"Fan1on"],
[153.5,142.3,132.5,112.3,110.2,0,"Fan1off"],
[33.2,34.5,34.6,35.3,35.3,35.8,"Light1on"],
[33.2,35.2,35.4,36.0,36.2,42.3,"Light3on"]]
plabel = []
#dataset.to_csv('table.csv', index = None, header=True)
for i, row in enumerate(dataset.copy()):
print(row[6],'3')
label = row[6]
plabel.append(label)
le = preprocessing.LabelEncoder()
le.fit(plabel)
label = le.transform(plabel)
print(label)
row[6] = label
dataset[i] = row
for column in row:
print(column)
Upvotes: 1
Reputation: 4872
you can use list comprehension
# get all sixth elements
plabel = [x[6] for x in dataset]
le.fit(plabel)
plabel = le.transform(plabel)
# replace sixth elements from each list inside 'dataset' with encoded label
dataset_encoded = [[plabel[i] if indx==6 else elem for indx, elem in enumerate(x)] for i,x in enumerate(dataset)]
>>> dataset_encoded
[[32.3, 33.5, 34.2, 35.3, 35.3, 35.7, 2],
[52.3, 52.5, 53.2, 54.8, 55.3, 55.3, 3],
[100.3, 110.2, 112.3, 132.5, 142.3, 153.5, 1],
[153.5, 142.3, 132.5, 112.3, 110.2, 0, 0],
[33.2, 34.5, 34.6, 35.3, 35.3, 35.8, 2],
[33.2, 35.2, 35.4, 36.0, 36.2, 42.3, 4]]
Upvotes: 1
Reputation: 130
A simple solution would be:
Here you go:
df = pd.DataFrame.from_records(dataset)
df[6] = le.fit_transform(df[6].values)
dataset = df.values.tolist()
Comment if anything needs explanation.
Upvotes: 1