Reputation: 756
I need to work with an array to make some computations. I have the following data:
x = [[81, 68, 71, 71, 67, -72], [79, 77, 88, 88, 59, -71], [67, 71, 68, 68, 85, -66]]
and I would need to play with the data and extract from each column the number of positive and negative values so the output should be like:
positive_value = [3,3,3,3,0]
negative_vaue = [0,0,0,0,3]
I gave it a try using a for
loop with no success and with Numpy as well, but I do not really know how to use it.
What is the best way to get that result?
Upvotes: 8
Views: 35907
Reputation: 1823
The code in the answer from splash58 above works, and beautifully written:
matrix_2d = [[81, 68, 71, 71, 67, 72], [79, 77, 88, 88, 59, -71], [67, 71, 68, 68, 85, -66]]
pos = [sum(y >= 0 for y in x) for x in zip(*matrix_2d)]
neg = [len(matrix_2d) - x for x in pos]
print('Positive counts by columns: ', pos)
print('Nagetive counts by columns: ', neg)
However, if you want to look deeper in the working of algorithms, here is a simpler version, although lengthier and more verbose:
matrix_2d = [[81, 68, 71, 71, 67, 72], [79, 77, 88, 88, 59, -71], [67, 71, 68, 68, 85, -66]]
matrix_rows = len(matrix_2d)
matrix_cols = len(matrix_2d[0])
positive_counts = [0] * matrix_cols
negative_counts = [0] * matrix_cols
for col_idx in range(matrix_cols):
for row_idx in range(matrix_rows):
if matrix_2d[row_idx][col_idx] < 0:
negative_counts[col_idx] += 1
else:
positive_counts[col_idx] += 1
print('Positive counts by columns: ', positive_counts)
print('Nagetive counts by columns: ', negative_counts)
I change the input of the matrix_2d[0, 5], so the expected result is:
Positive counts by columns: [3, 3, 3, 3, 3, 1]
Nagetive counts by columns: [0, 0, 0, 0, 0, 2]
Upvotes: 0
Reputation:
twoD = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
pos = neg = 0
for row in twoD:
for col in row:
if col < 0:
neg += 1
else:
pos += 1
print('Number of positive integers are', pos, 'Number of negative integers are', neg)
Upvotes: 0
Reputation: 201
you can use count_nonzero function ,for this you may need to modify your array
>>> np.count_nonzero(np.eye(4)) #identity matrix
4
>>> np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]])
5
>>> np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]], axis=0)
array([1, 1, 1, 1, 1])
>>> np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]], axis=1)
array([2, 3])
Upvotes: 3
Reputation: 1180
>>> x = [[81, 68, 71, 71, 67, -72], [79, 77, 88, 88, 59, -71], [67, 71, 68, 68, 85, -66]]
>>> zipped = list(zip(*x))
>>> for items in zipped:
pos = len(list(filter(lambda i: i > 0, items)))
neg = len(list(filter(lambda i: i < 0, items)))
positive_values.append(pos)
negative_values.append(neg)
>>> positive_values
[3, 3, 3, 3, 3, 0]
>>> negative_values
[0, 0, 0, 0, 0, 3]
Upvotes: 1
Reputation: 26153
without any library
pos = [ sum(y>=0 for y in x) for x in zip(*mylist) ]
neg = [ len(mylist)-x for x in pos]
print(pos, neg)
Upvotes: 5
Reputation: 477065
Probably the most elegant way is to convert it to a numpy array first, then perform a condition >= 0
on it, and then calculate the sum(..)
over the first axis:
import numpy as np
np.sum(np.array(x) >= 0, axis=0)
This then yields:
>>> np.sum(np.array(x) >= 0, axis=0)
array([3, 3, 3, 3, 3, 0])
So by using np.array(x) >= 0
, we obtain a 2d-array of booleans:
>>> np.array(X) >= 0
array([[ True, True, True, True, True, False],
[ True, True, True, True, True, False],
[ True, True, True, True, True, False]], dtype=bool)
Since True
counts as one, and False
as zero, by calculing the sum per column, we thus count the number of positive numbers.
In case you want to count strictly positive numbers (so only larger than zero), you should omit the =
in >=
:
>>> np.sum(np.array(x) > 0, axis=0)
array([3, 3, 3, 3, 3, 0])
Upvotes: 20