python-3.x - python scikit-learn 中的分层采样

标签 python-3.x machine-learning scikit-learn

我想使用分层采样(scikitlearn)将我的数据集分为训练集和测试集。我的方法如下:

1)我正在读取 CSV 文件并使用 pandas readCSV 加载它。所以最终我将加载的 csv 存储在名为“数据集”的数据帧中

         dataset = pd.readCSV('CSV_NAME)

2)现在我应用分层抽样:

         train,test = train_test_split(dataset,test_size=0.20,stratify=True)

但它抛出以下错误:

TypeError: Singleton array array(True, dtype=bool) cannot be considered a valid collection.

所以请建议我正确的做法。

最佳答案

'train_test_split'需要知道目标变量是什么。因此,您应该将您的调用更改为:

train,test = train_test_split(dataset[needed columns], dataset.target,test_size=0.20,stratify=True)

关于python-3.x - python scikit-learn 中的分层采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45706785/

相关文章:

python - 字符串格式为 base64 到 base64 对象

python - 加载和预测新数据 sklearn

python - 如何处理函数的不同返回值

python-3.x - 通过将网格放置在 st.container、st.column 或 st.empty 中来控制 Streamlit st_aggrid (AgGrid) 布局

algorithm - 指定执行自动学习的算法

python - 如何提高分类机器学习模型的真阳性率?

python - 随机森林回归的残差(Python)

python - tensorflow 使用混淆矩阵进行评估

machine-learning - 回归问题的降维/降噪技术

python - 如何用正斜杠连接文件夹和文件名?