indexing - 如何在训练/测试拆分后使用交叉验证验证
问题描述
在将数据拆分为训练和测试后,我在训练集上使用了 K-cross 验证。但这给出了一个错误,我认为这是由于训练和测试拆分后的索引。下面是我使用的代码。如何在火车/火车拆分后重置索引或处理此错误的任何其他建议将不胜感激。我已经尝试过 df.reset_index() 但这给出了一个错误 AttributeError: 'numpy.ndarray' object has no attribute 'reset_index'。谢谢你。
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=99)
# k-fold cross validation
scores = list()
kfold = KFold(n_splits=10, shuffle=True)
# enumerate splits
for train_ix, test_ix in kfold.split(X_train):
train_X, test_X = X_train[train_ix], X_train[test_ix]
train_y, test_y = y_train[train_ix], y_train[test_ix]
# fit model
model = LinearRegression()
model.fit(train_X, train_y)
# evaluate model
yhat = model.predict(test_X)
score = np.sqrt(metrics.mean_absolute_error(yhat, test_y))
print('Fold score : {}'.format(score))
KeyError: "Passing list-likes to .loc or [] with any missing labels is no longer supported. The following labels were missing: Int64Index([ 3, 9, 10, 17, 19,\n ...\n 41050, 41056, 41060, 41101, 41120],\n dtype='int64', length=3708).
解决方案
推荐阅读
- types - 你能在 Coq 中枚举所有归纳类型的值吗?
- java - 尝试使用 Firebase MLKit 条码扫描器,模拟器屏幕出现“等待条码检测模块下载,请稍候”
- java - JAVA EMAIL API 上的错误(无法解析 javax.mail.Authenticator 类型。它是从所需的 .class 文件中间接引用的)
- c - 如何使用leaks 命令行工具查找内存泄漏?
- python - 在 Sublime 编辑器中出现错误:Python 代码
- c++ - 如何理解赋值表达式“a=(b=4)=3”
- javascript - 如何比排序更快地找到数组的最大子集?
- perl - 生成公钥/私钥并在 Perl 中使用 Crypt::OpenSSL::RSA 对其进行签名
- python - ** 在 Python 中是什么意思?
- swift - SwiftUI 向动画文本添加不必要的“dot dot dot”