分割机器学习的数据

发布于 2025-02-10 11:44:37 字数 109 浏览 0 评论 0 原文

我有一个包含文本的数据框,我想根据机器学习过程的“作者”列将数据分开。例如,我想培训来自Aeschylus和Sophocles的文字,并测试Euripides的文字。我该怎么做?我正在使用Sklearn。

I have a dataframe that includes texts and i want to split the data according to the "writer" column for the machine learning process. For example, I want to train with texts from Aeschylus and Sophocles and test with the texts from Euripides. How can I do that? I am using sklearn.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟的代价 2025-02-17 11:44:38

尝试根据以下方式调整代码:

authors = ["Aesch", "Soph", "Euri", "Aesch", "Soph", "Euri"]
df = pd.DataFrame(authors, columns=["author"])
df["text"] = ["abc", "bcd", "cde", "abc", "bcd", "cde"]

# split your dataframe with a condition
train = df[df.author!="Euri"]
test = df[df.author=="Euri"]

Try to adapt your code according to this:

authors = ["Aesch", "Soph", "Euri", "Aesch", "Soph", "Euri"]
df = pd.DataFrame(authors, columns=["author"])
df["text"] = ["abc", "bcd", "cde", "abc", "bcd", "cde"]

# split your dataframe with a condition
train = df[df.author!="Euri"]
test = df[df.author=="Euri"]
凡间太子 2025-02-17 11:44:38

这就是GroupKfold的目的,它将组列还为特征和目标:

group_kfold = GroupKFold(n_splits=2)
X_train, X_test, y_train, y_test = group_kfold.split(X, y, group)

请参见文档:此处:

That's what GroupKFold is for, it takes the group column additionally to features and target:

group_kfold = GroupKFold(n_splits=2)
X_train, X_test, y_train, y_test = group_kfold.split(X, y, group)

See documentation here:
https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GroupKFold.html#sklearn.model_selection.GroupKFold

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文