【问题标题】:How can I specify a training set and test set from separate dataframes?如何从不同的数据框中指定训练集和测试集?
【发布时间】:2020-12-31 00:11:25
【问题描述】:
我有一个数据框,其中包含新闻文章和 Facebook 帖子(全文)以及相应的标签(所有文本的一组标签 - 文章和帖子)。但是,我想在两种类型的文本(文章和帖子)上训练我的分类器,但我的测试集中只有 facebook 帖子。是否有指定一组行(按“源”列分组)从中提取测试集?
我正在使用
sklearn.model_selection import train_test_split
和用于分类模型的简单转换器。
谢谢!
【问题讨论】:
标签:
python
pandas
scikit-learn
classification
training-data
【解决方案1】:
拆分是通过以下方式完成的:
# create X
X = df[<columns>]
# create y
y = df[<one column>]
# split to train and test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=123, stratify = y)
如果你有两个数据框,你需要先合并它们:
df = df1.append(df2)