使用OnehotEncoder将编码标签转换为数组

时间:2019-03-28 22:34:01

标签: python machine-learning one-hot-encoding

当我尝试使用Python 3.67使用OnehotEncoder将编码的标签转换为数组时遇到问题。

import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder, OneHotEncoder


dataset = pd.read_csv('crime.csv')
X = dataset.sample(1000)
X.dropna()

x= X.loc[:,dataset.columns != 'IS_CRIME'].values

#Displaying content of the categorical variable
a = X.iloc[:,16]

#Encoding
labelencoder_X = LabelEncoder()
x[:, 16] = labelencoder_X.fit_transform(x[:, 16])
onehotencoder = OneHotEncoder(categorical_features = [0])
x = onehotencoder.fit_transform(x).toarray()

Image 1

Image 2

0 个答案:

没有答案