How to calculate the sum of all columns of a 2D numpy array (efficiently)
Check out the documentation for numpy.sum
, paying particular attention to the axis
parameter. To sum over columns:
>>> import numpy as np
>>> a = np.arange(12).reshape(4,3)
>>> a.sum(axis=0)
array([18, 22, 26])
Or, to sum over rows:
>>> a.sum(axis=1)
array([ 3, 12, 21, 30])
Other aggregate functions, like numpy.mean
, numpy.cumsum
and numpy.std
, e.g., also take the axis
parameter.
From the Tentative Numpy Tutorial:
Many unary operations, such as computing the sum of all the elements
in the array, are implemented as methods of thendarray
class. By
default, these operations apply to the array as though it were a list
of numbers, regardless of its shape. However, by specifying theaxis
parameter you can apply an operation along the specified axis of an
array:
Most efficient way to sum huge 2D NumPy array, grouped by ID column?
you can use bincount():
import numpy as np
ids = [1,1,1,2,2,2,3]
data = [20,30,0,4,8,9,18]
print np.bincount(ids, weights=data)
the output is [ 0. 50. 21. 18.], which means the sum of id==0 is 0, the sum of id==1 is 50.
How do I sum the columns in 2D list?
Use zip
col_totals = [ sum(x) for x in zip(*my_list) ]
How to sum a 2d array in Python?
This is the issue
for row in range (len(input)-1):
for col in range(len(input[0])-1):
try
for row in range (len(input)):
for col in range(len(input[0])):
Python's range(x) goes from 0..x-1 already
range(...)
range([start,] stop[, step]) -> list of integersReturn a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
Adding Numpy Arrays in python using np.sum?
Use axis=0
in the sum function:
a = np.arange(15).reshape(-1,5)
# array([[ 0, 1, 2, 3, 4],
# [ 5, 6, 7, 8, 9],
# [10, 11, 12, 13, 14]])
np.sum(a, axis=0)
# array([15, 18, 21, 24, 27])
calculate sum of Nth column of numpy array entry grouped by the indices in first two columns?
I am pretty sure you can solve this problem in an easier way and I am not 100% sure that I understood you correctly, but here is some code that does what I think you want. If you have a possibility to check if the results are valid, I would suggest you do so.
import numpy as np
n = 20
q = np.zeros((20, 3))
q[:, -1] = np.linspace(0, 10, n)
check_matrix = np.array([[1, 1, 0, 0, 0, 0, 0, -0.7977, -0.243293],
[1, 1, 0, 0, 0, 0, 0, 1.5954, 0.004567],
[1, 2, 0, 0, 0, -1, 0, 0, 1.126557],
[2, 1, 0, 0, 0, 0.5, 0.86603, 1.5954, 0.038934],
[2, 1, 0, 0, 0, 2, 0, -0.7977, -0.015192],
[2, 2, 0, 0, 0, -0.5, 0.86603, 1.5954, 0.21394]])
check_matrix[:, :2] -= 1 # python indexing is zero based
matrices = np.zeros((n, 2, 2), dtype=np.complex_)
for i in range(2):
for j in range(2):
k_list = []
for k in range(len(check_matrix)):
if check_matrix[k][0] == i and check_matrix[k][1] == j:
k_list.append(check_matrix[k][8] *
np.exp(-1J * np.dot(q, check_matrix[k][2:5]
- check_matrix[k][5:8])))
matrices[:, i, j] = np.sum(k_list, axis=0)
NOTE: I changed your indices to have consistent
zero-based indexing.
Here is another approach where I replaced the k-loop with a vectored version:
for i in range(2):
for j in range(2):
k = np.logical_and(check_matrix[:, 0] == i, check_matrix[:, 1] == j)
temp = np.dot(check_matrix[k, 2:5] - check_matrix[k, 5:8], q[:, :, np.newaxis])[..., 0]
temp = check_matrix[k, 8:] * np.exp(-1J * temp)
matrices[:, i, j] = np.sum(temp, axis=0)
Related Topics
Python Global Exception Handling
Asyncio.Sleep() VS Time.Sleep()
Pyqt4 Wait in Thread for User Input from Gui
Multiple Linear Regression in Python
Angles Between Two N-Dimensional Vectors in Python
Display Fullscreen Mode on Tkinter
Failed to Get Convolution Algorithm. This Is Probably Because Cudnn Failed to Initialize,
How to Format Date String via Multiple Formats in Python
Remove Adjacent Duplicate Elements from a List
How to Run Scrapy from Within a Python Script
Why Does Defining _Getitem_ on a Class Make It Iterable in Python
Why Are Slice and Range Upper-Bound Exclusive
How to Set a Default Parameter Equal to Another Parameter Value
How to Write Inline If Statement for Print
Convert Datetime to Unix Timestamp and Convert It Back in Python