我正在尝试比较不同的算法,看看哪种算法最适合我的问题。
我直接尝试本教程中的代码:https ://machinelearningmastery.com/machine-learning-in-python-step-by-step/
特别是在下面的代码中:
import sys
import pandas as pd
import scipy as sp
import sklearn as sk
import numpy as np
import matplotlib.pyplot as plt
from pandas.plotting import scatter_matrix
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.linear_model import LogisticRegression
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
Run Code Online (Sandbox Code Playgroud)
import sys
import pandas as pd
import scipy as sp
import sklearn as sk
import …Run Code Online (Sandbox Code Playgroud) 如何将以下用 python 编写的代码写入 R ?
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)
Run Code Online (Sandbox Code Playgroud)
以 80/20 的比例拆分为训练集和测试集。
我想在以下数据帧上使用来自 sklearn 的 TimeSeriesSplit 来预测总和:

因此,为了准备 X 和 y,我执行以下操作:
X = df.drop(['sum'],axis=1)
y = df['sum']
Run Code Online (Sandbox Code Playgroud)
然后将这两个喂给:
for train_index, test_index in tscv.split(X):
X_train01, X_test01 = X[train_index], X[test_index]
y_train01, y_test01 = y[train_index], y[test_index]
Run Code Online (Sandbox Code Playgroud)
通过这样做,我收到以下错误:
KeyError: '[ 0 1 2 ...] not in index'
Run Code Online (Sandbox Code Playgroud)
这里 X 是一个数据帧,显然这会导致错误,因为如果我将 X 转换为数组,如下所示:
X = X.values
Run Code Online (Sandbox Code Playgroud)
然后它会起作用。但是,为了以后对模型的评估,我需要 X 作为数据框。有什么方法可以将 X 保留为数据帧并将其提供给 tscv 而不将其转换为数组?
time-series pandas scikit-learn sklearn-pandas train-test-split
我正在尝试使用 train_test_split 策略微调我的 sklearn 模型。我知道GridSearchCV执行参数调整的能力,但是,它与使用交叉验证策略相关,我想使用 train_test_split 策略进行参数搜索,因为训练速度对我的情况很重要,我更喜欢简单train_test_split通过交叉验证。
我可以尝试编写自己的 for 循环,但如果不利用 GridSearchCV 中使用的内置并行化,效率会很低。
有人知道如何利用 GridSearchCV 来实现这一点吗?或者提供一个不太慢的替代方案。