pandas 尝试使用 OneHot Encoder Python 的预处理错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43958518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:35:51  来源:igfitidea点击:

preprocessing error trying to use OneHot Encoder Python

python-2.7pandasnumpyscikit-learn

提问by user3476463

I'm trying to run the code below in virtual machine for a homework practice problem. I'm getting the error message below, and I'm trying to figure out if it's an issue with my code or the site. If anyone can point out if it's an error with my code an how to fix it I'd be grateful. If my code looks ok, then I'll let the course know they have a bug.

我正在尝试在虚拟机中运行以下代码以解决家庭作业练习问题。我收到下面的错误消息,我想弄清楚这是我的代码还是网站的问题。如果有人能指出我的代码是否有错误以及如何修复它,我将不胜感激。如果我的代码看起来没问题,那么我会让课程知道他们有一个错误。

Code:



import numpy as np
import pandas as pd

# Load the dataset
X = pd.read_csv('titanic_data.csv')
# Limit to categorical data
X = X.select_dtypes(include=[object])

from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder

# TODO: Create a LabelEncoder object, which will turn all labels present in
#       in each feature to numbers.
# HINT: Use LabelEncoder()

df=pd.DataFrame(X)

le = preprocessing.labelEncoder()


# TODO: For each feature in X, apply the LabelEncoder's fit_transform
#       function, which will first learn the labels for the feature (fit)
#       and then change the labels to numbers (transform).

df2=df.apply(le.fit_transform)


#for feature in X:
    # HINT: use fit_transform on X[feature] using the LabelEncoder() object
    #X[feature] = label_encoder.fit_transform(X[feature])

# TODO: Create a OneHotEncoder object, which will create a feature for each
#       label present in the data.
# HINT: Use OneHotEncoder()
ohe = preprocessing.OneHotEncoder()

# TODO: Apply the OneHotEncoder's fit_transform function to all of X, which will
#       first learn of all the (now numerical) labels in the data (fit), and then
#       change the data to one-hot encoded entries (transform).

# HINT: Use fit_transform on X using the OneHotEncoder() object

onehotlabels = enc.fit_transform(df2)


Error:

Traceback (most recent call last):
  File "vm_main.py", line 33, in <module>
    import main
  File "/tmp/vmuser_zrkfroofmi/main.py", line 2, in <module>
    import studentMain
  File "/tmp/vmuser_zrkfroofmi/studentMain.py", line 3, in <module>
    import OneHot
  File "/tmp/vmuser_zrkfroofmi/OneHot.py", line 21, in <module>
    le = preprocessing.labelEncoder()
NameError: name 'preprocessing' is not defined

回答by Giovanni Bruner

Call OneHotEncoder without preprocessing before the name. So just do ohe = OneHotEncoder(). The problem is in your import, what you have in your script would work if you did from sklearn import preprocessing.

在名称之前调用 OneHotEncoder 而不进行预处理。所以就做吧ohe = OneHotEncoder()。问题在于您的导入,如果您执行了from sklearn import preprocessing.

回答by gii96

From the code, 'from sklearn.preprocessing import OneHotEncoder', user was trying to import OneHotEncoder from preprocessing without importing preprocessing first. Preprocessing is a package from sklearn and OneHotEncoder is a preprocessor. That caused the error. So import preprocessing and then try with OneHotEncoder

从代码“from sklearn.preprocessing import OneHotEncoder”中,用户试图从预处理中导入 OneHotEncoder 而没有先导入预处理。Preprocessing 是 sklearn 的一个包,OneHotEncoder 是一个预处理器。这导致了错误。所以导入预处理然后尝试使用 OneHotEncoder