0

I have a numpy ndarray that I made using numpy.loadtxt. I want to pull an entire row from it based on a condition in the third column. Something like : if array[2][i] is meeting my conditions, then get array[0][i] and array [1][i] as well. I'm new to python, and all of the numpy features, so I'm looking for the best way to do this. Ideally, I'd like to pull 2 rows at a time, but I wont always have an even number of rows, so I imagine that is a problem

import numpy as np

'''
Created on Jan 27, 2013

@author:
'''
class Volume:

    f ='/Users/Documents/workspace/findMinMax/crapc.txt'
    m = np.loadtxt(f, unpack=True, usecols=(1,2,3), ndmin = 2)


    maxZ = max(m[2])
    minZ = min(m[2])
    print("Maximum Z value: " + str(maxZ))
    print("Minimum Z value: " + str(minZ))

    zIncrement = .5
    steps = maxZ/zIncrement
    currentStep = .5
    b = []

    for i in m[2]:#here is my problem
         while currentStep < steps: 
            if m[2][i] < currentStep and m[2][i] > currentStep - zIncrement:
                b.append(m[2][i]) 
            if len(b) < 2:
                currentStep + zIncrement

                print(b)

Here is some code that I did in java that is the general idea of what I want:

while( e < a.length - 1){
for(int i = 0; i < a.length - 1; i++){
        if(a[i][2] < stepSize && a[i][2] > stepSize - 2){

            x.add(a[i][0]);
            y.add(a[i][1]);
            z.add(a[i][2]);
        }
        if(x.size()  < 1){
            stepSize += 1;
        }
    }
}
1
  • That makes sense! I'm sorry I wasn't clear. I haven't learned to think like a programmer yet. I'm a bio student that has to learn this. :/ Commented Feb 3, 2013 at 22:20

2 Answers 2

2

First of all, you probably don't want to put your code in that class definition...

import numpy as np


def main():
    m = np.random.random((3, 4))
    mask = (m[2] > 0.5) & (m[2] < 0.8)  # put your conditions here
                                        # instead of 0.5 and 0.8 you can use
                                        # an array if you like
    m[:, mask]

if __name__ == '__main__':
    main()

mask is a boolean array, m[:, mask] is the array you want

m[2] is the third row of m. If you type m[2] + 2 you get a new array with the old values + 2. m[2] > 0.5 creates an array with boolean values. It is best to try this stuff out with ipython (www.ipython.org)

In the expression m[:, mask] the : means "take all rows", mask describes which columns should be included.

Update

Next try :-)

for i in range(0, len(m), 2):
    two_rows = m[i:i+2]
Sign up to request clarification or add additional context in comments.

9 Comments

I don't understand how I can go over the entire ndarray like this. Can you please elaborate?
I think that we are misunderstanding, its my fault, I'm not good with explaining ideas
what part did I not understand correctly? You might want to have a look at scipy.org/Tentative_NumPy_Tutorial
I think that we are misunderstanding, its my fault, I'm not good with explaining ideas. please correct my understanding. I believe my array looks like this -1 0 0.29 1 2 2.96 2 -2 3.0 so I thought m[2] would specify the whole 3rd column and m[2][0] would be the first value in that column? Basically, I want to grab the two rows at the top. I'm only using the incrementors because I want to only grab 2 rows at once, and the distance between them matters.
It might also help if you said what you are trying to accomplish
|
0

If you can write your condition as a simple function

def condition(value):
    # return True or False depending on value

then you could select your subarrays like this:

cond = condition(a[2])
subarray0 = a[0,cond]
subarray1 = a[1,cond]

2 Comments

can you elaborate on what the true or false does. I am not completely getting you. I'm sorry, I'm still learning.
I mean, it depends on what your condition is. If you're lucky, you can express the condition using standard functions, such that it can operate on the array a[2] without an elementwise loop, for example return value>0 or return (value.abs()>0.5)*(np.sin(value)<0.3) works elementwise also if value is an array without slowing the code by loops.

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.