2

I am getting a TypeError when I try to eliminate the negative values in this array. I didn't have this problem in a previous iteration of this code, but I suppose that that particular data didn't have negative values in it. I don't understand how the data ends up being a tuple....is that a "feature" of np.loadtxt? Did I make it a tuple when I swapped the columns/rows in the zip function? I want to know how to fix this, but I'd also love an explanation as to how the data ended up being a tuple in the first place. Also, I feel like there is probably a more elegant way to do this than a "for" loop, and I'd appreciate any insight into that.

my_array = np.loadtxt(cw.my_Fname, delimiter=',', skiprows = 12, usecols = necessary_data_columns)

my_array = zip(*my_array)

i=0
for value in my_array[6]:
    if value < 0:
        my_array[6][i] = 0
    i += 1

TypeError: 'tuple' object does not support item assignment    
1
  • Can you post a minimal, sample source file? Commented Apr 25, 2014 at 13:36

4 Answers 4

3

If you want to replace all the negative numbers by zero just do:

my_array = np.clip(my_array, 0, np.inf)

If you only want to do it in a specific column:

my_array[:, col] = np.clip(my_array[:, col], 0, np.inf)

Note: you can transpose your numpy array with my_array.T instead of zip(*my_array), and the zip produces the tuples that you found out...

When you need to transpose the results from np.loadtxt(), just pass unpack=True and you get the same result:

my_array = np.loadtxt(cw.my_Fname, delimiter=',', skiprows=12,            
                      usecols=necessary_data_columns, unpack=True)
Sign up to request clarification or add additional context in comments.

1 Comment

+1 - Also, if you want to operate on the array in-place, you can specify the out argument for np.clip. E.g. np.clip(my_array, 0, np.inf, out=my_array)
2

Python built-in zip doesn't really understand numpy arrays.

Instead, use numpy.transpose:

In [11]: x
Out[11]: 
array([[ 1.,  2.],
       [ 3.,  4.],
       [ 5., -1.]])

In [12]: zip(*x)
Out[12]: [(1.0, 3.0, 5.0), (2.0, 4.0, -1.0)]

In [13]: x.transpose()
Out[13]: 
array([[ 1.,  3.,  5.],
       [ 2.,  4., -1.]])

After that you are free to modify your array as you wish.

2 Comments

So the zip part changed my list-of-lists into tuples? Thanks!
The zip actually understands the numpy arrays, but as a normal iterator where each row is one entry...
2

Your problem is that by doing

my_array = zip(*my_array)

You've turned my_array into a tuple, rather than a numpy array. Therefore, you're getting an error when you try to modify the tuple (tuples are immutable).

You seem to want to transpose the array. With numpy, that's just:

my_array = my_array.T

After that your code should work as is.


However, there's a better way to do this.

You're using numpy, so just do:

my_array = np.loadtxt(cw.my_Fname, delimiter=',', skiprows = 12, 
                      usecols = necessary_data_columns)
my_array = my_array.T

my_array[6][my_array[6] < 0] = 0

Or a bit more readable:

my_array = np.loadtxt(cw.my_Fname, delimiter=',', skiprows = 12, 
                      usecols = necessary_data_columns)

col = my_array[:,6]
col[col < 0] = 0

Comments

0

You will need to make all changes on a list and then you can convert it back to a tuple if you want:

my_array = np.loadtxt(cw.my_Fname, delimiter=',', skiprows = 12, usecols = necessary_data_columns)

my_array = zip(*my_array)

my_array = map(list, my_array)           #convert list of tuples to list of lists

i=0
for value in my_array[6]:
    if value < 0:
        my_array[6][i] = 0
    i += 1

Comments

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.