【发布时间】:2017-07-15 03:42:54
【问题描述】:
我刚刚开始尝试使用 pandas 和 scikit 进行数据分析。我的测试集是NHSTA's open crash dataset——我现在的目标是做一个简单的随机森林分类,根据其他参数预测司机的性别(我现在不关注准确性——我想让事情先运行)
我的代码:
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']
我的问题:
我正在尝试将第 0-5 列拆分为数据集,将第 6 列(性别)拆分为标签。为什么我在尝试打印 X 和 Y 时收到
TypeError: unhashable type?即使在使用将文本值转换为数字映射的
LabelEncoder之后,当我打印“数字映射后”时,它如何打印实际标签?
谢谢
【问题讨论】:
标签: python pandas scikit-learn