Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding value of single numpy array to all columns in other numpy array [duplicate]

I have two numpy arrays

A= array([[1,2,3,4],
          [5,6,7,8],
          [9,10,11,12]])

B = array([10,20,30])

and I want to generate array C:

C = array([11,12,13,14],
          [25,26,27,28],
          [39,40,41,42]])

I have tried some ways.. but they seem very inefficient. Is there any way this can be done efficiently?

like image 682
H.Choi Avatar asked Mar 26 '19 10:03

H.Choi


People also ask

How do I copy a NumPy array to another NumPy array?

Conclusion: to copy data from a numpy array to another use one of the built-in numpy functions numpy. array(src) or numpy. copyto(dst, src) wherever possible.

How do you repeat a value in a NumPy array?

NumPy: repeat() function The repeat() function is used to repeat elements of an array. Input array. The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis.

How do I add NumPy arrays to each other?

To add the two arrays together, we will use the numpy. add(arr1,arr2) method. In order to use this method, you have to make sure that the two arrays have the same length. If the lengths of the two arrays are​ not the same, then broadcast the size of the shorter array by adding zero's at extra indexes.

How do I add a column to a NumPy array?

You can use one of the following methods to add a column to a NumPy array: The following examples show how to use each method in practice. We can use the following syntax to add a column to the end of the NumPy array: We can use the following syntax to insert a new column before the column in index position 2 of the NumPy array:

How to append values to a NumPy array?

Append Values to a Numpy Array. You can use the numpy append () function to append values to a numpy array. In this tutorial, we’ll look at the syntax and usage of the numpy append () function through some examples.

How to join two arrays in NumPy?

Because two 2-dimensional arrays are included in operations, you can join them either row-wise or column-wise. Mainly NumPy () allows you to join the given two arrays either by rows or columns. Let us see some examples to understand the concatenation of NumPy.

How to create two-dimensional array in NumPy?

Firstly, import NumPy package : Creating a NumPy array using arrange (), one-dimensional array eventually starts at 0 and ends at 8. In this you can even join two exhibits in NumPy, it is practiced utilizing np.concatenate, np.hstack.np.np.concatenate it takes tuples as the primary contention. Now it can also be used for two-dimensional array also:


2 Answers

This can be done with a little help from broadcasting by adding a new axis to B (either with None or with np.newaxis) so that they have compatible shapes, and B is broadcastable accross the larger array A:

A + B[:,None]

array([[11, 12, 13, 14],
       [25, 26, 27, 28],
       [39, 40, 41, 42]])
like image 104
yatu Avatar answered Nov 03 '22 03:11

yatu


pleas look at this example :

    ethernet_devices = [1, [7], [2], [8374163], [84302738]]
    usb_devices = [1, [7], [1], [2314567], [0]]

    all_devices = [x + y for x, y in zip(ethernet_devices, usb_devices)]

Sources: https://therenegadecoder.com/code/how-to-sum-elements-of-two-lists-in-python/

like image 33
abdoulsn Avatar answered Nov 03 '22 04:11

abdoulsn