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

时间:2017-05-13 22:20:55

标签: python-2.7 pandas numpy scikit-learn

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

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

2 个答案:

答案 0 :(得分:0)

在名称之前调用OneHotEncoder而不进行预处理。所以只需ohe = OneHotEncoder()。问题在于导入,如果您执行from sklearn import preprocessing,则脚本中的内容将起作用。

答案 1 :(得分:0)

从代码“从sklearn.preprocessing import OneHotEncoder”开始,用户试图从预处理中导入OneHotEncoder,而没有先导入预处理。预处理是sklearn的软件包,而OneHotEncoder是预处理器。那导致了错误。因此,导入预处理,然后尝试使用OneHotEncoder