Apply multiple functions to each row of a numpy arrayHow can I apply a function to every row/column of a matrix in MATLAB?What's a variable in a inner function definition in python?Apply a function to each row of a ndarrayApply function to vectors in 3D numpy arrayapply function to each column of a matrix (Vectorizing)Vectorize numpy indexing and apply a function to build a matrixnumpy array operation methodAll possible columnwise multiplications in numpy?Create binary array of matching rows in an array using numpy?Numpy - Apply a custom function on all combination of rows in matrix to get a new matrix?

"Valet parking " or "parking valet"

What force enables us to walk? Friction or normal reaction?

What is a good example for artistic ND filter applications?

Is it unprofessional to mention your cover letter and resume are best viewed in Chrome?

What are the closest international airports in different countries?

Why tantalum for the Hayabusa bullets?

Can you continue the movement of a Bonus Action Dash granted by Expeditious Retreat if your Concentration is broken mid-move?

Microgravity indicators

What were the first chips with hardware support for SPI?

Why did some Apollo missions carry a grenade launcher?

Unknown indication below upper stave

Applications of pure mathematics in operations research

Does dual boot harm a laptop battery or reduce its life?

Why are subdominants unstable?

How do I say "this is why…"?

What is a Trio Word™?

How did the SysRq key get onto modern keyboards if it's rarely used?

What would the United Kingdom's "optimal" Brexit deal look like?

How to innovate in OR

Sub-dependencies and code disclosure

Road bike front wheel question

Earth observation-like spacecraft orbiting other planets or moons?

Why doesn't the Gottesman-Knill theorem render quantum computing almost useless?

Can a US President, after impeachment and removal, be re-elected or re-appointed?



Apply multiple functions to each row of a numpy array


How can I apply a function to every row/column of a matrix in MATLAB?What's a variable in a inner function definition in python?Apply a function to each row of a ndarrayApply function to vectors in 3D numpy arrayapply function to each column of a matrix (Vectorizing)Vectorize numpy indexing and apply a function to build a matrixnumpy array operation methodAll possible columnwise multiplications in numpy?Create binary array of matching rows in an array using numpy?Numpy - Apply a custom function on all combination of rows in matrix to get a new matrix?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















Let's say we have a matrix of 3 rows and 2 columns as mat, and I want to apply on each 3 row one of the functions of list what_functions_to_apply_list for which I have their definition. So the output of np.apply_along_axis should be 3 rows times the output dimension of the functions.



How can I do this without for looping in a vectorized way?



e.g.



def f1(inp1,inp2):
return out1, out2


where



 functions_dic = 'f1': func1, 'f2':func2, 'f3':func3
what_functions_to_apply_list = ['f1','f1','f2']
funcs_inputs = [[inp11,inp12], [inp21,inp32], [inp31,inp32]]
mat = np.ones((3, 2))
np.apply_along_axis(what_functions_to_apply_list , 1, mat)









share|improve this question



















  • 2





    You can't. Applying a custom function like this will necessarily require a call to Python and it will run in for-loop speed

    – roganjosh
    Mar 26 at 21:22











  • apply_along_axis isn't meant for that kind of use. For a start, the first argument is a function, not a list of functions. Even with a correct function it isn't faster alternative to explicit iteration. It is not a 'no-loop vectorized' tool.

    – hpaulj
    Mar 26 at 21:53











  • Please add sample data.

    – Istvan
    Mar 26 at 22:13

















1















Let's say we have a matrix of 3 rows and 2 columns as mat, and I want to apply on each 3 row one of the functions of list what_functions_to_apply_list for which I have their definition. So the output of np.apply_along_axis should be 3 rows times the output dimension of the functions.



How can I do this without for looping in a vectorized way?



e.g.



def f1(inp1,inp2):
return out1, out2


where



 functions_dic = 'f1': func1, 'f2':func2, 'f3':func3
what_functions_to_apply_list = ['f1','f1','f2']
funcs_inputs = [[inp11,inp12], [inp21,inp32], [inp31,inp32]]
mat = np.ones((3, 2))
np.apply_along_axis(what_functions_to_apply_list , 1, mat)









share|improve this question



















  • 2





    You can't. Applying a custom function like this will necessarily require a call to Python and it will run in for-loop speed

    – roganjosh
    Mar 26 at 21:22











  • apply_along_axis isn't meant for that kind of use. For a start, the first argument is a function, not a list of functions. Even with a correct function it isn't faster alternative to explicit iteration. It is not a 'no-loop vectorized' tool.

    – hpaulj
    Mar 26 at 21:53











  • Please add sample data.

    – Istvan
    Mar 26 at 22:13













1












1








1








Let's say we have a matrix of 3 rows and 2 columns as mat, and I want to apply on each 3 row one of the functions of list what_functions_to_apply_list for which I have their definition. So the output of np.apply_along_axis should be 3 rows times the output dimension of the functions.



How can I do this without for looping in a vectorized way?



e.g.



def f1(inp1,inp2):
return out1, out2


where



 functions_dic = 'f1': func1, 'f2':func2, 'f3':func3
what_functions_to_apply_list = ['f1','f1','f2']
funcs_inputs = [[inp11,inp12], [inp21,inp32], [inp31,inp32]]
mat = np.ones((3, 2))
np.apply_along_axis(what_functions_to_apply_list , 1, mat)









share|improve this question














Let's say we have a matrix of 3 rows and 2 columns as mat, and I want to apply on each 3 row one of the functions of list what_functions_to_apply_list for which I have their definition. So the output of np.apply_along_axis should be 3 rows times the output dimension of the functions.



How can I do this without for looping in a vectorized way?



e.g.



def f1(inp1,inp2):
return out1, out2


where



 functions_dic = 'f1': func1, 'f2':func2, 'f3':func3
what_functions_to_apply_list = ['f1','f1','f2']
funcs_inputs = [[inp11,inp12], [inp21,inp32], [inp31,inp32]]
mat = np.ones((3, 2))
np.apply_along_axis(what_functions_to_apply_list , 1, mat)






python function numpy vectorization






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 21:17









SoyolSoyol

3532 silver badges19 bronze badges




3532 silver badges19 bronze badges










  • 2





    You can't. Applying a custom function like this will necessarily require a call to Python and it will run in for-loop speed

    – roganjosh
    Mar 26 at 21:22











  • apply_along_axis isn't meant for that kind of use. For a start, the first argument is a function, not a list of functions. Even with a correct function it isn't faster alternative to explicit iteration. It is not a 'no-loop vectorized' tool.

    – hpaulj
    Mar 26 at 21:53











  • Please add sample data.

    – Istvan
    Mar 26 at 22:13












  • 2





    You can't. Applying a custom function like this will necessarily require a call to Python and it will run in for-loop speed

    – roganjosh
    Mar 26 at 21:22











  • apply_along_axis isn't meant for that kind of use. For a start, the first argument is a function, not a list of functions. Even with a correct function it isn't faster alternative to explicit iteration. It is not a 'no-loop vectorized' tool.

    – hpaulj
    Mar 26 at 21:53











  • Please add sample data.

    – Istvan
    Mar 26 at 22:13







2




2





You can't. Applying a custom function like this will necessarily require a call to Python and it will run in for-loop speed

– roganjosh
Mar 26 at 21:22





You can't. Applying a custom function like this will necessarily require a call to Python and it will run in for-loop speed

– roganjosh
Mar 26 at 21:22













apply_along_axis isn't meant for that kind of use. For a start, the first argument is a function, not a list of functions. Even with a correct function it isn't faster alternative to explicit iteration. It is not a 'no-loop vectorized' tool.

– hpaulj
Mar 26 at 21:53





apply_along_axis isn't meant for that kind of use. For a start, the first argument is a function, not a list of functions. Even with a correct function it isn't faster alternative to explicit iteration. It is not a 'no-loop vectorized' tool.

– hpaulj
Mar 26 at 21:53













Please add sample data.

– Istvan
Mar 26 at 22:13





Please add sample data.

– Istvan
Mar 26 at 22:13












1 Answer
1






active

oldest

votes


















0














A straight forward application of a list of functions to the rows of an array:



In [418]: alist = [np.add, np.subtract, np.multiply] 
In [419]: data = np.arange(6).reshape(3,2)
In [420]: [foo(*ab) for foo, ab in zip(alist, data)]
Out[420]: [1, -1, 20]





share|improve this answer

























  • how about providing inputs to each function as well

    – Soyol
    Mar 27 at 16:41











  • This is providing inputs, the rows of data. If you mean other parameters (e.g. axis), you'll have to create functions (or lambdas) that set those first.

    – hpaulj
    Mar 27 at 17:11










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55366330%2fapply-multiple-functions-to-each-row-of-a-numpy-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














A straight forward application of a list of functions to the rows of an array:



In [418]: alist = [np.add, np.subtract, np.multiply] 
In [419]: data = np.arange(6).reshape(3,2)
In [420]: [foo(*ab) for foo, ab in zip(alist, data)]
Out[420]: [1, -1, 20]





share|improve this answer

























  • how about providing inputs to each function as well

    – Soyol
    Mar 27 at 16:41











  • This is providing inputs, the rows of data. If you mean other parameters (e.g. axis), you'll have to create functions (or lambdas) that set those first.

    – hpaulj
    Mar 27 at 17:11















0














A straight forward application of a list of functions to the rows of an array:



In [418]: alist = [np.add, np.subtract, np.multiply] 
In [419]: data = np.arange(6).reshape(3,2)
In [420]: [foo(*ab) for foo, ab in zip(alist, data)]
Out[420]: [1, -1, 20]





share|improve this answer

























  • how about providing inputs to each function as well

    – Soyol
    Mar 27 at 16:41











  • This is providing inputs, the rows of data. If you mean other parameters (e.g. axis), you'll have to create functions (or lambdas) that set those first.

    – hpaulj
    Mar 27 at 17:11













0












0








0







A straight forward application of a list of functions to the rows of an array:



In [418]: alist = [np.add, np.subtract, np.multiply] 
In [419]: data = np.arange(6).reshape(3,2)
In [420]: [foo(*ab) for foo, ab in zip(alist, data)]
Out[420]: [1, -1, 20]





share|improve this answer













A straight forward application of a list of functions to the rows of an array:



In [418]: alist = [np.add, np.subtract, np.multiply] 
In [419]: data = np.arange(6).reshape(3,2)
In [420]: [foo(*ab) for foo, ab in zip(alist, data)]
Out[420]: [1, -1, 20]






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 27 at 6:23









hpauljhpaulj

125k7 gold badges100 silver badges177 bronze badges




125k7 gold badges100 silver badges177 bronze badges















  • how about providing inputs to each function as well

    – Soyol
    Mar 27 at 16:41











  • This is providing inputs, the rows of data. If you mean other parameters (e.g. axis), you'll have to create functions (or lambdas) that set those first.

    – hpaulj
    Mar 27 at 17:11

















  • how about providing inputs to each function as well

    – Soyol
    Mar 27 at 16:41











  • This is providing inputs, the rows of data. If you mean other parameters (e.g. axis), you'll have to create functions (or lambdas) that set those first.

    – hpaulj
    Mar 27 at 17:11
















how about providing inputs to each function as well

– Soyol
Mar 27 at 16:41





how about providing inputs to each function as well

– Soyol
Mar 27 at 16:41













This is providing inputs, the rows of data. If you mean other parameters (e.g. axis), you'll have to create functions (or lambdas) that set those first.

– hpaulj
Mar 27 at 17:11





This is providing inputs, the rows of data. If you mean other parameters (e.g. axis), you'll have to create functions (or lambdas) that set those first.

– hpaulj
Mar 27 at 17:11








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55366330%2fapply-multiple-functions-to-each-row-of-a-numpy-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Obelisk of Theodosius Contents History Description Notes Bibliography Further reading External links Navigation menuAge of spirituality : late antique and early Christian art, third to seventh centuryOver 60 picturesObelisks of the World41°00′21.24″N 28°58′31.43″E / 41.0059000°N 28.9753972°E / 41.0059000; 28.97539727724550-7235741376235741376

밀양 대씨 역사 각주 함께 보기 둘러보기 메뉴밀양 대씨

1973년 목차 사건 문화 탄생 사망 노벨상 달력 둘러보기 메뉴