我正在尝试使用自举来获取模型的MSE的平均估算值。我正在尝试按照以下指南进行操作:https : //machinelearningmastery.com/a-gentle-introduction-to-the-bootstrap-method/这是我的代码:
models_mse = [None]*100
for i in range(100):
boot = resample(X, replace = True, n_samples = math.floor(len(X) / 2),random_state = 1)
y_new = [y for y in X if y not in boot]
X_train, X_test, y_train, y_test = train_test_split(boot, y_new, test_size=0.1, random_state=42)
reg = LogisticRegression()
reg.fit(X_train, y_train)
MSE = mean_squared_error(y_test, reg.predict(X_test))
models_mse.append(MSE)
这会产生错误:
ValueError: Found input variables with inconsistent numbers of samples: [497, 0]
含义,y_new
是空的。根据我对自举的理解,我们将样本视为“种群”,并用该伪种群的替换样本进行重新采样,以生成更多样本以估计伪种群的参数,即我的情况。在这里,我有一些X
使用onehot的编码数据。看起来像这样:
array([[1., 0., 0., ..., 0., 0., 1.],
[1., 0., 1., ..., 1., 0., 0.],
[1., 0., 1., ..., 0., 0., 1.],
...,
[0., 1., 0., ..., 0., 0., 1.],
[0., 1., 0., ..., 1., 0., 0.],
[0., 1., 0., ..., 1., 0., 0.]])
并且是一个numpy数组:
>>>type(X)
numpy.ndarray
我的问题如下:y_new
当我观察到boot
和之间的差异时,为什么为空X
?此外,我任意决定将原始样本的一半作为样本。自举时,是否有更系统的方法来选择样本数量?最后,总体而言,我的设置是否正确解决了我要解决的问题?
谢谢。
编辑:从用户评论进行一些修订后,我的新代码是:
models_mse = [None]*100
for i in range(100):
boot = resample(X_train, replace = True, n_samples = X_train.shape[0],random_state = 1)
reg = LogisticRegression()
reg.fit(boot, y_train)
MSE = mean_squared_error(y_test, reg.predict(X_test))
models_mse.append(MSE)
不会抛出任何错误。但是,每个模型的MSE都是完全相同的,这很奇怪,boot
因为每次迭代都应该不同吗?
在您的代码y_new
中选择X
:
y_new = [y for y in X if y not in boot]
您可能想选择X
。但是它仍然无法正常工作,因为您无法in
对numpy数组进行操作。就像这篇文章所说,resample
API并不能为您提供测试集的现成观察结果。但是,好处是我们从API中获得的想要实现非常简单。同样,您可能不想在采样时每次都使用相同的种子(随机状态)。
models_mse=[]
for _ in range(100):
train_idx = np.random.randint(0,len(X),size=(len(X),))
test_idx = np.array([i for i in range(len(X)) if i not in train_idx])
X_train, Y_train, X_test, Y_test = X[train_idx], Y[train_idx], X[test_idx],Y[test_idx]
model = LogisticRegression()
model.fit(X_train, Y_train)
Y_pred = model.predict(X_test)
mse = MSE(Y_test, Y_pred) # replace by appropriate API/function
models_mse.append(mse)
print("Bootstrapped MSE={}".format(sum(models_mse)/100))
我使用的火车集大小与原始数据集的大小相同X
,这是我通常所做的。您可以根据需要进行更改。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句