从Python数组中随机选取某几个元素

Randomly Select Elements from an Array in Python

In machine learning, we wanna have a larger dataset to train our model. However, we can hardly find an adequately large dataset in some cases. In this case, we can use K-fold cross validation to select a better model based on a small dataset.

There is another lazy way we can train our model. We randomly select several data from the set as the testing set, we use the rest part of the set as the training set to train our model.

For a given 2D input dataset and a 1D output dataset, say x_data and y_data respectively. For example we choose 5 points as the testing set, the Python code is:

test_index = np.random.choice(len(x_data), 5, replace = False)

x_test = x_data[test_index]
y_test = y_data[test_index]

train_index = np.arange(len(x_data))
train_index = np.delete(train_index, test_index)

x_train = x_data[train_index]
y_train = y_data[train_index]

In this case, we can use the training set and the testing set to train our model.

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值