Python sklearn onehotencoder
Python sklearn onehotencoder
我正在尝试为我的向量的第 4 个特征(在 numpy 数组中)编码分类数据。类别是“4”或“6”。我可以用这个把它们变成二进制文件:
features_in_training_set = [[0 0 0 0 4], [0 0 0 0 4], [0 0 0 0 6],[0 0 0 0 4],[0 0 0 0 6]]
features_in_training_set[:,4] = LabelEncoder().fit_transform(features_in_training_set[:,4])
但是,当然,我需要对此进行更改,以便分类器不会认为“4”大于“6”。但是,当我 运行 以下内容时:
onehotencoder = OneHotEncoder(categorical_features=[4], handle_unknown='ignore')
features_in_training_set = onehotencoder.fit_transform(features_in_training_set).toarray()
我收到的错误是:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
TypeError: Wrong type for parameter `n_values`. Expected 'auto', int or array of ints, got <class 'numpy.ndarray'>
我已经检查过是否有任何缺失值或任何字符串,但我没有。所有特征都是整数。
谢谢。
scikit-learn (> 0.20) 中的当前 OneHotEncoder
可以处理字符串或其他分类特征本身,不需要首先使用 LabelEncoder
将类别编码为数字(或将不同的数字编码为唯一的)像你一样对数字进行排序)。
这个错误是 OneHotEncoder
中的一个错误,因为它一直在发展以处理上述情况,同时也应该支持旧的用例作为你的问题。将 n_values='auto'
添加到代码中将消除此错误,如下所示:
onehotencoder = OneHotEncoder(categorical_features=[4], n_values='auto',
handle_unknown='ignore')
如果您从代码中删除 handle_unknown
参数,那么这也有效,但不应该这样做。
在此处查看此问题:
我正在尝试为我的向量的第 4 个特征(在 numpy 数组中)编码分类数据。类别是“4”或“6”。我可以用这个把它们变成二进制文件:
features_in_training_set = [[0 0 0 0 4], [0 0 0 0 4], [0 0 0 0 6],[0 0 0 0 4],[0 0 0 0 6]]
features_in_training_set[:,4] = LabelEncoder().fit_transform(features_in_training_set[:,4])
但是,当然,我需要对此进行更改,以便分类器不会认为“4”大于“6”。但是,当我 运行 以下内容时:
onehotencoder = OneHotEncoder(categorical_features=[4], handle_unknown='ignore')
features_in_training_set = onehotencoder.fit_transform(features_in_training_set).toarray()
我收到的错误是:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
TypeError: Wrong type for parameter `n_values`. Expected 'auto', int or array of ints, got <class 'numpy.ndarray'>
我已经检查过是否有任何缺失值或任何字符串,但我没有。所有特征都是整数。
谢谢。
scikit-learn (> 0.20) 中的当前 OneHotEncoder
可以处理字符串或其他分类特征本身,不需要首先使用 LabelEncoder
将类别编码为数字(或将不同的数字编码为唯一的)像你一样对数字进行排序)。
这个错误是 OneHotEncoder
中的一个错误,因为它一直在发展以处理上述情况,同时也应该支持旧的用例作为你的问题。将 n_values='auto'
添加到代码中将消除此错误,如下所示:
onehotencoder = OneHotEncoder(categorical_features=[4], n_values='auto',
handle_unknown='ignore')
如果您从代码中删除 handle_unknown
参数,那么这也有效,但不应该这样做。
在此处查看此问题: