0

I want to iterate rows one by one of an array of population (10,4) and parse each element of a column to given variables of function (x1, x2, x3, x4), respectively. How should I set loops so that one loop can iterate rows (one by one) and the second loop can assign values of columns to the function's variables? The variables also may need to define before function accordingly. Many thanks in advance! Here is my code:

import pandas as pd
import numpy as np

nv = 4              
lb = [0, 0, 0, 0]
ub = [20, 17, 17, 15]
n = 10             

def random_population(nv,n,lb,ub):
    pop = np.zeros((n, nv)) 
    for i in range(n):
        pop[i,:] = np.random.uniform(lb,ub)
    return pop

population = random_population(nv, n, lb, ub)           

def test(x1, x2, x3, x4):   
##Assignment statement
    y = (10*x1 + 45.5*x2 - 6*x3) * x4 
    
    return y
Husnain
  • 243
  • 1
  • 2
  • 5
  • 1
    Does this answer your question? [Iterate 2D arrays and assignment to variables of function in python](https://stackoverflow.com/questions/71673016/iterate-2d-arrays-and-assignment-to-variables-of-function-in-python) – Michael Szczesny Mar 31 '22 at 03:50
  • I have read comments from that post, but they asked to parse column-wise (all rows together), but I want to parse one by one. – Husnain Mar 31 '22 at 03:52
  • 1
    Please do not repost your question, the duplicate has already been answered. If the problem isn't solved, clarify the question. What is the problem with calling the *test* function vectorized? – Michael Szczesny Mar 31 '22 at 03:58
  • Thanks for your time, actually the problem was posted again as it was not correctly asked in the question. Sorry for the inconvenience caused. – Husnain Mar 31 '22 at 04:04
  • I post an answer by iterations on your [previous post](https://stackoverflow.com/questions/71673016/iterate-2d-arrays-and-assignment-to-variables-of-function-in-python/71714026#71714026). – Ali_Sh Apr 04 '22 at 05:51

0 Answers0