Sum numbers of each row of a matrix Python
The issue you are having, is that you are already iterating over the elements so it is unnecessary to use it as an index:
x = sum(elemento)
It is generally considered bad form but to iterate over indexes you would use:
for i in range(len(lista)):
x = sum(lista[i])
However, without introducing any other modules, you can use map()
or a simple list comprehension:
>>> res = list(map(sum, lista)) # You don't need `list()` in Py2
>>> print(res)
[6, 15, 24]
Or
>>> res = [sum(e) for e in lista]
>>> print(res)
[6, 15, 24]
Perform sum over different slice of each row for 2D array
Let's run with your raveling idea. You can convert the indices of your array into raveled indices like this:
ind = np.stack((start, end), axis=0)
ind += np.arange(data.shape[0]) * data.shape[1]
ind = ind.ravel(order='F')
if ind[-1] == data.size:
ind = ind[:-1]
Now you can ravel the original array, and add.reduceat
on the segments thus defined:
np.add.reduceat(data.ravel(), ind)[::2] / np.subtract(end, start)
TL;DR
def row_mean(data, start, end):
ind = np.stack((start, end), axis=0)
ind += np.arange(data.shape[0]) * data.shape[1]
ind = ind.ravel(order='F')
if ind[-1] == data.size:
ind = ind[:-1]
return np.add.reduceat(data.ravel(), ind)[::2] / np.subtract(end, start)
Timings
Using the exact same arrays shown in @Divakar's answer, we get the following results (specific to my machine of course):
%timeit einsum_mean(data, start, end)
261 ms ± 2.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit broadcasting_mean(data, start, end)
405 ms ± 1.64 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit ragged_mean(data, start, end)
520 ms ± 3.68 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit row_mean(data, start, end)
45.6 ms ± 708 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
Somewhat surprisingly, this method runs 5-10x faster than all the others, despite doing lots of extra work by adding up all the numbers between the regions of interest. The reason is probably that it has extremely low overhead: the arrays of indices are small, and it only makes a single pass over a 1D array.
Finding the sum of column of all the rows in a matrix without using numpy
You should use another for loop for j
and reinitialize
the result when a new column started to be processed.
for j in range(col):
result = 0
for i in range(row):
result += mat1[i][j]
print(result)
Can I insert the code in a function? After I got the first column's answer, I can increment j outside the loop and then call the
function? I think it is known asrecursion
.
Yes, you can do this with recursion.
matrix = [[5, -1], [19, 8]]
row = 2
column = 2
def getResult(j, matrix, result):
if j >= column:
return result
s = sum([matrix[i][j] for i in range(row)])
result.append(s)
return getResult(j + 1, matrix, result)
result = getResult(0, matrix, [])
Output
> result
[24, 7]
Python: general sum over numpy rows
According to numpy.sum documentation, you cannot specify axis=1
for vectors as you would get a numpy AxisError
saying axis 1 is out of bounds for array of dimension 1
.
A possible workaround could be, for example, writing a dedicated function that checks the size before performing the sum. Please find below a possible implementation:
import numpy as np
M = np.array([[1, 4],
[2, 3]])
v = np.array([1, 4])
def sum_over_columns(input_arr):
if len(input_arr.shape) > 1:
return input_arr.sum(axis=1)
return input_arr.sum()
print(sum_over_columns(M))
print(sum_over_columns(v))
In a more pythonic way (not necessarily more readable):
def oneliner_sum(input_arr):
return input_arr.sum(axis=(1 if len(input_arr.shape) > 1 else None))
Sum Rows of 2D np array with List of Indices
I ended up coming up with my own solution when I realized I could sort my list without affecting my desired output. I used np.unique to determine the first indices of each element in the sorted list and then summed the rows between those indices. See below.
elements, indices = np.unique(data, return_counts=True)
row_summing = np.append([0], np.cumsum(indices))[:-1] #[0, index1, index2,...]
output = np.add.reduceat(matrix, row_summing, axis=0)
Related Topics
How to Write to an Existing Excel File Without Overwriting Data (Using Pandas)
Removing White Space Around a Saved Image
Find Largest Number from User Input
How to Adjust Padding With Cutoff or Overlapping Labels
How to Update Sqlalchemy Orm Object by a Python Dict
Typeerror: Missing 1 Required Positional Argument: 'Self'
How to Convert Column With String Type to Int Form in Pyspark Data Frame
What Causes a Python Segmentation Fault
Making a Discord Bot Change Playing Status Every 10 Seconds
Generate List of Quarters Betweeen Given Dates
How to Connect to a Remote Windows Machine to Execute Commands Using Python
Reading Columns of a Txt File on Python
Python How to Remove Escape Characters from a String
Plotting Data from Multiple Pandas Data Frames in One Plot
Retrieving Subfolders Names in S3 Bucket from Boto3
Finding Non-Numeric Rows in Dataframe in Pandas