我需要完成以下任务:
从:
a = array([[1,3,4],[1,2,3]...[1,2,1]])
Run Code Online (Sandbox Code Playgroud)
(向每行添加一个元素):
a = array([[1,3,4,x],[1,2,3,x]...[1,2,1,x]])
Run Code Online (Sandbox Code Playgroud)
我试过像[n] =数组([1,3,4,x])这样的东西
但numpy抱怨形状不匹配.我尝试迭代a并将元素x附加到每个项目,但不会反映更改.
有关如何实现这一目标的任何想法?
我有一个向量数组,并计算他们的差异与第一个的差异.使用python广播时,计算速度明显慢于通过简单循环进行计算.为什么?
import numpy as np
def norm_loop(M, v):
n = M.shape[0]
d = np.zeros(n)
for i in range(n):
d[i] = np.sum((M[i] - v)**2)
return d
def norm_bcast(M, v):
n = M.shape[0]
d = np.zeros(n)
d = np.sum((M - v)**2, axis=1)
return d
M = np.random.random_sample((1000, 10000))
v = M[0]
Run Code Online (Sandbox Code Playgroud)
%timeit norm_loop(M,v) - > 25.9 ms
%timeit norm_bcast(M,v) - > 38.5 ms
我有Python 3.6.3和Numpy 1.14.2
要在google colab中运行示例:https://drive.google.com/file/d/1GKzpLGSqz9eScHYFAuT8wJt4UIZ3ZTru/view ? usp = sharing
我有以下 numpy 数组:
array(['00:00', '00:05', '00:15', '00:20', '00:25', '00:30', '00:35',
'00:40', '00:45', '00:50', '00:55', '01:00', '01:05', '01:10',
'01:15', '01:20', '01:40', '01:45', '01:55', '02:05', '02:10',
'02:15', '02:35', '02:40', '02:45', '02:55', '03:05', '03:10',
'03:30', '03:55', '04:00', '04:05', '04:25', '04:40', '04:55',
'05:00', '05:05', '05:15', '05:20', '05:25', '05:30', '05:35',
'05:50', '05:55', '06:05', '06:20', '06:25', '06:30', '06:35',
'06:45', '06:50', '07:05', '07:15', '07:30', '07:40', '07:45',
'07:50', '07:55', '08:10', '08:20', '08:25', '08:40', '08:45',
'08:50', '09:15', '09:20', '09:45', '09:50', '09:55', '10:10',
'10:15', '10:25', '10:30', '10:45', '10:50', '11:00', '11:05', …Run Code Online (Sandbox Code Playgroud)