0
$\begingroup$

I have column with movie ids like this:

tt0984332
tt0984332
tt0847742
ttnanana1

I would like to convert them into numbers that can be inserted into neural network as features, like this:

0
0
1
2

How can I do that?

$\endgroup$
2
  • $\begingroup$ What is the information contained in those strings ? $\endgroup$ Dec 13 '18 at 9:29
  • $\begingroup$ string contains id of a movie $\endgroup$
    – Myron
    Dec 13 '18 at 9:42
1
$\begingroup$

What you are looking for is performed exactly by sklearn.LabelEncoder, an example of which can be found here.

In your case:

from sklearn.preprocessing import LabelEncoder

encoder = LabelEncoder()
encoder.fit(["tt0984332", "tt0984332", "tt0847742", "ttnanana1"])

# Show the unique classes
print(encoder.classes_)
# out: array(['tt0847742', 'tt0984332', 'ttnanana1'], dtype='<U9')

# Convert labels to integers
encoder.transform(["tt0984332", "tt0984332", "tt0847742", "ttnanana1"])
# out: array([1, 1, 0, 2])
$\endgroup$
0
$\begingroup$
#I did just index mapping using dictionary and then added it to data frame
#preprocess itemid
unique_items = set(df.itemid.values)
item_to_idx_dict = {}
item_count = 0

for item in unique_items:
    item_to_idx_dict[item] = item_count
    item_count += 1

df['item_idx'] = df.apply(lambda row: item_to_idx_dict[row.itemid], axis = 1)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.