numpy iterating over multidimensional array - python

I am very new to numpy and I am trying to achieve the following in the most pythonic way. So, I have two arrays:
a=array([[0, 1, 2],[3,4,5]])
b=zeros(a.shape)
Now, what I would like is for each element in b for be one greater than the value of the corresponding element in a i.e b=a+1
I was wondering how this can be achieved in numpy.

The easiest way is the following:
b = a + 1
But if you want to iterate over the array yourself (although not recommended):
for i in range(len(a)):
for j in range(len(a[i])):
b[i][j] = a[i][j] + 1

Related

Using a for loop to sum every element in a numpy array

I am trying to make the transition from excel to python, but run in some troubles, with summing every element within an one dimensional array.
In excel this can be easily done, as in the attached image.
From excel I can clearly see the mathematical pattern for achieving this. My approach was to create a for loop, by indexing the array A.
This is my code:
import numpy as np
A = np.array([0.520094,0.850895E-1,-0.108374e1])
B = np.array([0]) #initialize array
B[0] = A[0]
A is equivalent to column A in excel & similarly B
Using a for loop to sum every element/row:
for i in range(len(A)):
i = i+1
B.append([B[i-1]+A[i]])
print(B)
This strategy doesn't work and keep getting erros. Any suggestion as to how I could make this work or is there a more elegant way of doing this?
Just use np.cumsum:
import numpy as np
A = np.array([0.520094,0.850895E-1,-0.108374e1])
cumsum = np.cumsum(A)
print(cumsum)
Output:
[ 0.520094 0.6051835 -0.4785565]
A manual approach would look like this:
A = np.array([0.520094,0.850895E-1,-0.108374e1])
B = [] # Create B as a list and not a numpy array, because it's faster to append
for i in range(len(A)):
cumulated = A[i]
if i > 0:
cumulated += B[i-1]
B.append(cumulated)
B = np.array(B) # Convert B from list to a numpy array

How to make efficient moving index in numpy array

I want to know how to code an efficient index over a numpy array. For the moment, I go over the array elements using repeated additions. For example, I have to make some loops over an array A like this:
import numpy as np
A = np.arange(0,100)
M = 10
for i in range(A.shape[0]-M):
B = []
for j in range(M):
value = A[i+j]
B.append(value)
Is there a way for me to get the values without repeatedly doing the i+j addition?

element wise combine list of numpy arrays

I am trying to do some linear combination of numpy arrays.
I have three lists of numpy arrays:
a = [np.random.normal(0,1, [1,2]), np.random.normal(0,1, [3,4]), np.random.normal(0,1, [10,11])]
b = [np.random.normal(0,1, [1,2]), np.random.normal(0,1, [3,4]), np.random.normal(0,1, [10,11])]
c = [np.random.normal(0,1, [1,2]), np.random.normal(0,1, [3,4]), np.random.normal(0,1, [10,11])]
I want to element-wise combine each element in each array in list a and b based on corresponding element's value of c , to get a new list d: say d_i = a_i * c_i + (1-c_i) *b_i(linear combination).
What I thought was to pick each element in each array in a and find corresponding elements in b and c and then combine. However, I found this is troublesome, inefficient and a bit stupid. Could anyone suggest a better way?
Well assuming all of your lists are the same length then I don't think that there is going to be anything much more efficient than
d = [a[i] * c[i] + (1-c[i]) * b[i] for i in range(len(a))]
Now if all you need to do is operate upon the list d one time then maybe you could speed things up with a generator comprehension?
d = (a[i] * c[i] + (1-c[i]) * b[i] for i in range(len(a)))
But at the end of the day there is no way to create a linear combination of elements in less than linear time.

Array with 4 elements: need to compare max+min and the other 2 elements

I have an array with 4 values in it, called array r, using the numpy array command.
from numpy import array, amax, amin
r = array([r1,r2,r3,r4]
I need to sum the max and the min of this array:
g_1 = amax(r)+amin(r)
Now I need to compare this value (g_1) with the sum of the two other elements of the array (I don't know what value is the max when I program this part of the code) and I don't know how to do that.
from numpy import sum
g_2 = sum(r) - g_1
comp = g_1 <= g_2
The sum of the other two elements of the array is simply the sum of all elements of the array, minus the max and min values: sum(r) - g_1
You might as well sort the array, will probably require less compares overall:
r_sort = np.sort(r)
g_1 = r_sort[0] + r_sort[-1]
g_2 = r_sort[1] + r_sort[2]
I tried to use a for and if to see if I can do it right and the code I wrote seems to work:
from numpy import array, amax, amin
r=array([r1,r2,r3,r4])
g_1=amax(r)+amin(r)
for j in range (size(r)):
if r[j] != amax(r) and r[j] != amin(r):
g_2+=r[j]
This code seems to return correctly the g_2 I was looking for. Not the best solution, what do you think about it?

Update 2D numpy array values

Is there a more efficient way to update the values of a multidimensional numpy array?
For example, I have a loop
for i in np.arange(5):
for j in np.arange(5):
if (i + j) % 2 == 0:
v[i,j] = v[i,j] + v[i, j + 1]
I was thinking on parallelizing this process later (with multiprocessing and Pool) but I can't imagine how. Maybe defining a function and using map but this is a 2D array and the operations depend on the element indexes.
Basically you are doing this:
You can do this in two lines using slice indexing:
v[0:5:2,0:5:2] += v[0:5:2,1:6:2] # even rows
v[1:5:2,1:5:2] += v[1:5:2,2:6:2] # odd rows

Categories

Resources