第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何基于另一個(gè) NumPy 數(shù)組的值創(chuàng)建一個(gè) NumPy 數(shù)組?

如何基于另一個(gè) NumPy 數(shù)組的值創(chuàng)建一個(gè) NumPy 數(shù)組?

陪伴而非守候 2023-04-11 16:00:48
我想創(chuàng)建一個(gè) NumPy 數(shù)組。它的元素值取決于另一個(gè) NumPy 數(shù)組中元素的值。目前,我必須在列表理解中使用 for 循環(huán)來(lái)遍歷數(shù)組a以獲取b. NumPy 實(shí)現(xiàn)這一目標(biāo)的方法是什么?測(cè)試腳本:import numpy as npdef get_b( a ):    b_dict = {  1:10., 2:20., 3:30. }    return b_dict[ a ]a = np.full( 10, 2 )print( f'a = {a}' )b = np.array( [get_b(i) for i in a] )print( f'b = ' )輸出:a = [2 2 2 2 2 2 2 2 2 2]b = [20. 20. 20. 20. 20. 20. 20. 20. 20. 20.]
查看完整描述

4 回答

?
繁華開(kāi)滿天機(jī)

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊

您可以使用np.vectorize將字典值映射到數(shù)組


In [6]: b_dict = {  1:10., 2:20., 3:30 }


In [7]: a = np.full( 10, 2 )


In [8]: np.vectorize(b_dict.get)(a)

Out[8]: array([20., 20., 20., 20., 20., 20., 20., 20., 20., 20.])


查看完整回答
反對(duì) 回復(fù) 2023-04-11
?
慕運(yùn)維8079593

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊

解決問(wèn)題的另一種方法:


from operator import itemgetter

np.array(itemgetter(*a)(b_dict))

輸出:


[20., 20., 20., 20., 20., 20., 20., 20., 20., 20.]

比較:


#@kmundnic solution

def m1(a):

  def get_b(x):

    b_dict = {  1:10., 2:20., 3:30. }

    return b_dict[x]

  return np.fromiter(map(get_b, a),dtype=np.float)


#@bigbounty solution

def m2(a):

  b_dict = {  1:10., 2:20., 3:30. }

  return np.vectorize(b_dict.get)(a)


#@Ehsan solution

def m3(a):

  b_dict = {  1:10., 2:20., 3:30. }

  return np.array(itemgetter(*a)(b_dict))


#@Sun Bear solution

def m4(a):

  def get_b( a ):

    b_dict = {  1:10., 2:20., 3:30. }

    return b_dict[ a ]

  return np.array( [get_b(i) for i in a] )


in_ = [np.full( n, 2 ) for n in [10,100,1000,10000]]

對(duì)于small dictionary,似乎m2在大輸入時(shí)最快,而m3在小輸入時(shí)最快。

http://img1.sycdn.imooc.com//643513d90001e33d03190210.jpg

對(duì)于更大的字典:


b_dict = dict(zip(np.arange(100),np.arange(100)))

in_ = [np.full(n,50) for n in [10,100,1000,10000]]

m3是最快的方法。您可以根據(jù)您的字典大小和鍵數(shù)組大小進(jìn)行選擇。

http://img1.sycdn.imooc.com//643513e600013d7103180207.jpg

查看完整回答
反對(duì) 回復(fù) 2023-04-11
?
搖曳的薔薇

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊

map使用and怎么樣np.fromiter?


def get_b( a ):

    b_dict = {  1:10., 2:20., 3:30. }

    return b_dict[ a ]


a = np.full( 10, 2 )

b = np.fromiter(map(get_b, a), dtype=np.float64)

編輯 1:小時(shí)間比較:


%timeit np.array( [get_b(i) for i in a] )

5.58 μs ± 123 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)


%timeit np.fromiter(map(get_b, a), dtype=np.float64)

5.77 μs ± 177 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)


%timeit np.vectorize(b_dict.get)(a)

12.9 μs ± 76.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

編輯 2:好像那個(gè)例子太小了:


a = np.full( 1000, 2 )


%timeit np.array( [get_b(i) for i in a] )

415 μs ± 9.13 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)


%timeit np.fromiter(map(get_b, a), dtype=np.float64)

383 μs ± 2.5 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)


%timeit np.vectorize(b_dict.get)(a)

68.6 μs ± 625 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)


查看完整回答
反對(duì) 回復(fù) 2023-04-11
?
catspeake

TA貢獻(xiàn)1111條經(jīng)驗(yàn) 獲得超0個(gè)贊

必須b_dict是字典嗎?如果你有一個(gè)數(shù)組,例如。ref = np.array([0, 10,20,30])您可以按索引快速選擇值,?ref[a]在使用 numpy 時(shí),我會(huì)盡量避免使用 dict。

我發(fā)現(xiàn)使用 NumPy 的索引會(huì)使性能比嘗試使用 python 快幾個(gè)到幾個(gè)數(shù)量級(jí)dict。下面是一個(gè)進(jìn)行此類比較的腳本。

import numpy as np

from operator import itemgetter

import timeit

import matplotlib.pyplot as plt



#@kmundnic solution

def m1(a):

? ? def get_b(x):

? ? ? ? b = {? 1:10., 2:20., 3:30. }

? ? ? ? #b = dict( zip( np.arange(1,101),np.arange(10,1001,10) ) )

? ? ? ? return b[x]

? ? return np.fromiter(map(get_b, a),dtype=np.float)


#@bigbounty solution

def m2(a):

? ? b = {? 1:10., 2:20., 3:30. }

? ? #b = dict( zip( np.arange(1,101),np.arange(10,1001,10) ) )

? ? return np.vectorize(b.get)(a)


#@Ehsan solution

def m3(a):

? ? b = {? 1:10., 2:20., 3:30. }

? ? #b = dict( zip( np.arange(1,101),np.arange(10,1001,10) ) )

? ? return np.array(itemgetter(*a)(b))


#@Sun Bear solution

def m4(a):

? ? def get_b( a ):

? ? ? ? b = {? 1:10., 2:20., 3:30. }

? ? ? ? #b = dict( zip( np.arange(1,101),np.arange(10,1001,10) ) )

? ? ? ? return b[ a ]

? ? return np.array( [get_b(i) for i in a] )


#@hpaulj solution

def m5(a):

? ? b = np.array([10, 20, 30])

? ? #b = np.arange(10,1001,10)?

? ? return b[a]


? ? ? ??

sizes=[10,100,1000,10000]

pm1 = []

pm2 = []

pm3 = []

pm4 = []

pm5 = []

for size in sizes:

? ? a = np.full( size, 2 )

? ? pm1.append( timeit.timeit( 'm1(a)', number=1000, globals=globals() ) )

? ? pm2.append( timeit.timeit( 'm2(a)', number=1000, globals=globals() ) )

? ? pm3.append( timeit.timeit( 'm3(a)', number=1000, globals=globals() ) )

? ? pm4.append( timeit.timeit( 'm4(a)', number=1000, globals=globals() ) )

? ? pm5.append( timeit.timeit( 'm5(a)', number=1000, globals=globals() ) )


print( 'm1 slower than m5 by :',np.array(pm1) / np.array(pm5) )

print( 'm2 slower than m5 by :',np.array(pm2) / np.array(pm5) )

print( 'm3 slower than m5 by :',np.array(pm3) / np.array(pm5) )

print( 'm4 slower than m5 by :',np.array(pm4) / np.array(pm5) )


fig = plt.figure()

ax = fig.add_subplot(1, 1, 1)

ax.plot( sizes, pm1, label='m1' )

ax.plot( sizes, pm2, label='m2' )

ax.plot( sizes, pm3, label='m3' )

ax.plot( sizes, pm4, label='m4' )

ax.plot( sizes, pm5, label='m5' )

ax.grid( which='both' )

ax.set_xscale('log')

ax.set_yscale('log')

ax.legend()

ax.get_xaxis().set_label_text( label='len(a)', fontweight='bold' )

ax.get_yaxis().set_label_text( label='Runtime (sec)', fontweight='bold' )

plt.show()

結(jié)果:


長(zhǎng)度 (b) = 3:


m1 slower than m5 by : [? 4.22462367? 29.79407905? 85.03454097 339.2915358 ]

m2 slower than m5 by : [? 8.64220685 11.57175871 13.76761749 46.1940683 ]

m3 slower than m5 by : [? 3.25785432? 21.63131578? 54.71305704 220.15777696 ]

m4 slower than m5 by : [? 4.60710166? 30.93616607? 91.8936744? 371.00398273 ]

長(zhǎng)度 (b) = 100:


m1 slower than m5 by : [? 218.98603678? 1976.50128737? 9697.76615006 17742.79151719 ]

m2 slower than m5 by : [? 41.76535891? 53.85600913 109.35129345 164.13075291 ]

m3 slower than m5 by : [? 24.82715462? 36.77830986? 87.56253196 141.04493237 ]

m4 slower than m5 by : [? 222.04184193? 2001.72120836? 9775.22464369 18431.00155305 ]

http://img1.sycdn.imooc.com/643514050001ea9106530241.jpg

查看完整回答
反對(duì) 回復(fù) 2023-04-11
  • 4 回答
  • 0 關(guān)注
  • 224 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)