简体   繁体   English

Python 如何将复杂范围内的 function 转换为 numpy

[英]Python how to convert complicated for in range function into numpy

I have a python function:我有一个 python function:

def gettpx(mt,age,maxage):
    val=1
    for i in range(int(age),int(maxage)):
        val=val*(1-mt[i])
    return val


age=74.3 maxage=94

mt is a list that has 136 random float: mt 是一个包含 136 个随机浮点数的列表:

mt=[0.39134593317492183,
 0.4656687788156125,
 0.7726701677118988,
 0.6832737926306782,
 0.25434722369719787,
 0.5474168041098489,
 0.7565441631563412,
 0.6636140058253324,
 0.20619693973275566,
 0.18357681712491203,
 0.006298936931721455,
 0.31207600754736053,
 0.37007600075770664,
 0.6806390956770311,
 0.7332366303320125,
 0.1968614130307731,
 0.2901122022871424,
 0.8900608105971619,
 0.5451547463327554,
 0.10315212265231932,
 0.8576249848111005,
 0.3134678295270493,
 0.0426886875139868,
 0.7971066522875242,
 0.6275640260325314,
 0.3191790402832023,
 0.5736375205001637,
 0.2283885769846492,
 0.43242110254429944,
 0.7361341207591294,
 0.1507446620630265,
 0.1355620284577157,
 0.4934401353118615,
 0.2998251808080844,
 0.8942003120669906,
 0.9635310604668181,
 0.185318821681831,
 0.01057937404667364,
 0.5118774705453705,
 0.5069872157210595,
 0.4581929103316379,
 0.1110582022724621,
 0.403994055020674,
 0.4774063299408774,
 0.390325612328589,
 0.19998195718999912,
 0.22392495115374578,
 0.7731292045555932,
 0.44564812037753543,
 0.7346674501567144,
 0.3983650042920601,
 0.5026395647979464,
 0.9622606931401192,
 0.28099190605575464,
 0.1839867054642057,
 0.7878034689897855,
 0.6951340440419705,
 0.10823834882135341,
 0.2616048698739901,
 0.411610479093538,
 0.7012604318711319,
 0.9798209446756172,
 0.44230107818237197,
 0.9614740919248279,
 0.8564578449971214,
 0.9842570368203774,
 0.158211011442684,
 0.6496756591877918,
 0.10565029305271245,
 0.5651744010042374,
 0.13194552477062826,
 0.5196069085858211,
 0.5880004438767666,
 0.3033679581277028,
 0.7575137623233205,
 0.773706325477112,
 0.33207724178949094,
 0.23436084911786204,
 0.941164517960277,
 0.7951287830989671,
 0.8313943006393854,
 0.7192536319091007,
 0.18809721872992702,
 0.8938131664065166,
 0.33060718909432685,
 0.24505191042603425,
 0.6088314697319741,
 0.3756036512482237,
 0.9239167984533697,
 0.9085608244622685,
 0.79742956438553,
 0.8602734460425286,
 0.08361035196204758,
 0.32619153615484,
 0.20157035256338107,
 0.34874045146117705,
 0.23804974284765246,
 0.5892263896358054,
 0.09421031158758886,
 0.4966196265804318,
 0.702653241187416,
 0.23603558881823217,
 0.6863136620695368,
 0.6238201051099804,
 0.12767533241593898,
 0.9048324535728437,
 0.14154232821066137,
 0.5805496236931306,
 0.15150045479072383,
 0.805000609020089,
 0.8860109443278866,
 0.9788848179625059,
 0.687591827928862,
 0.8867789292510606,
 0.09421946288180982,
 0.5379511988717263,
 0.2922324304056604,
 0.7901798791041743,
 0.9553621827311647,
 0.16391784322052116,
 0.33146429013538614,
 0.22921076669521534,
 0.6916347891865995,
 0.9033288112532827,
 0.6128369147231646,
 0.09996975198817704,
 0.2336295158639229,
 0.32165846855278624,
 0.22281731459505916,
 0.09118101975591075,
 0.24887784482670616,
 0.32138883355124415,
 0.16191871045488881,
 0.029192842999484103,
 0.7340915307412786,
 0.7488384230134199]

The output of this function is:这个 function 的 output 是:

result=gettpx(mt,74.3,94)

result=2.071

Since my data size is very large,so I want to modify this function to make it run faster,after reading this post https://www.blog.duomly.com/loops-in-python-comparison-and-performance/ I realized I should convert it to numpy.由于我的数据量很大,所以我想修改这个 function 让它运行得更快,在阅读了这篇文章https://www.blog.duomly.com/loops-in-python-comparison-and-performance/我意识到我应该将其转换为 numpy。

Any friend know how to convert this function to numpy?有朋友知道如何将这个 function 转换为 numpy 吗?

Using numpy :使用numpy

mt = np.array(mt)
age, maxage = 10,20
val = np.prod(1-mt[age:maxage])
print(val)

What you are doing here has a time complexity of O(n) where n is maxage-age you are multiplying all (1-mt[value]) numbers, where value is numbers between age and maxage.您在这里所做的时间复杂度为O(n) ,其中 n 是 maxage-age,您将所有(1-mt[value])数字相乘,其中value是年龄和 maxage 之间的数字。 Using numpy fastens the vectorized processes like adding 2 arrays or multiplying same indexed elements of 2 arrays, many and in you case no vectorization can be done so your existing code is good enough and doing this with a numpy array will hardly make any difference. Using numpy fastens the vectorized processes like adding 2 arrays or multiplying same indexed elements of 2 arrays, many and in you case no vectorization can be done so your existing code is good enough and doing this with a numpy array will hardly make any difference.

this is fine:这可以:

def gettpx(mt,age,maxage):
    val=1
    for i in range(int(age),int(maxage)):
        val*=(1-mt[i])
    return val

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM