如何为训练集和测试集获取虚拟变量?

我想为训练集和测试集的分类字段创建虚拟变量,然后仅在训练集和测试集中共有的特征上训练分类器。我运行了下面的代码来为两个数据集创建虚拟变量,但得到了TypeError错误。

我在Jupyter笔记本的一个单元格中输入了以下内容

def get_features(train, test):
    trainval = list(train.columns.values) # 列出训练集特征
    testval = list(test.columns.values) # 列出测试集特征
    features = list(set(trainval) & set(testval)) # 检查哪些特征是共有的(移除结果列)
    features.remove('Id') # 移除无用的ID列
    return features

def process_features(train,test):
    tables=[test,train]
    for table in tables:
        table['SoldDt']= table[['MoSold','YrSold']].apply(lambda x : '{}-{}'.format(x[0],x[1]), axis=1)
        table['YearBuilt']= pd.to_datetime(table.YearBuilt,format="%Y")
        table['YearRemodAdd']= pd.to_datetime(table.YearRemodAdd,format="%Y")
        table['SoldDt']= pd.to_datetime(table.SoldDt,format="%m-%Y")
        table.GarageYrBlt.fillna(1,inplace=True)
        table.GarageYrBlt=table.GarageYrBlt.apply(int)
        table.GarageYrBlt.replace(1,'NaT',inplace=True)
        table['GarageYrBlt']= pd.to_datetime(table.GarageYrBlt,format="%Y")
        del table['MoSold']
        del table['YrSold']
        table['MSSubClass']=table['MSSubClass'].apply(str)
        table['OverallQual']=table['OverallQual'].apply(str)
        table['OverallCond']=table['OverallCond'].apply(str)
        table.Alley.fillna("NotAvl",inplace=True)
        table.BsmtQual.fillna("NB",inplace=True)
        table.BsmtCond.fillna("NB",inplace=True)
        table.BsmtExposure.fillna("NB",inplace=True)
        table.BsmtFinType1.fillna("NB",inplace=True)
        table.BsmtFinType2.fillna("NB",inplace=True)
        table.FireplaceQu.fillna("NF",inplace=True)
        table.GarageType.fillna("NG",inplace=True)
        table.GarageFinish.fillna("NG",inplace=True)
        table.GarageQual.fillna("NG",inplace=True)
        table.GarageCond.fillna("NG",inplace=True)
        table.PoolQC.fillna("NP",inplace=True)
        table.Fence.fillna("NFe",inplace=True)
        table.MiscFeature.fillna("NotAvl",inplace=True)
        table.LotFrontage.fillna(0,inplace=True)
        table=table.dropna(inplace=True)
        table=pd.get_dummies(table)
    features = get_features(train,test)
    return train,test,features

然后,我在另一个单元格中调用了该函数

train = pd.read_csv('/mnt/disk2/Data/HousePrices/train.csv')
test = pd.read_csv('/mnt/disk2/Data/HousePrices/test.csv')
train,test,features = process_features(train,test)

我得到了以下错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-b2727d6cdc63> in <module>()
  1 train = pd.read_csv('/mnt/disk2/Data/HousePrices/train.csv')
  2 test = pd.read_csv('/mnt/disk2/Data/HousePrices/test.csv')
----> 3 train,test,features = process_features(train,test)

<ipython-input-16-dc47e5e9f9b6> in process_features(train, test)
  40 
  41         table=table.dropna(inplace=True)
---> 42         table=pd.get_dummies(table)
  43 
  44     print ("Getting features...")

/usr/local/lib/python3.5/dist-packages/pandas/core/reshape.py in get_dummies(data, prefix, prefix_sep, dummy_na, columns, sparse, drop_first)
  1102     else:
  1103         result = _get_dummies_1d(data, prefix, prefix_sep, dummy_na,
-> 1104                                  sparse=sparse, drop_first=drop_first)
  1105     return result
  1106 

/usr/local/lib/python3.5/dist-packages/pandas/core/reshape.py in _get_dummies_1d(data, prefix, prefix_sep, dummy_na, sparse, drop_first)
  1123     # if all NaN
  1124     if not dummy_na and len(levels) == 0:
-> 1125         return get_empty_Frame(data, sparse)
  1126     
  1127     codes = codes.copy()

/usr/local/lib/python3.5/dist-packages/pandas/core/reshape.py in get_empty_Frame(data, sparse)
  1115             index = data.index
  1116         else:
-> 1117             index = np.arange(len(data))
  1118         if not sparse:
  1119             return DataFrame(index=index)

TypeError: object of type 'NoneType' has no len()

回答:

在这一行

table=table.dropna(inplace=True)

dropna 返回了 None,因为文档中说明

inplace : boolean, default False    如果为True,则原地操作并返回None。

但随后你尝试将这个 None 值传递给 get_dummies()

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注