使用Python进行有放回抽样

X, y = load_data(return_X_y=True)X_train, X_test, y_train, y_test = train_test_split(X,y,                                                test_size=0.3, random_state=123,                                            shuffle=True)def sample(X,y):# 尝试的代码 train_index = sorted(np.random.permutation(len(X))) test_index = [i for i in range(0,len(X)) if i not in train_index]for i in range(0,len(X)):# 训练集和验证集的分割    x_trn, x_val = X[train_index], X[test_index]    y_trn, y_val = y[train_inex], y[test_index]return x_trn , x_val, y_trn, y_val

然后调用它如下

sample(X_train, y_train)

填写代码以从训练数据中均匀地进行有放回抽样。抽样数据集的大小应等于训练数据集的大小。


回答:

根据作业的描述,这就是你所需要的。你随机选择索引,然后返回这些索引处的点。

def sample(X,y):    picks = np.random.randint(0,len(X),len(X))    return X[picks], y[picks]

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注