我正在用numpy(python2.7)从1d数组构建2d数组。我在寻找最有效的方法。最新的发现是:
a=np.ones(100000)
# SUBSCRIPTING
n_dim=3
x=0
for i in xrange(0,1000):
x=np.zeros(shape=(100000,n_dim))
for j in xrange(0,n_dim):
x[:,j]=a*j
# ~1.574 s/1000 loops - joinind 3 1d arrays
# ~9.162 s/1000 loops - joinind 10 1d arrays
# STACKING
for i in xrange(0,1000):
x=a*0.
for j in xrange(1,n_dim):
x=np.vstack((x,a*j))
x=x.T
# ~1.786 s/1000 loops - joinind 3 1d arrays
# ~16.603 s/1000 loops - joinind 10 1d arrays
第一种方法(下标)是我想出的最快的方法,与第二种方法(叠加)相比,性能的提高随着我加入的一维数组的数量而增加。由于我需要重复这一步,我想知道是否有更快的方法?如果解决方案提供了显著的性能提升,那么我愿意使用这种方案。在
也许我可以尝试以一种限制堆叠操作次数的方式堆叠阵列(例如,将4个1d阵列连接起来:首先堆叠阵列1和2,然后堆叠阵列3和4,最后再堆叠结果阵列)。在
我的问题是如何从一维阵列高效地构建二维阵列。我在这里使用的数组中的值是虚拟的。在实际应用程序中,我加入的一维数组中的大多数值可能会有所不同。在
Tags:
方法innumpyfornponeszeros数组性能dimarraysloopsxrangesubscriptingjoinind2条回答网友
1楼 ·
编辑于 2023-03-06 23:30:51
因为numpy将数组(默认情况下)存储在row-major order中,按行设置值更有效。因此,我将使用:
x=np.zeros(shape=(n_dim, 100000))
for j in range(0,n_dim):
x[j,:]=a*j
或者,您可以将x
定义为列major,然后,这与前面的代码一样快:
^{pr2}$
您还可以使用numpy外部产品创建x
:
v = np.arange(n_dim)
x = np.outer(v, a)
网友
2楼 ·
编辑于 2023-03-06 23:30:51
这是使用vstack
的糟糕方法;您反复调用它,为每个j
创建一个新的x
x=a*0.
for j in xrange(1,n_dim):
x=np.vstack((x,a*j))
x=x.T
正确的方法是构建一个数组列表,并且只使用vstack
一次。在
^{pr2}$
在这种情况下,append
与vstack
一样工作,可能更快。还有一个column_stack
函数。关键的区别是我利用了快速列表追加,以及array
(和vstack
)在其参数列表中获取许多项的能力。在
如果你能把循环写成一个列表理解,那就更好了
x = np.array([a*j for j in xrange(1,n_dim)])
在预先分配的数组中插入通常是最快的选择。但是您应该熟悉这种从列表构建的方法。在
基本np.array
表达式
np.array([[1,2,3],[4,5,6]])
就是这样,从1d数组的列表(或者在本例中是列表)构建2d。在
np.array([a*0,a*1,a*2])
jakub
注意到{}速度很慢。对于n_dim=10
:
In [257]: timeit x=np.array([(a*j) for j in range(n_dim)]).T
1 loops, best of 3: 228 ms per loop
In [258]: timeit x=np.array([(a*j).tolist() for j in range(n_dim)]).T
1 loops, best of 3: 228 ms per loop
显然,np.array
正在将输入数组转换为列表,然后从嵌套列表(或类似的东西)中执行其通常的构造。在
In [259]: timeit x=np.vstack([(a*j) for j in range(n_dim)]).T
10 loops, best of 3: 24.9 ms per loop
数组列表上的vstack
要快得多。比迭代的vstack
(我预期的)快。基本上与Ramon's
行插入(和插入order='F'
)相同
In [272]: %%timeit
x=np.zeros((n_dim,a.shape[0]))
for j in range(n_dim):
x[j,:]=a*j
.....: x=x.T
.....:
10 loops, best of 3: 23.3 ms per loop
当concatenate
(由vstack
使用)被编译时,我怀疑它做了类似于迭代插入的操作。在源代码中,通常创建一个空的目标数组,然后用适当的值填充它。在
where can you get viagra sildenafil 100 online best price viagra 50 mg
keflex for wound infection cephalexin and keflex the same cephalexin expired 3 years ago
methenamine hippurate and keflex cephalexin 500 mg para que serve can cephalexin make you itchy
what to avoid while taking keflex standard dose of keflex 500mg for a 235 lb person keflex for dental infection dosage
is keflex good for uti which is better keflex vs amoxicillin for burn infection keflex shot for my cat
cephalexin dosage for dogs calculator how much does cephalexin cost cephalexin for ear infection dosage
cephalexin and amoxicillin allergy can i take cephalexin and metronidazole together can cephalexin affect birth control
amoxicillin dosing calculator how long amoxicillin good for can you take prednisone with amoxicillin
amoxicillin 500 mg used for amoxicillin 250mg/5ml dosage calculator can you take old amoxicillin
can you take acetaminophen with amoxicillin amoxicillin 400mg/5ml dosage for adults amoxicillin-clavulanate potassium
amoxicillin side effects reddit how long amoxicillin good for amoxicillin clavulanate
buy amoxicillin without prescription double dose of amoxicillin accidentally amoxicillin use
augmentin dose for adults augmentin ko-amoksiklav which is stronger augmentin or ceftin
antybiotyk augmentin dla noworodka amoxicillin vs augmentin sinusitis allergy augmentin rash
dangers of prednisone prednisone and headaches how often can you take prednisone
prednisolone vs prednisone prednisone for dogs dosage chart is prednisone dangerous
what is doxycycline hyclate 100mg used for will doxycycline treat a tooth infection buy doxycycline amazon
doxycycline mono 100mg tab doxycycline sunburn how long for doxycycline to work
can diabetics take ciprofloxacin ciprofloxacin ear drops price what happens if i take antacids with ciprofloxacin
can you take omeprazole with ciprofloxacin side effects ciprofloxacin 500 mg can i take melatonin with ciprofloxacin
how long does azithromycin take to work azithromycin 500 dose for adults azithromycin hearing loss
cialis ® for daily use tadalafil 40 mg cialis not working anymore
us online cialis cialis generic cheap prices tadalafil drug
order cialis brand cialis free trial voucher 2019 cialis cost in nz
sildenafil australia buy generic viagra from india purchase viagra online without prescription
generic viagra online best price generic viagra 50mg price sildenafil cream
cyber bullying essay conclusion essay example write my essay cheap
what is a header in an essay essay writing help best rated essay writing service
essay writers online cheap essay outline example help with essay introduction
descriptive essay example how to start a persuasive essay what does an essay look like
diversity essay personal essay for college essay writing service online
someone write my essay to construct an effective ending to any type of essay, you should essay about service
med rx online pharmacy target pharmacy warfarin drug price
trusted online pharmacy Kamagra Effervescent Feldene
oxycodone pharmacy rules us pharmacy viagra no prescription ventolin uk pharmacy
pharmacy online levitra inhouse pharmacy spironolactone Brand Cialis
online pharmacy nizoral oxycodone pharmacy mexico online pharmacy oxycodone prescription
store pharmacy Lincocin Tetracycline
online pharmacy no prescription required fluconazole pharmacy online lloyd pharmacy viagra
best online pharmacy accutane provigil online us pharmacy is online pharmacy legit
remedies rx pharmacy ketoconazole pharmacy online pharmacy coupon
how long does tadalafil 6mg last sildenafil or tadalafil? online tadalafil
does tadalafil help with premature ejaculation sildenafil vs tadalafil cual es mejor tadalafil losartan combination therapy
tadalafil mg dosage snri tadalafil interaction tadalafil for prostatitis
tadalafil com 20mg tadalafil daily tadalafil pah vs tadalafil
tadalafil online uk tadalafil 20mg india vardenafil vs tadalafil vs sildenafil
tadalafil better than sildenafil prescription for tadalafil tadalafil dosage liquid
tadalafil 5 mg tablet uses jodie marsh tadalafil tadalafil cost cvs
tadalafil with a statin get tadalafil cheapest tadalafil
microdosing tadalafil tadalafil 12mg low cost tadalafil online
tadalafil cialis cost tadalafil images pregnancy category for tadalafil
tadalafil 20 mg brand name tadalafil in women losartan and tadalafil
tadalafil buy uk tadalafil 100 mg how much tadalafil to take
is tadalafil a nitrate tadalafil tab can you take expired tadalafil
can you cut tadalafil in half sildenafil vs tadalafil dosage cialis tadalafil treat
is tadalafil cialis nitrates and tadalafil tadalafil generico
apo tadalafil how long does tadalafil last in the body para que sirve tadalafil 5mg
tadalafil for daily use tadalafil pricing tadalafil with food
tadalafil effect on female tadalafil ip 20 mg chrisale tadalafil para que sirve