Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python fastai表格模型-如何获得新数据的预测?_Python_Tabular_Fast Ai - Fatal编程技术网

Python fastai表格模型-如何获得新数据的预测?

Python fastai表格模型-如何获得新数据的预测?,python,tabular,fast-ai,Python,Tabular,Fast Ai,我使用的是kaggle房价数据集,它分为:训练和测试 我用fastai Tablear和train set建立了一个模型 如何预测测试数据集的值 我知道这听起来很简单,大多数其他LIB都会像model.predict(test)那样做,但这里的情况并非如此。 我搜索了fastai论坛和其他文档。关于这个问题有很多话题,其中大多数要么没有答案,要么是过时的解决方法(因为fastai2最近发布,现在被称为fastai) a。model.predict仅适用于单行,循环测试不是最优的。它非常慢 b

我使用的是kaggle房价数据集,它分为:训练和测试

  • 我用fastai Tablear和train set建立了一个模型
  • 如何预测测试数据集的值
我知道这听起来很简单,大多数其他LIB都会像model.predict(test)那样做,但这里的情况并非如此。 我搜索了fastai论坛和其他文档。关于这个问题有很多话题,其中大多数要么没有答案,要么是过时的解决方法(因为fastai2最近发布,现在被称为fastai)

a。model.predict仅适用于单行,循环测试不是最优的。它非常慢

b。model.get_preds为您培训的数据提供结果


请建议您可以使用经过培训的学习者预测表格数据的新df。

模型。get_preds
用于对未看到的数据进行批量预测。您只需要对这个新数据应用与对培训数据相同的转换

dl = model.dls.test_dl(test_data, bs=64) # apply transforms
preds,  _ = model.get_preds(dl=dl) # get prediction

fastai的论坛非常活跃,您可能会得到库开发人员的响应,所以以后也可以尝试一下。

我发现了一个问题。对于未来的读者-为什么你不能为新的df工作

(对卡格尔的房价进行了测试)

问题的根源在于明确的不一致。如果你用一组cat特征训练你的模型,说颜色=红、绿、蓝;您的新df有颜色:红色、绿色、蓝色、黑色-它将抛出一个错误,因为它不知道如何处理新类(黑色)。更不用说到处都需要相同的列,这可能很棘手,因为如果像我一样使用fillmissing proc,它会为cat值创建新的col(无论是否缺少)。所以你需要对猫身上的NAN进行三次检查。 我真的很想让fastai从头到尾都发挥作用:

列车/测试列相同,只有列车有一个额外目标。在这一点上,有些猫科动物有不同的类。我只是决定将它们结合起来(JU使其工作),但它不会导致泄漏吗

combined = pd.concat([train, test]) # test will have nans at target, but we don't care
cont_cols, cat_cols = cont_cat_split(combined, max_card=50)
combined = combined[cat_cols]
我们在做一些调整

train[cont_cols] = train[cont_cols].astype('float') # if target is not float, there will be an error later
test[cont_cols[:-1]] = test[cont_cols[:-1]].astype('float'); # slice target off (I had mine at the end of cont_cols)
到了表格式的熊猫那里

procs = [Categorify, FillMissing]

to = TabularPandas(combined,
                   procs = procs,
                   cat_names = cat_cols)

train_to_cat = to.items.iloc[:train.shape[0], :] # transformed cat for train
test_to_cat = to.items.iloc[train.shape[0]:, :] # transformed cat for test. Need to separate them
to.items将为我们提供转换的cat列。在那之后,我们需要把所有的东西重新组装起来

train_imp = pd.concat([train_to_cat, train[cont_cols]], 1) # assemble new cat and old cont together
test_imp = pd.concat([test_to_cat, test[cont_cols[:-1]]], 1) # exclude SalePrice

train_imp['SalePrice'] = np.log(train_imp['SalePrice']) # metric for kaggle
之后,我们按照fastai教程进行操作

dep_var = 'SalePrice'
procs = [Categorify, FillMissing, Normalize]
splits = RandomSplitter(valid_pct=0.2)(range_of(train_imp))

to = TabularPandas(train_imp, 
                   procs = procs,
                   cat_names = cat_cols,
                   cont_names = cont_cols[:-1], # we need to exclude target
                   y_names = 'SalePrice',
                   splits=splits)

dls = to.dataloaders(bs=64)

learn = tabular_learner(dls, n_out=1, loss_func=F.mse_loss)
learn.lr_find()

learn.fit_one_cycle(20, slice(1e-2, 1e-1), cbs=[ShowGraphCallback()])
在这一点上,我们有一个学习者,但仍然无法预测。我想在我们这样做之后:

dl = learn.dls.test_dl(test_imp, bs=64)
preds, _ = learn.get_preds(dl=dl) # get prediction
它只会工作(cont值和predict的预处理),但不会。它不会填充。 所以,只要在测试中找到并填写NAN即可:

missing = test_imp.isnull().sum().sort_values(ascending=False).head(12).index.tolist()
for c in missing:
    test_imp[c] = test_imp[c].fillna(test_imp[c].median())
在此之后,我们最终可以预测:

dl = learn.dls.test_dl(test_imp, bs=64)
preds, _ = learn.get_preds(dl=dl) # get prediction

final_preds = np.exp(preds.flatten()).tolist()

sub = pd.read_csv('../input/house-prices-advanced-regression-techniques/sample_submission.csv')
sub.SalePrice = final_preds

filename = 'submission.csv'
sub.to_csv(filename, index=False)
对于冗长的叙述我深表歉意,但我对编码还比较陌生,这个问题很难指出。关于如何在线解决它的信息很少。简言之,这是一种痛苦

不幸的是,这仍然是一个解决问题的办法。如果测试中任何特性中的类的数量不同,它将崩溃。同样奇怪的是,它在测试dls时并没有填充NA


如果您有任何愿意分享的建议,请告诉我。

我先问了,但他们还没有回答。我以前尝试过你的方法,给出了错误:self中的索引超出了范围我以前能够组装dl,但是第二行的问题。嗯,上传你尝试过的代码和你在数据样本中得到的错误。这将有助于缩小这个问题的范围。