- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想要一个数组“set1”,它包含数组“a”的前四行,然后如果一行的第一个值/元素与“a”和“d”相同,则出现另一个区别取决于“d”的第二个值:
我到目前为止:
a=np.array(([5,2,3,4],[3,22,23,24],[2,31,32,34],[1,41,42,44],[4,51,52,54],[6,61,62,64]))
d=np.array(([2,3,5],[4,1,4],[2,1,2],[5,3,1],[6,2,44],[5,1,3],[1,3,55],[1,1,6]))
set1= np.zeros((a.shape[0],a.shape[1]+3),dtype="int")
set1[:,:4] = a[:,:]
for i in (set1[:,0]-1):
j=np.where(d[:,0]==set1[i,0])
if len(j[0])==1:
if d[j[0],1]==1:
set1[i,4]=d[j[0],2]
elif d[j[0],1]==2:
set1[i,5]=d[j[0],2]
elif d[j[0],1]==3:
set1[i,6]=d[j[0],2]
print(set1)
我的代码只有在只有 一次 出现相同数量的第一个元素时才有效(在这种情况下,只显示集合 1"6 61 62 64 0 44 0"的最后一行正确)。在所有其他情况下,不会存档所需的输出。例如显示第五行:
[5 51 52 54 0 0 0]
而不是想要的
[5 51 52 54 3 0 1]
是否有更“pythonic”的方式来做到这一点?比较第一个元素并根据上述规则组合元素(1 --> 第 4 个元素,等等)?
[编辑] 更改数字以避免混淆 ID 与索引
最佳答案
d
的前两列是 set1
中的索引,您要将值放在 d
的第三列中,稍作修正后。从那里开始,就像使用我在评论中链接到的索引模式一样简单。
完整示例:
a=np.array((
[1,2,3,4],
[2,22,23,24],
[3,31,32,34],
[4,41,42,44],
[5,51,52,54],
[6,61,62,64]))
d=np.array((
[2,3,5],
[4,1,4],
[2,1,2],
[5,3,1],
[6,2,44],
[5,1,3],
[1,3,55],
[1,1,6]))
# allocate the result array
m, n = a.shape
res = np.zeros((m, n+3))
res[:m, :n] = a
# do the work
i, j, values = d.T
res[i-1, j+3] = values
这样
>>> res
array([[ 1., 2., 3., 4., 6., 0., 55.],
[ 2., 22., 23., 24., 2., 0., 5.],
[ 3., 31., 32., 34., 0., 0., 0.],
[ 4., 41., 42., 44., 4., 0., 0.],
[ 5., 51., 52., 54., 3., 0., 1.],
[ 6., 61., 62., 64., 0., 44., 0.]])
d
的第一列不是索引...在 d
的第一列不是索引的一般情况下,您需要查找 d[:,0]
的每个条目的位置在 a
中。渐进地执行此操作的最快方法是使用哈希表,但实际上,执行此操作的足够快的方法是使用 np.searchsorted
:
a=np.array((
[5,2,3,4],
[3,22,23,24],
[2,31,32,34],
[1,41,42,44],
[4,51,52,54],
[8,61,62,64]))
d=np.array((
[2,3,5],
[4,1,4],
[2,1,2],
[5,3,1],
[8,2,44],
[5,1,3],
[1,3,55],
[1,1,6]))
# allocate the result array
m, n = a.shape
res = np.zeros((m, n+3))
res[:m, :n] = a
# do the work
i, j, values = d.T
ids = a[:, 0]
sort_ix = np.argsort(ids)
search_ix = np.searchsorted(ids, i, sorter=sort_ix)
id_map = sort_ix[search_ix]
res[id_map, j+3] = values
这样
>>> res
array([[ 5., 2., 3., 4., 3., 0., 1.],
[ 3., 22., 23., 24., 0., 0., 0.],
[ 2., 31., 32., 34., 2., 0., 5.],
[ 1., 41., 42., 44., 6., 0., 55.],
[ 4., 51., 52., 54., 4., 0., 0.],
[ 8., 61., 62., 64., 0., 44., 0.]])
注意:如果d[0, :]
有连续的整数1, ..., n
,但不一定按顺序,那么你可以避免排序而只使用直接查找表。将上面注释后的位替换为:
# do the work
i, j, values = d.T
ids = a[:, 0]
id_map = np.zeros_like(ids)
id_map[ids-1] = np.arange(len(ids))
res[id_map[i-1], j+3] = values
关于python - 列表理解与 np.where 比较两个数组并将它们组合起来,它的第一个条目是相等的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33632724/
有时评估积分非常困难,但很容易验证解是否正确。在我看来它至少应该是 np,但我对这个概念的理解是有限的,我可能会遗漏一些东西 编辑:为了清楚起见,我很好奇算法的复杂性,该算法找到函数的反导数以解决不定
我对这三个类别的理解是否正确? 要证明问题 X 是 NP: 表明 X 可以在多项式时间内确定性地得到验证(或者X 可以使用 NTM 解决) 要证明问题 X 是 NP 完全的: 表明 X 可以在多项式时
有什么区别NP , NP-完全和 NP-Hard ? 我知道网上有很多资源。我想阅读你的解释,原因是它们可能与外面的不同,或者有一些我不知道的东西。 最佳答案 我假设您正在寻找直观的定义,因为技术定义
我正在寻找一种方法来检查 numpy 数组是 np.float64 还是 np.float32。这适用于 np.float64: a = np.random.rand(10) if not issub
我知道 np.nanargmin 找到列表中不是 NaN 的最小数字。但是,如果调用数组 [np.nan, np.inf],它会产生 0,这是一个 NaN。我发现这种行为很奇怪,我只是想知道以这种方式
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 8 年前。 Improve
我很好奇使用 np.empty 到底有多大不同?而不是 np.zeros ,以及关于 np.ones 的区别.我运行这个小脚本来对每个创建大型数组所花费的时间进行基准测试: import numpy
在操作矩阵的时候,不同的接口对于矩阵的输入维度要求不同,输入可能为1-D,2-D,3-D等等。下面介绍一下使用Numpy进行矩阵维度变更的相关方法。主要包括以下几种: 1、np.newaxis扩充
除了使用一组 or 语句之外 isinstance( x, np.float64 ) 或 isinstance( x, np.float32 ) 或 isinstance( np.float16 )
我尝试了以下代码,但没有发现 np.dot 和 np.multiply 与 np.sum 之间的区别 这里是 np.dot 代码 logprobs = np.dot(Y, (np.log(A2)).T
我编写了一些程序来更新 numpy在每次迭代中列出并对其进行一些操作。迭代次数取决于时间。例如在 1 秒内,可能有 1000 到 2500 次迭代。这意味着 numpy 列表中的项目对于运行程序 1
我有以下两个数据框: a = pd.DataFrame([[1,2, 3],[4,3,6], [np.nan, 2, np.nan]]) 0 1 2 0 1.0 2 3.0 1
我有一个包含很多非零值的数组。当我使用以下方法计算非零项目总数时,我得到了 2 个不同的结果: 1) non_zero_weights = np.any(np.not_equal(lr_l1.coef
我对这么多int有点挣扎cython 中的数据类型。 np.int, np.int_, np.int_t, int 我猜 int在纯python中相当于np.int_ , 那么 np.int 在哪里来
如果您要选择以下三种初始化零数组的方法之一,您会选择哪一种以及为什么? my_arr_1 = np.full(size, 0) 或 my_arr_2 = np.zeros(size) 或 my_arr
我正在使用 np.select 根据应用于其他列的多个条件创建一个新列。这是一个简单的例子: df = pd.DataFrame({'A': [0, 3, 4], 'B': [10, 0, 2]})
我正在使用 np.select 根据应用于其他列的多个条件创建一个新列。这是一个简单的例子: df = pd.DataFrame({'A': [0, 3, 4], 'B': [10, 0, 2]})
我正在读这个code ,用于从头开始实现线性回归: # convert from data frames to numpy matrices X = np.matrix(X.values) y = n
我正在解压缩具有许多不同数据类型的大型二进制文件 (~1GB)。我正处于创建循环以隐藏每个字节的早期阶段。我一直在使用 struct.unpack,但最近认为如果我使用 numpy 它会运行得更快。然
我需要从依存分析树中提取形式为 NP-VP-NP 的三元组,作为 Stanford Parser 中词汇化分析的输出。 执行此操作的最佳方法是什么。例如如果解析树如下: (ROOT (S
我是一名优秀的程序员,十分优秀!