from sklearn.preprocessing import OneHotEncoder
column_to_encode = ['Sex','Fasting_Blood_Sugar','Exercise_Angina']
# Create a nominal encoder object
encoder = OneHotEncoder(drop="first")
# Fit and transform the specified column using the encoder
encoded_column = encoder.fit_transform(df[column_to_encode])
# Convert the encoded column to a DataFrame
encoded_df = pd.DataFrame(encoded_column.toarray(), columns=encoder.get_feature_names_out(column_to_encode))
# Concatenate the encoded DataFrame with the original DataFrame
df = pd.concat([df, encoded_df], axis=1)
# Drop the original column that was encoded
df.drop(column_to_encode, axis=1, inplace=True)
我正在尝试这个代码。并且从数据集中删除三个值。请帮助我理解
1条答案
按热度按时间vc9ivgsu1#
您应该使用get_dummies()函数。这是你在动态编码时应该使用的函数。我建议只在使用管道时使用OneHotEncoder,它将自己工作。