python - 以不完全可怕的方式在python中随机改组矩阵
问题描述
对于 DataScience 应用程序,我需要在开始处理之前随机打乱矩阵的行。
有没有办法做到这一点,而不仅仅是获取索引,改组索引,然后将改组的索引传递给矩阵?如:
indx = np.asarray(list(range(0, data.shape[0], 1)))
shufIndx = shuffle(indx)
data = data[shufIndx,:]
return (data)
谢谢!
解决方案
使用python
(not numpy
),您可以直接random.shuffle
使用行:
import random
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix)
random.shuffle(matrix) # random.shuffle mutates the input and returns None
print(matrix)
样本输出:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[7, 8, 9], [1, 2, 3], [4, 5, 6]]
推荐阅读
- html - Wordpress - 移动设备上的第一列低于另一列
- react-native - 清除浏览器 cookie React Native 0.60
- javascript - PDFJS:无效的 PDF 结构
- flutter - 颤振 | Dio Package ...在后台下载大文件
- spring - Spring boot 项目 .war 文件在 web-apps 文件夹中部署后不会自动启动
- excel - 运行时错误“1004”无法获取数据透视表类的 PivotFields 属性
- python - For Loop 在每次迭代中创建新的数据帧
- deep-learning - Transformers 中的自注意力机制如何能够学习单词之间的关系?
- java - 骆驼 3 - java.lang.NoClassDefFoundError: org/apache/camel/impl/DefaultComponent
- sql - SQL Server:每组创建 N 行并为每个组提供组号