繁体   English   中英

如何在 Python 中重复具有不同变量值的过程

[英]How to repeat a process with different value of variable in Python

对于 T 的每个值,我得到一个速度列表,然后找到标准偏差。 我想尝试使用不同的 T 值,每次我可以获得每个 T 的速度列表,然后找到标准偏差,而无需单独复制和运行代码。 有什么想法我该怎么做? 请仅在 Python 中。 谢谢

T2 = 1 #change as needed
N = 100  #number of loops (random walkers)
random_walk = np.empty((N,100)) #first element is row, second is column
for i in range (len(random_walk)):
    U_pos = 0
    U_neg = 0 
    U = U_pos - U_neg
    P_neg = 0.5*(1 - np.tanh(U/(2*T2)))
    P_pos = 0.5*(1 + np.tanh(U/(2*T2)))
    for j in range (len(random_walk[0])):
        random_walk[i][0] = 0
        if (random() > 0) and (P_neg > random()):  
            movement = -1
            U_j = np.random.pareto(2,1)+0.5
            if U_j > U_neg: 
                U_neg = U_j
            else: 
                U_neg = U_neg
            U = U_pos - U_neg
            P_neg = 0.5*(1 - np.tanh(U/(2*T2)))
            P_pos = 0.5*(1 + np.tanh(U/(2*T2)))
        else:
            movement = 1
            U_j = np.random.pareto(2,1)+0.5
            if U_j > U_pos:
                U_pos = U_j
            else: 
                U_pos = U_pos
            U = U_pos - U_neg
            P_pos = 0.5*(1 + np.tanh(U/(2*T2)))
            P_neg = 0.5*(1 - np.tanh(U/(2*T2)))   
        random_walk[i][j] = random_walk[i][j-1] + movement

您可以将代码块转换为可以调用的 function。 function 调用可以使用特定的数字列表,也可以是用户输入的值,如我编写的以下“原理证明”示例中所示。

import numpy as np
import random as random

def velocity(T2):
    N = 100  #number of loops (random walkers)
    random_walk = np.empty((N,100)) #first element is row, second is column
    for i in range (len(random_walk)):
        U_pos = 0
        U_neg = 0 
        U = U_pos - U_neg
        P_neg = 0.5*(1 - np.tanh(U/(2*T2)))
        P_pos = 0.5*(1 + np.tanh(U/(2*T2)))
        for j in range (len(random_walk[0])):
            random_walk[i][0] = 0
            x = random.random()
            if (x > 0) and (P_neg > x):  
                movement = -1
                U_j = np.random.pareto(2,1)+0.5
                if U_j > U_neg: 
                    U_neg = U_j
                else: 
                    U_neg = U_neg
                U = U_pos - U_neg
                P_neg = 0.5*(1 - np.tanh(U/(2*T2)))
                P_pos = 0.5*(1 + np.tanh(U/(2*T2)))
            else:
                movement = 1
                U_j = np.random.pareto(2,1)+0.5
                if U_j > U_pos:
                    U_pos = U_j
                else: 
                    U_pos = U_pos
                U = U_pos - U_neg
                P_pos = 0.5*(1 + np.tanh(U/(2*T2)))
                P_neg = 0.5*(1 - np.tanh(U/(2*T2)))   
            random_walk[i][j] = random_walk[i][j-1] + movement
        return random_walk
        
while True:
    data = int(input('Please enter a value to evaluate or 0 to exit: '))
    if data == 0:
        break
    print(velocity(data))

以下是该程序示例中的一些示例输入和 output。

@Una:~/Python_Programs/Velocity$ python3 Velocity.py 
Please enter a value to evaluate or 0 to exit: 2
[[  0.  -1.  -2. ... -95. -96. -97.]
 [  0.   0.   0. ...   0.   0.   0.]
 [  0.   0.   0. ...   0.   0.   0.]
 ...
 [  0.   0.   0. ...   0.   0.   0.]
 [  0.   0.   0. ...   0.   0.   0.]
 [  0.   0.   0. ...   0.   0.   0.]]
Please enter a value to evaluate or 0 to exit: 14
[[  0.  -1.   0. ... -13. -14. -15.]
 [  0.   0.   0. ...   0.   0.   0.]
 [  0.   0.   0. ...   0.   0.   0.]
 ...
 [  0.   0.   0. ...   0.   0.   0.]
 [  0.   0.   0. ...   0.   0.   0.]
 [  0.   0.   0. ...   0.   0.   0.]]
Please enter a value to evaluate or 0 to exit: 0

这只是您可以执行此操作的一种可能方式,但这应该让您深思。

希望有帮助。

问候。

暂无
暂无

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

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