3

Working on a project that gives us free reign on what to use. So I decided I'd learn python for it.

To make this short, I want sum all the elements in a "row" of a matrix I'm reading in.

This is what my 2D array looks like after I read in my table from my text file.

['0000' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '1']
['0001' '0' '1' '0' '1' '0' '0' '0' '0' '1' '0' '0' '0' '0' '1']
['0010' '0' '1' '0' '1' '0' '0' '0' '0' '1' '0' '0' '0' '0' '1']
['0011' '0' '1' '0' '1' '0' '0' '0' '0' '1' '0' '0' '0' '0' '1']
['0100' '0' '0' '0' '0' '0' '1' '0' '1' '0' '0' '1' '0' '0' '1']
['0101' '0' '0' '1' '0' '0' '0' '1' '0' '0' '1' '0' '1' '1' '0']
['0110' '0' '0' '1' '0' '1' '0' '0' '0' '0' '1' '0' '1' '1' '0']
['0111' '0' '0' '1' '0' '0' '0' '0' '0' '0' '1' '0' '0' '1' '0']
['1000' '0' '0' '0' '0' '0' '1' '0' '1' '0' '0' '1' '0' '0' '1']
['1001' '1' '0' '0' '0' '0' '0' '1' '0' '0' '1' '0' '1' '1' '0']
['1010' '1' '0' '0' '0' '1' '0' '0' '0' '0' '1' '0' '1' '1' '0']
['1011' '1' '0' '0' '0' '0' '0' '0' '0' '0' '1' '0' '0' '1' '0']
['1100' '0' '0' '0' '0' '0' '1' '0' '1' '0' '0' '1' '0' '0' '1']
['1101' '0' '0' '0' '0' '0' '0' '1' '0' '0' '0' '0' '1' '1' '0']
['1110' '0' '0' '0' '0' '1' '0' '0' '0' '0' '0' '0' '1' '1' '0']
['1111' '0' '0' '0' '0' '0' '0' '0' '0' '0' '1' '0' '1' '1' '0']

I want to sum all elements of each row excluding index 0 (the 4 digit numbers). And then store those sums in a list.

This is what my list of sums should look like:

[1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)

However, this is what my code outputs:

number of rows: 16
number of cols: 15
num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]

I'm not sure what the error is, but I think it has to do with string/int conversion. Since my table is in strings, but I convert it to ints for summing. Debugging it shows the correct results, so I'm not sure where the error is.

Here is my code:

import numpy

print ("Reading..")
txtfile = open("test1.txt", "r")
print(txtfile.readline())
txtfile.close()

r= numpy.genfromtxt('test1.txt',dtype=str,skiprows=1)

for x in range (0,len(r)):
    print(r[x])

allTested = [0] * (len(r[0]) - 1)
num1s = [0] * (len(r[0]) - 1)

print("number of rows:", len(r))
print("number of cols:", len(r[0]))
print("num1s before:",num1s)

for x in range (0,len(r)):
    for y in range(1,len(r[0])):
        num1s[y-1] += int(r[x][y])


print("num1s after :",num1s)

2 Answers 2

10

You're over-complicating it. Instead try:

np.sum(array,axis=1).tolist()

this should return a list which contain the sum of all rows

ex:

import numpy as np
array = np.array([range(10),range(10),range(10),range(10)])

sum_ = np.sum(array,axis=1).tolist()

print sum_
print type(sum_) 

>> [45, 45, 45, 45]
>> <type 'list'>
Sign up to request clarification or add additional context in comments.

5 Comments

Haha brother I know I must be complicating a lot of things in python. Is that a command that generates a list? As in I don't need the nested for loop ?
yeah .tolist() will generate a list and you don't need a loop for that. just try it
Doesn't seem to be working. This is what I'm doing: w = numpy.sum(r,axis=1).tolist() and then printing w. I get "cannot perform reduce with flexible type" when I run it. Would you mind editing my code and showing me how you do it? I'm most likely implementing it incorrectly.
@user4927176 ok just declare your array as an array of floats this way r= numpy.genfromtxt('test1.txt',dtype=float, skiprows=1)
I would love to declare my array as int. But I want the first element of each row to remain the same size it is. If I do dtype= int it turns 0000 to 0, and 0001 to 1. Which I dont want. Is there a fix for that ?
2

Okay. Figured out the answer.

@wajid Farhani was close, but it wasn't working in my case.

His command for np.sum works, but I had to perform some indexing so I can ignore index 0 of every row. My issue was that I thought indexing 2D array was done by array[x][y], when it's array[x,y].

Fixed code:

import numpy

print ("Reading..")
txtfile = open("test1.txt", "r")
print(txtfile.readline())
txtfile.close()

r= numpy.genfromtxt('test1.txt',dtype=str,skiprows=1)

for x in range (0,len(r)):
    print(r[x])

allTested = [0] * (len(r[0]) - 1)
num1s = [0] * (len(r))

print("number of rows:", len(r))
print("number of cols:", len(r[0]))
print("num1s before:",num1s)

array = numpy.array(r,dtype=int)

s = numpy.sum(array[0:len(array),1:len(array[0])],axis=1).tolist()

print("num1s after :",s)

Correct output:

num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
num1s after : [1, 4, 4, 4, 4, 5, 5, 3, 4, 5, 5, 3, 4, 3, 3, 3]

2 Comments

happy that my answer helped you. One more thing, it's wajdi not wajid :)
You are over complicating array indexing. s = numpy.sum(array[:, 1:], axis=1).tolist() would do the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.