list comprehension with numpy arrays - bad practice?

2024/10/1 19:42:09

I am wondering if the below approach would be considered bad practice, and if so, if someone could give some guidance towards another approach.

Here is the code in question:

a = np.array([[1,2,3],[4,5,6]])
b = np.array([-5,5])
c = np.array([np.multiply(a[x],b[x]) for x in range(2)])

The objective here is to obtain an array of the same shape as 'a' where the values in the first element of 'a' are multiplied by the first element of 'b' and the values in the second element of 'a' are multiplied by the second element of 'b'

The above code works, but given the mixture of lists/arrays involved I'm concerned this is advised against - but I'm not clear on a more elegant solution. Many thanks in advance!

Answer

NumPythonic way would be to extend the dimensions of b to a 2D array with np.newaxis/None and then let broadcasting come into play for a vectorized elementwise multiplication. The implementation would look like this -

c = a * b[:,None]

Once the dimensions are extended, you can also use np.multiply for the same effect, like so -

c = np.multiply(a,b[:,None])

Most importantly, here's some performance numbers to persuade you on using broadcasting -

In [176]: a = np.random.rand(2000,3000)In [177]: b = np.random.rand(2000)In [178]: %timeit np.array([np.multiply(a[x],b[x]) for x in range(a.shape[0])])
10 loops, best of 3: 118 ms per loopIn [179]: %timeit a * b[:,None]
10 loops, best of 3: 63.8 ms per loopIn [180]: %timeit np.multiply(a,b[:,None])
10 loops, best of 3: 64 ms per loop
https://en.xdnf.cn/q/70932.html

Related Q&A

pandas: write dataframe to excel file *object* (not file)?

I have a dataframe that I want to convert to excel file, and return it using HTTP. Dataframes to_excel method accepts either a path, or an ExcelWriter, which, in turn, refers to a path.Is there any way…

win32: moving mouse with SetCursorPos vs. mouse_event

Is there any difference between moving the mouse in windows using the following two techniques?win32api.SetCursorPos((x,y))vs:nx = x*65535/win32api.GetSystemMetrics(0) ny = y*65535/win32api.GetSystemM…

Pandas: Unstacking One Column of a DataFrame

I want to unstack one column in my Pandas DataFrame. The DataFrame is indexed by the Date and I want to unstack the Country column so each Country is its own column. The current pandas DF looks like t…

python-polars split string column into many columns by delimiter

In pandas, the following code will split the string from col1 into many columns. is there a way to do this in polars? d = {col1: ["a/b/c/d", "a/b/c/d"]} df= pd.DataFrame(data=d) df…

pylint giving not-callable error for object property that is callable

Not sure if I am doing something wrong or if this is a problem with pylint. In the code below I get a linting error that self.type is not callable E1102.Although I could just ignore it and keep workin…

ModuleNotFoundError: No module named api

I created a Django project inside of api folder called bucks:api |____ categories/|____ __init__.py|____ ...|____ models.py|____ tests.py|____ views.py |____ .../ |____ bucks/ |____ users/|____ __init_…

Reading csv header white space and case insensitive

Is there a possibility to read the header of a CSV file white space and case insensitive? As for now I use csv.dictreader like this:import csv csvDict = csv.DictReader(open(csv-file.csv, rU))# determi…

How to remove the seconds of Pandas dataframe index?

Given a dataframe with time series that looks like this:Close 2015-02-20 14:00:00 1200.1 2015-02-20 14:10:00 1199.8 2015-02-21 14:00:00 1199.3 2015-02-21 14:10:00 1199.0 2015-02-22 14:00:00 1198.4…

Slow loading SQL Server table into pandas DataFrame

Pandas gets ridiculously slow when loading more than 10 million records from a SQL Server DB using pyodbc and mainly the function pandas.read_sql(query,pyodbc_conn). The following code takes up to 40-4…

compress a string in python 3?

I dont understand in 2.X it worked :import zlib zlib.compress(Hello, world)now i have a :zlib.compress("Hello world!") TypeError: must be bytes or buffer, not strHow can i compress my string …