1

i have a problem in Python. I am creating two numpy arrays from dict entries. I want to join those two numpy arrays in a specific way like this:

# create array with classes
probVec = filePickle['classID']
a = np.empty([0, 1])

for x in np.nditer(probVec):
    a = np.append(a,x)

timeVec = filePickle['start']
timeVec = np.asarray(timeVec)
b = np.empty([0, 1])

for x in np.nditer(timeVec):
    b = np.append(b,x)

# create input-vectors for clustering
c = np.vstack((b,a)).transpose()

Now, if i want to join them in a more specific way, like taking only specific items of array "probVec" to join them with the corresponding entry of array "timeVec" like this:

for x in np.nditer(probVec):
    if x == 3.0:
        a = np.append(a,x)

for x in np.nditer(timeVec):
    b = append with x values that have the same indices as the ones appended in the first loop

Because both arrays contain values corresponding to each other they have the same length. So my goal is something like this:

probVec = [2.0, 1.0, 3.0, 3.0, 4.0, 3.0...]

timeVec = [t1, t2, t3, t4, t5, t6...]

c = [[3.0 t3]
     [3.0 t4]
     [3.0 t6]
         .
         .
         .
     ]

I just don't know what's the best way to realize that.

1 Answer 1

1

Using a comparison operator on an array, like a == 3.0, you get a boolean array that can be used for indexing, selecting the rows where the condition is true.

In [87]: a = np.random.randint(low=1, high=4, size=10)   # example data

In [88]: a
Out[88]: array([3, 1, 3, 1, 1, 3, 2, 2, 2, 2])

In [89]: b = np.arange(10)

In [90]: c = np.column_stack((a, b))

In [91]: c[a == 3]
Out[91]: 
array([[3, 0],
       [3, 2],
       [3, 5]])
Sign up to request clarification or add additional context in comments.

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.