python - Scikit train_test_split 按指数

标签 python pandas scikit-learn scipy classification

我有一个按日期索引的 pandas 数据框。让我们假设它是从 1 月 1 日到 1 月 30 日。我想将此数据集拆分为 X_train、X_test、y_train、y_test,但我不想混合日期,所以我希望将火车和测试样本除以某个日期(或索引)。我在努力

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

但是当我检查这些值时,我发现日期是混合的。我想将我的数据拆分为:

Jan-1 到 Jan-24 进行训练,Jan-25 到 Jan-30 进行测试(因为 test_size 是 0.2,所以 24 需要训练,6 需要测试)

我该怎么做?谢谢

最佳答案

你应该使用

X_train, X_test, y_train, y_test = train_test_split(X,Y, shuffle=False, test_size=0.2, stratify=None)

不要使用 random_state=None 它会占用 numpy.random

here它提到使用 shuffle=Falsestratify=None

关于python - Scikit train_test_split 按指数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53648699/

相关文章:

python - 如何用Python确定拟合参数的不确定性?

Python - 创建 2 个新列,其中包含多个行值的第 25 个和第 75 个百分位

python - scikit-learn fit() 在规范化数据后导致错误

machine-learning - 如何在弹性网络回归模型中添加有关预测变量的先验知识?

python - 在 2 个单独的变量中同时获取 Floor Division 和 Remainder

python - FTP 下载,带有显示当前下载状态的文本标签

python - 类型错误 : Unsupported type <class 'numpy.dtype' > in write()

python - SciKit Gradient Boosting - 如何将预测与初始表结合起来?

python - 根据字符串模式从列表创建列表

Python:使用 pandas 导入 csv。尝试绘制一列,但出现错误,提示 "no numerical data to plot"