使用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] ...