我有一个数据集,其中包括教育水平的类别列初始值是0,nan,高中,研究生院,大学我已经清理了数据并将其转换为以下值
0->其他1->高中2->研究生院3->大学
在同一列中,现在我想将此列热编码为4列
我已经尝试使用scikit learn,如下所示
onehot_encoder = OneHotEncoder()
onehot_encoded = onehot_encoder.fit_transform(df_csv['EDUCATION'])
print(onehot_encoded)
我得到了这个错误
ValueError: Expected 2D array, got 1D array instead:
array=[3 3 3 ... 3 1 3].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
发布于 2020-11-21 09:48:14
对于您的特定情况,如果您重塑底层数组(以及设置sparse=False
),它将为您提供一次性编码的数组:
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
df = pd.DataFrame({'EDUCATION':['high school','high school','high school',
'university','university','university',
'graduate school', 'graduate school','graduate school',
'others','others','others']})
onehot_encoder = OneHotEncoder(sparse=False)
onehot_encoder.fit_transform(df['EDUCATION'].to_numpy().reshape(-1,1))
>>>
array([[0., 1., 0., 0.],
[0., 1., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 0., 1.],
[0., 0., 0., 1.],
[0., 0., 0., 1.],
[1., 0., 0., 0.],
[1., 0., 0., 0.],
[1., 0., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 1., 0.],
[0., 0., 1., 0.]])
在我看来,最直接的方法是使用pandas.get_dummies
pd.get_dummies(df['EDUCATION'])
发布于 2020-11-21 09:38:07
您需要将sparse
设置为False
from sklearn.preprocessing import OneHotEncoder
onehot_encoder = OneHotEncoder(sparse=False)
y_train = np.random.randint(0,4,100)[:,None]
y_train = onehot_encoder.fit_transform(y_train)
或者,您也可以这样做
from sklearn.preprocessing import LabelEncoder
from keras.utils import np_utils
y_train = np.random.randint(0,4,100)
encoder = LabelEncoder()
encoder.fit(y_train)
encoded_y = encoder.transform(y_train)
y_train = np_utils.to_categorical(encoded_y)
https://stackoverflow.com/questions/64938963
复制相似问题