numpy: multiply arrays rowwise

2024/11/18 23:26:31

I have those arrays:

a = np.array([[1,2],[3,4],[5,6],[7,8]])b = np.array([1,2,3,4])

and I want them to multiply like so:

[[1*1, 2*1],
[3*2, 4*2],
[5*3, 6*3],
[7*4, 8*4]]

... basically out[i] = a[i] * b[i], where a[i].shape is (2,) and b[i] then is a scalar.

What's the trick? np.multiply seems not to work:

>>> np.multiply(a, b)
ValueError: operands could not be broadcast together with shapes (4,2) (4)
Answer

add an axis to b:

>>> np.multiply(a, b[:, np.newaxis])
array([[ 1,  2],[ 6,  8],[15, 18],[28, 32]])
https://en.xdnf.cn/q/26510.html

Related Q&A

django post_save signals on update

I am trying to set up some post_save receivers similar to the following: @receiver(post_save, sender=Game, dispatch_uid=game_updated) def game_updated(sender, **kwargs):DO SOME STUFF HEREMyPick.objects…

Fastest way to pack a list of floats into bytes in python

I have a list of say 100k floats and I want to convert it into a bytes buffer.buf = bytes() for val in floatList:buf += struct.pack(f, val) return bufThis is quite slow. How can I make it faster using …

PyPI is slow. How do I run my own server?

When a new developer joins the team, or Jenkins runs a complete build, I need to create a fresh virtualenv. I often find that setting up a virtualenv with Pip and a large number (more than 10) of requi…

How to suppress the deprecation warnings in Django?

Every time Im using the django-admin command — even on TAB–completion — it throws a RemovedInDjango19Warning (and a lot more if I use the test command). How can I suppress those warnings?Im using D…

whats the fastest way to find eigenvalues/vectors in python?

Currently im using numpy which does the job. But, as im dealing with matrices with several thousands of rows/columns and later this figure will go up to tens of thousands, i was wondering if there was …

Python Patch/Mock class method but still call original method

I want to use patch to record all function calls made to a function in a class for a unittest, but need the original function to still run as expected. I created a dummy code example below:from mock im…

Daemon vs Upstart for python script

I have written a module in Python and want it to run continuously once started and need to stop it when I need to update other modules. I will likely be using monit to restart it, if module has crashed…

Comparison of Python modes for Emacs

So I have Emacs 24.3 and with it comes a quite recent python.el file providing a Python mode for editing.But I keep reading that there is a python-mode.el on Launchpad, and comparing the two files it j…

Python best formatting practice for lists, dictionary, etc

I have been looking over the Python documentation for code formatting best practice for large lists and dictionaries, for example,something = {foo : bar, foo2 : bar2, foo3 : bar3..... 200 chars wide, e…

TypeError: string indices must be integers, not str // working with dict [duplicate]

This question already has answers here:Why am I seeing "TypeError: string indices must be integers"?(10 answers)Closed 28 days ago.I am trying to define a procedure, involved(courses, person…