[機器學習]三行代碼快速劃分交叉訓練中訓練集和驗證集_機器學習

來源:互聯網
上載者:User
使用numpy.random.choice()和set()快速劃分交叉訓練資料集

之前在劃分訓練集和驗證集時,都是手工隨機產生index,很笨。

學到的新方法如下:

import numpy as np# 常態分佈產生未經處理資料x = np.random.random.normal(1,0.1,100)# 按8:2分割資料x_train_index = np.random.choice(len(x),round(len(x)*0.8),replace = False)x_valid_index = np.array(list(set(range(len(x))) - set(x_train_index)))x_train = x[x_train_index]x_valid = x[x_valid_index]
總結1: np.random.choice()

Definition : choice(a, size=None, replace=True, p=None)

Type : Function of None module

Parameters
a : 1-D array-like or int
If an ndarray, a random sample is generated from its elements. If an int, the random sample is generated as if a was np.arange(n)
size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.
replace : boolean, optional
Whether the sample is with or without replacement
是否包含重複元素
p : 1-D array-like, optional
The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.
按什麼機率分布選取元素,預設是均勻分布

Returns
samples : 1-D ndarray, shape (size,)
The generated random samples 總結2: set()

Python的集合(set)和其他語言類似, 是一個無序不重複元素集, 準系統包括關係測試和消除重複元素. 總結3: batch training

batch training 一樣可以使用這種方法選取資料

batch_size = 25for epoch in range(100):    rand_index = np.random.choice(len(x_train), size = batch_size)    rand_x = x_train[rand_index]    rand_y = y_train[rand_index]    ...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.