2017-05-13 8 views
0

私は宿題の練習問題のために仮想マシンで以下のコードを実行しようとしています。下のエラーメッセージが表示され、コードやサイトに問題があるかどうかを判断しようとしています。誰かが私のコードでエラーが発生した場合、それを修正する方法を教えていただければ幸いです。私のコードが大丈夫ならば、コースにバグがあることを知らせます。OneHot Encoder Pythonを使用しようとする前処理エラー

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 
+0

'le = preprocessing.labelEncoder()'は、labelEncoderをインポートする際に 'le = labelEncoder()'にする必要があります。 'ohe = preprocessing.OneHotEncoder()'と同じです。 – ayhan

答えて

0

名前の前に前処理を行わずにOneHotEncoderを呼び出します。だからちょうどohe = OneHotEncoder()をしてください。あなたのインポートに問題があります。from sklearn import preprocessingを実行した場合、あなたのスクリプト内にあるものは動作します。

関連する問題