[Python] optimize numpy loop using functional approach

Discussão em 'Python' iniciado por Stack, Novembro 26, 2024.

  1. Stack

    Stack Membro Participativo

    Dears, I am new in Python/Numpy, would like to know how to eliminate the loops and have a better performance for the code below, for a monte carlo simulation.

    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    import numpy as np
    import pandas as pd

    iterations = 3
    months = 12
    group_size = 5
    exit_factor = 0.2
    membership_fee = 2


    # join delay [periods]
    join_delay = np.random.triangular(left=0, mode=4, right=7,
    size=(iterations, group_size)).astype(int)
    # exit [number of users]
    exit_users = np.random.triangular(left=5, mode=10, right=20,
    size=(months, iterations)).astype(int)

    sim_table = np.zeros(shape = (iterations, group_size, months), dtype = 'int')
    print(join_delay)
    for j in range (0, iterations):
    for i in range(0, group_size):
    sim_table[j,i,join_delay[j,i]] = 200
    print(sim_table)

    Continue reading...

Compartilhe esta Página