, you are given a list of items in a DataFrame as below.D = [‘A’,’B’,’C’,’D’,’E’,’AA’,’AB’]Now, you want to apply label encoding on this list for importing and transforming, using LabelEncoder.from sklearn.preprocessing import LabelEncoderle = LabelEncoder()What will be the output of the print statement below ?print le.fit_transform(D)array([0, 2, 3, 4, 5, 6, 1])array([0, 3, 4, 5, 6, 1, 2])array([0, 2, 3, 4, 5, 1, 6])Any of the above
Question
, you are given a list of items in a DataFrame as below.D = [‘A’,’B’,’C’,’D’,’E’,’AA’,’AB’]Now, you want to apply label encoding on this list for importing and transforming, using LabelEncoder.from sklearn.preprocessing import LabelEncoderle = LabelEncoder()What will be the output of the print statement below ?print le.fit_transform(D)array([0, 2, 3, 4, 5, 6, 1])array([0, 3, 4, 5, 6, 1, 2])array([0, 2, 3, 4, 5, 1, 6])Any of the above
Solution
The output of the print statement will be array([0, 1, 2, 3, 4, 5, 6]).
Here's the step by step explanation:
- Import the necessary library:
from sklearn.preprocessing import LabelEncoder - Initialize the LabelEncoder object:
le = LabelEncoder() - Fit and transform the data:
le.fit_transform(D)
LabelEncoder in sklearn is a utility class to help normalize labels such that they contain only values between 0 and n_classes-1. It can be used to transform non-numerical labels to numerical labels (or nominal categorical variables). Numerical labels are always between 0 and n_classes-1.
The list D = ['A','B','C','D','E','AA','AB'] is sorted alphabetically and then encoded as numbers from 0 to 6. So, 'A' is 0, 'AA' is 1, 'AB' is 2, 'B' is 3, 'C' is 4, 'D' is 5, and 'E' is 6.
Therefore, the output of print(le.fit_transform(D)) will be array([0, 1, 2, 3, 4, 5, 6]).
Similar Questions
What will be the output of the print statement below ?print le.fit_transform(D)array([0, 2, 3, 4, 5, 6, 1])array([0, 3, 4, 5, 6, 1, 2])array([0, 2, 3, 4, 5, 1, 6])Any of the above
What will be the output of the givencode?a. List=[‘p’,’r’,’i’,’n’,’t’,]b. Print list[8:]
What will be the output of the following Python code?x = [[0], [1]]print(len(' '.join(list(map(str, x)))))
x = [[0], [1]]print(len(' '.join(list(map(str, x)))))
Consider the Python Listarr = [[123],[456]]What will the following code display?for i in range(len(rows)): for j in range(len(columns)): print(rows[i][j],end=" ")print("\n")
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.