【问题标题】:Rerunning a prediction on the whole dataset对整个数据集重新运行预测
【发布时间】:2021-12-06 22:21:47
【问题描述】:

我想根据以下数据集预测Decision:

     userId        itemId      Decision
  0      u1            i1             0
  1      u1            i2             1
  2      u2            i1             1
  3      u2            i3             0
  4      u2            i4             1
  5      u3            i5             0
    ...

我做了以下,我对结果很满意:

import numpy as np
from surprise import KNNWithMeans, Dataset, Reader
from surprise.model_selection import train_test_split

reader = Reader(rating_scale=(0, 1))
data = Dataset.load_from_df(df_2[['userId', 'itenId', 'Decision']], reader)
trainset, testset = train_test_split(data, test_size=0.25)
algo = KNNWithMeans()
algo.fit(trainset)
test = algo.test(testset)
test = pd.DataFrame(test)
test.drop("details", inplace=True, axis=1)
test.columns = ['userId', 'itemId', 'actual', 'cf_predictions']

所以test 看起来像这样:

test = pd.DataFrame({'userId': ['u3', 'u3', 'u4', 'u4', 'u5', 'u5'],
                     'itemId': ['i5', 'i6', 'i1', 'i3', 'i4', 'i5'],
                     'actual': [0, 1, 1, 0, 1, 0],
                     'cf_predictions': [0.05, 0.66, 0.99, 0.04, 0.98, 0.06]})

我什至把它们四舍五入了!

def g(row):
    if row['cf_predictions'] > 0.5:
        val = 1.0
    else:
        val = 0.0
    return val

test['cf_threshold'] = test.apply(g, axis=1)

但我想在整个数据集上重新运行模型,而不仅仅是在 testset 上。我是否“连接”trainset 和 testset?如果我输入trainset,它会给我<surprise.trainset.Trainset at 0x1593f42c550>

【问题讨论】:

    标签: python pandas dataframe recommendation-engine


    【解决方案1】:

    您可以使用预测所有数据

    prediction_data = data.drop('Decision', axis=1)  
    preds = algo.predict(prediction_data)
    

    【讨论】:

    • 愚蠢的问题,但您的意思是从原始数据框(在我的代码中命名为 df_2)还是从上面代码中定义的 data 中删除 Decision 列?因为data 不是数据框,是吗?
    • 朋友没有愚蠢的问题。但是是的,数据是一个数据框,您可以查看文档surprise.readthedocs.io/en/stable/dataset.html,但我没有使用 inplace = True 这意味着我制作了没有决策列的数据副本。
    • 如果我从数据框中删除它,我会得到TypeError: predict() missing 1 required positional argument: 'iid'。如果我从data 中删除它,我会得到AttributeError: 'DatasetAutoFolds' object has no attribute 'drop'。只是这个库的工作方式与普通的分类模型有点不同......
    • 说真的,我没有找到可以帮助您的功能。但我找到了一个不专业的解决方案train_test_split(data, test_size=0.99),测试大小为 0.99 %。但我不知道这个问题的解决方案。你测试这个功能github.com/NicolasHug/Surprise/issues/193和这个链接github.com/Microsoft/Recommenders/blob/…
    • @sknon7 谢谢,这实际上不是一个坏主意哈哈。但是,它将训练什么?我现在要做的是将我的数据框df_2 转换为元组列表,就像我的testset 表示为一样。但它很大,我什至没有足够的内存哈哈。
    猜你喜欢
    • 2020-12-07
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    相关资源
    最近更新 更多