sklearn.preprocessing.LabelEncoder和onehotencoder

来源:互联网 发布:java sdk是什么 编辑:程序博客网 时间:2024/06/06 02:30

sklearn.preprocessing.LabelEncoder

Examples

LabelEncoder can be used to normalize labels.

>>>
>>> from sklearn import preprocessing>>> le = preprocessing.LabelEncoder()>>> le.fit([1, 2, 2, 6])LabelEncoder()>>> le.classes_array([1, 2, 6])>>> le.transform([1, 1, 2, 6]) array([0, 0, 1, 2]...)>>> le.inverse_transform([0, 0, 1, 2])array([1, 1, 2, 6])

It can also be used to transform non-numerical labels (as long as they are hashable and comparable) to numerical labels.

>>>
>>> le = preprocessing.LabelEncoder()>>> le.fit(["paris", "paris", "tokyo", "amsterdam"])LabelEncoder()>>> list(le.classes_)['amsterdam', 'paris', 'tokyo']>>> le.transform(["tokyo", "tokyo", "paris"]) array([2, 2, 1]...)>>> list(le.inverse_transform([2, 2, 1]))['tokyo', 'tokyo', 'paris']
fit()中必须是内容
from sklearn.preprocessing import LabelEncodercols = ('FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond',         'ExterQual', 'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual', 'BsmtFinType1',         'BsmtFinType2', 'Functional', 'Fence', 'BsmtExposure', 'GarageFinish', 'LandSlope',        'LotShape', 'PavedDrive', 'Street', 'Alley', 'CentralAir', 'MSSubClass', 'OverallCond',         'YrSold', 'MoSold')# process columns, apply LabelEncoder to categorical featuresfor c in cols:    lbl = LabelEncoder()     lbl.fit(list(all_data[c].values))     all_data[c] = lbl.transform(list(all_data[c].values))# shape        print('Shape all_data: {}'.format(all_data.shape))


阅读全文
0 0