- 我有一个形状为(4601,58)的numpy矩阵。
- 我想基于行数的矩阵分裂随机按60%,20%,20%分
- 这是机器学习任务,我需要
- 有随机选取行的numpy的功能?
12
A
回答
17
可以使用numpy.random.shuffle
import numpy as np
N = 4601
data = np.arange(N*58).reshape(-1, 58)
np.random.shuffle(data)
a = data[:int(N*0.6)]
b = data[int(N*0.6):int(N*0.8)]
c = data[int(N*0.8):]
3
如果你想随机选择行,你可以只使用random.sample
从标准Python库:
import random
population = range(4601) # Your number of rows
choice = random.sample(population, k) # k being the number of samples you require
random.sample
样品无需更换,所以你不必担心重复行结束了在choice
。给定一个叫做matrix
的数组,可以通过切片来选择行,如下所示:matrix[choice]
。
当然,k
可以等于总体中元素的总数,然后choice
将包含行的索引的随机排序。那么你可以根据你的需要划分choice
,如果这就是你需要的。
7
一种补充HYRY的答案,如果你想一直洗牌多个阵列X,Y,Z,使用相同的第一维:x.shape[0] == y.shape[0] == z.shape[0] == n_samples
。
你可以这样做:
rng = np.random.RandomState(42) # reproducible results with a fixed seed
indices = np.arange(n_samples)
rng.shuffle(indices)
x_shuffled = x[indices]
y_shuffled = y[indices]
z_shuffled = z[indices]
然后每个洗牌阵列的分裂在HYRY的回答进行。
1
既然你需要它的机器学习,这里是我写的一个方法:
import numpy as np
def split_random(matrix, percent_train=70, percent_test=15):
"""
Splits matrix data into randomly ordered sets
grouped by provided percentages.
Usage:
rows = 100
columns = 2
matrix = np.random.rand(rows, columns)
training, testing, validation = \
split_random(matrix, percent_train=80, percent_test=10)
percent_validation 10
training (80, 2)
testing (10, 2)
validation (10, 2)
Returns:
- training_data: percentage_train e.g. 70%
- testing_data: percent_test e.g. 15%
- validation_data: reminder from 100% e.g. 15%
Created by Uki D. Lucas on Feb. 4, 2017
"""
percent_validation = 100 - percent_train - percent_test
if percent_validation < 0:
print("Make sure that the provided sum of " + \
"training and testing percentages is equal, " + \
"or less than 100%.")
percent_validation = 0
else:
print("percent_validation", percent_validation)
#print(matrix)
rows = matrix.shape[0]
np.random.shuffle(matrix)
end_training = int(rows*percent_train/100)
end_testing = end_training + int((rows * percent_test/100))
training = matrix[:end_training]
testing = matrix[end_training:end_testing]
validation = matrix[end_testing:]
return training, testing, validation
# TEST:
rows = 100
columns = 2
matrix = np.random.rand(rows, columns)
training, testing, validation = split_random(matrix, percent_train=80, percent_test=10)
print("training",training.shape)
print("testing",testing.shape)
print("validation",validation.shape)
print(split_random.__doc__)
- 训练(80,2)
- 测试(10,2)
- 验证(10, 2)
相关问题
- 1. 分矩阵为n个矩阵
- 2. 生成一个随机矩阵
- 3. 将矩阵分成更小的矩阵
- 4. 将矩阵分成子组矩阵
- 5. 如何生成一个新的随机数并转置矩阵?
- 6. 如何生成对称随机矩阵?
- 7. 制作一个随机数字矩阵
- 8. NumPy的添加矩阵到另一个矩阵
- 9. 生成一个具有N个非零元素的随机稀疏矩阵
- 10. 如何使用Python生成随机N维旋转矩阵?
- 11. 排列置换一个numpy的阵列的成ndarray或矩阵
- 12. Python numpy矩阵乘以一个对角矩阵
- 13. 使用另一个矩阵对NumPy矩阵进行子集
- 14. 把一个字符矩阵转换成一个数字矩阵
- 15. 用numpy中的另一个小矩阵替换矩阵的子部分
- 16. 如何将一个矩阵乘以另一个矩阵以返回一个矩阵
- 17. 如何用openMaya将矩阵乘以另一个矩阵?
- 18. 如何使用AMPL中的正态分布生成一个随机数矩阵?
- 19. 如何在Java中显示N×N随机数矩阵?
- 20. 从矩阵n×m个
- 21. 得到两个一维numpy的矩阵
- 22. 如何结合两个python矩阵numpy
- 23. R中的随机生成矩阵
- 24. Python Numpy - 附加三个阵列形成矩阵或3D阵列
- 25. 整数矩阵到随机矩阵归一化
- 26. 如何矩阵矩阵转换(N)(1,N)基体在python
- 27. 如何通过n矩阵每n由M矩阵
- 28. deteminant N * N矩阵
- 29. 将分区矩阵分成N个大小相同的块(块)R
- 30. 如何在numpy矩阵的每一行获得N个最大值的索引矩阵?