【问题标题】:Pandas questions: Label Encoder and splitting columns to provide datasets with labelsPandas 问题:标签编码器和拆分列以提供带有标签的数据集
【发布时间】: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']

我的问题:

  1. 我正在尝试将第 0-5 列拆分为数据集,将第 6 列(性别)拆分为标签。为什么我在尝试打印 X 和 Y 时收到 TypeError: unhashable type

  2. 即使在使用将文本值转换为数字映射的LabelEncoder 之后,当我打印“数字映射后”时,它如何打印实际标签?

谢谢

【问题讨论】:

    标签: python pandas scikit-learn


    【解决方案1】:

    好的,我想多了 - 这很好 - 列名上的简单拆分 将熊猫导入为 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))
    

    【讨论】:

      猜你喜欢
      • 2020-04-02
      • 2011-06-10
      • 2020-11-25
      • 1970-01-01
      • 2018-02-03
      • 2020-06-04
      • 2017-11-14
      • 2022-01-15
      • 2021-05-09
      相关资源
      最近更新 更多