2017-07-14 9 views
0

データアナリティクスにpandasとscikitを使用しようとしています。私のテストセットはNHSTA's open crash datasetです - 今私の目標は、他のパラメータに基づいてドライバの性別を予測する簡単なRandomForest分類を行うことです(私は今正確性に焦点を当てていません - 最初に物事を走らせたいです)。パンダの質問:データセットにラベルを付けるためのラベルエンコーダと分割カラム

マイコード:

import pandas as pd 
import matplotlib.pyplot as plt 
import numpy as np 
from sklearn.cluster import KMeans 
from sklearn.decomposition import PCA 
from sklearn.preprocessing import LabelEncoder 
from sklearn.preprocessing import StandardScaler 
from sklearn.model_selection import train_test_split 


crashes = pd.read_csv("crashes.csv", nrows=100000) 


crashes.drop("Case Individual ID", axis=1, inplace = True) 
crashes.drop("Case Vehicle ID", axis=1, inplace = True) 
crashes.drop("Transported By", axis=1, inplace = True) 
crashes.drop("Injury Descriptor", axis=1, inplace = True) 
crashes.drop("Injury Location", axis=1, inplace = True) 

crashes = crashes [pd.notnull(crashes['Age'])] 
crashes = crashes[crashes.Age >= 10 ] 

le = LabelEncoder() 
crashes = crashes[crashes.columns[:]].apply(le.fit_transform) 
crashes = crashes._get_numeric_data() 

crashes_train, crashes_test = train_test_split(crashes, test_size = 0.2) 

print "After numeric mapping:",list(crashes_train) 

X = crashes_train[:,[0,1,2,3,4,5]] 
Y = crashes_train[:,[6]] 
print "X=",list (X) #error 
print "Y=",list (Y) #error 

データ列:

After numeric mapping: ['Year', 'Victim Status', 'Role Type', 'Seating Position', 'Ejection', 'License State Code', 'Sex', 'Safety Equipment', 'Injury Severity', 'Age'] 

私の質問:

  1. 私はカラム0-5をデータセットに、カラム6(Sex)をラベルに分割しようとしています。 XとYを印刷しようとしたときに、なぜTypeError: unhashable typeが表示されるのですか?

  2. テキスト値を数値マッピングに変換するLabelEncoderを使用した後でも、「数値マッピング後」を印刷すると、実際のラベルが印刷されます。

おかげ

答えて

0

さて、私は思考を超えていた - これはよく働いた - 簡単な分割を列名 輸入パンダにPD

import matplotlib.pyplot as plt 
import numpy as np 
from sklearn.cluster import KMeans 
from sklearn.decomposition import PCA 
from sklearn.preprocessing import LabelEncoder 
from sklearn.preprocessing import StandardScaler 
from sklearn.model_selection import train_test_split 
from sklearn.ensemble import RandomForestClassifier 
#from sklearn.cross_validation import train_test_split 

crashes = pd.read_csv("crashes.csv", nrows=100000) 


crashes.drop("Case Individual ID", axis=1, inplace = True) 
crashes.drop("Case Vehicle ID", axis=1, inplace = True) 
crashes.drop("Transported By", axis=1, inplace = True) 
crashes.drop("Injury Descriptor", axis=1, inplace = True) 
crashes.drop("Injury Location", axis=1, inplace = True) 

crashes = crashes [pd.notnull(crashes['Age'])] 
crashes = crashes[crashes.Age >= 10 ] 

le = LabelEncoder() 
crashes = crashes[crashes.columns[:]].apply(le.fit_transform) 
crashes = crashes._get_numeric_data() 

crashes_train, crashes_test = train_test_split(crashes, test_size = 0.2) 



print "After numeric mapping:",list(crashes_train) 

#X = crashes_train.set_index['Year', 'Victim Status', 'Role Type', 'Seating Position', 'Ejection', 'License State Code'] 
#Y = crashes_train.set_index['Sex'] 

Y = crashes_train[['Age', 'Year']] 
X = crashes_train[['Year', 'Victim Status', 'Role Type', 'Seating Position', 'Ejection', 'License State Code']] 
names = crashes_train.columns.values 

print "X=",list (X) 
print "Y=",list (Y) 

rfc = RandomForestClassifier() 
rfc.fit(X, Y) 
print("Features sorted by their score:") 
print(sorted(zip(map(lambda x: round(x, 4), rfc.feature_importances_), names), reverse=True)) 
として