- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
例如,我有一个列出对的文本文件
10,1
2,7
3,1
10,1
然后将其转换为对称矩阵,因此 (1,10) 条目是 (1,10) 对出现在列表中的次数。我现在想对这个矩阵进行二次采样。我所说的子样本是指 - 我想制作一个矩阵,该矩阵是仅使用原始文本文件中随机 30% 的行的结果。因此,在本例中,如果我删除了文本文件的 70%,则 (1,10) 对可能只显示一次而不是两次,因此矩阵中的 (1,10) 条目将为 1 而不是 2。
如果我确实有原始文本文件,只需使用 random.sample 挑选出文件中 30% 的行,就可以轻松完成此操作。但如果我只有矩阵,我怎样才能随机抽取 70% 的数据呢?
最佳答案
我想最好的方法取决于你的数据在哪里:
这是一个适合第二种情况的解决方案,尽管它也可以工作第一种情况没问题。
基本上,计数恰好位于二维矩阵中的事实并非如此重要的是:这基本上是从具有以下特征的总体中进行抽样的问题:已被扔进垃圾桶。所以我们能做的就是直接提取垃圾箱,而忘记矩阵一点:
import numpy as np
import random
# Input counts matrix
mat = np.array([
[5, 5, 2],
[1, 1, 3],
[6, 0, 4]
], dtype=np.int64)
# Build a list of (row,col) pairs, and a list of counts
keys, counts = zip(*[
((i,j), mat[i,j])
for i in range(mat.shape[0])
for j in range(mat.shape[1])
if mat[i,j] > 0
])
然后使用累积计数数组从这些容器中进行采样:
# Make the cumulative counts array
counts = np.array(counts, dtype=np.int64)
sum_counts = np.cumsum(counts)
# Decide how many counts to include in the sample
frac_select = 0.30
count_select = int(sum_counts[-1] * frac_select)
# Choose unique counts
ind_select = sorted(random.sample(xrange(sum_counts[-1]), count_select))
# A vector to hold the new counts
out_counts = np.zeros(counts.shape, dtype=np.int64)
# Perform basically the merge step of merge-sort, finding where
# the counts land in the cumulative array
i = 0
j = 0
while i<len(sum_counts) and j<len(ind_select):
if ind_select[j] < sum_counts[i]:
j += 1
out_counts[i] += 1
else:
i += 1
# Rebuild the matrix using the `keys` list from before
out_mat = np.zeros(mat.shape, dtype=np.int64)
for i in range(len(out_counts)):
out_mat[keys[i]] = out_counts[i]
现在您将在 out_mat
中获得采样矩阵。
关于python - 对矩阵 python 进行二次采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11818215/
我正在寻找一种方法来对数字进行 1:40、3812 次(长度 = 3812)的采样,并进行替换 - 但对其进行限制,使每个数字的使用次数不会超过 100 次。有没有办法在采样命令 (sample())
如果我想随机采样 pandas 数据帧,我可以使用 pandas.DataFrame.sample . 假设我随机抽取 80% 的行。如何自动获取另外 20% 未选取的行? 最佳答案 正如 Lager
我使用以下函数在每个图像中采样点。如果batch_size为None,tf.range会给出错误。如何在 tensorflow 中采样 def sampling(binary_selection,nu
我想知道是否有任何方法可以循环浏览 .wav 文件以获取 wav 文件中特定点的振幅/DB。我现在正在将它读入一个字节数组,但这对我来说没有任何帮助。 我将它与我开发的一些硬件结合使用,这些硬件将光数
我有一个日期时间的时间序列,双列存储在 mySQL 中,并且希望每分钟对时间序列进行采样(即以一分钟为间隔提取最后一个值)。在一个 select 语句中是否有一种有效的方法来做到这一点? 蛮力方式将涉
我正在为延迟渲染管道准备好我的一个小型 DirectX 11.0 项目中的一切。但是,我在从像素着色器中对深度缓冲区进行采样时遇到了很多麻烦。 首先我定义深度纹理及其着色器资源 View :
问题出现在量子值的样本上。情况是: 有一个表支付(payments): id_user[int] sum [int] date[date] 例如, sum(数量) 可以是 0 到 100,000 之间
这是一个理论问题。我目前正在研究渲染方程,我不明白在哪种情况下区域采样或半球采样更好以及为什么。 我想知道的另一件事是,如果我们采用两种方法的平均值,结果是否会更好? 最佳答案 Veach 和 Gui
我有一个 4x4 阵列,想知道是否有办法从它的任何位置随机抽取一个 2x2 正方形,允许正方形在到达边缘时环绕。 例如: >> A = np.arange(16).reshape(4,-1) >> s
我想构建 HBase 表的行键空间的随机样本。 例如,我希望 HBase 中大约 1% 的键随机分布在整个表中。执行此操作的最佳方法是什么? 我想我可以编写一个 MapReduce 作业来处理所有数据
当像这样在 GLSL 中对纹理进行采样时: vec4 color = texture(mySampler, myCoords); 如果没有纹理绑定(bind)到 mySampler,颜色似乎总是 (0
我考虑过的一些方法: 继承自Model类 Sampled softmax in tensorflow keras 继承自Layers类 How can I use TensorFlow's sampl
我有表clients,其中包含id、name、company列。 表agreements,其中包含id、client_id、number、created_at列. 一对多关系。 我的查询: SELEC
在具有许多类的分类问题中,tensorflow 文档建议使用 sampled_softmax_loss通过一个简单的 softmax减少训练时间。 根据docs和 source (第 1180 行),
首先,我想从三个数据帧(每个 150 行)中随机抽取样本并连接结果。其次,我想尽可能多地重复这个过程。 对于第 1 部分,我使用以下函数: def get_sample(n_A, n_B, n_C):
我正在尝试编写几个像素着色器以应用于类似于 Photoshop 效果的图像。比如这个效果: http://www.geeks3d.com/20110428/shader-library-swirl-p
使用 Activity Monitor/Instruments/Shark 进行采样将显示充满 Python 解释器 C 函数的堆栈跟踪。如果能看到相应的 Python 符号名称,我会很有帮助。是否有
我正在使用GAPI API来访问Google Analytics(分析),而不是直接自己做(我知道有点懒...)。我看过类文件,但看不到任何用于检查采样的内置函数。我想知道使用它的人是否找到了一种方法
我正在尝试从 Peoplesoft 数据库中随机抽取总体样本。在线搜索使我认为 select 语句的 Sample 子句可能是我们使用的一个可行选项,但是我无法理解 Sample 子句如何确定返回的样
我有一个程序,在其中我只是打印到 csv,我想要每秒正好 100 个样本点,但我不知道从哪里开始或如何做!请帮忙! from datetime import datetime import panda
我是一名优秀的程序员,十分优秀!