Does anybody know how to record/capture data into a numpy array?

Viewed 167

I have for after alot of days of research found no answer, so i hope that we can find a solution here.

Matrix = np.random.randint(5, size=(60, 6, 6))
arr = np.random.randint(5, size=(1, 240))
arr1 = np.random.randint(5, size=(1, 240))
arr2 = np.random.randint(5, size=(1, 240))
arr3 = np.random.randint(5, size=(1, 240))

in the original data all the arr, arr1, arr2, arr3 consists of different data and got sizes of (1,240), but is sliced to size (1,72), kkk, vvv represents placement shifts for each iteration but with the fixed size of (1,72) but might not be relevant to solving this case.

I have this code:

kkk = 3
vvv = 79
    while True:
        for i in range(len(Matrix)):
            for j in range(len(Matrix[i])):
                for z in range(len(Matrix[j])):
                    pass
                    Matrix2 = np.concatenate((arr[kkk:vvv], arr1[kkk:vvv], arr2[kkk:vvv], arr3[kkk:vvv]), axis=0)
                    for v in range(len(Matrix2 )):
                        for vv in range(len(Matrix2 [v])):
                            for kv in np.isclose(Matrix[i, j, z], Matrix2 [v], rtol=0.005, atol=0.0):
                                def array_for(x, y):
                                    return np.asanyarray([kv for kv in np.isclose(x[i, j, z], y[v], rtol=0.005, atol=0.0)])

                                boolarray = array_for(Matrix, Matrix2)
    
    
        if j >= 5:
            kkk += 4
            vvv += 4
            if i == 59:
                break

The thing i am looking to do is record data from my boolarray, into an array of the size that:

def array_for(x, y):
return np.asanyarray([kv for kv in np.isclose(x[i, j, z], y[v], rtol=0.005, atol=0.0)])

This gives me.

The size of Matrix, is 60,6,6 The size of Matrix2, is 4,72 The size of boolarray, is 4,1, for each value in Matrix

[False False False False]

i want to record the boolarray data into an array of 1,6,6,4,72 and end up with 60 of these arrays so it becomes 60,6,6,4,72

The problem i found is that numpy is memorybased arrays, and if i try and append i get a bunch of 4,1 arrays which wouldnt be able to concate either in the loop or outside the loop, with anything other than itself.

Also trying to use np.insert, will yield me the same result.

And appending data to a python list, would create wrong shapes, like haveing a shape 1,1 and inside it 72 values.

Is it possible to use this logic and if so how to achieve the desired result:

For values in boolarray: 
    append values a list[],
    concate with each condition like:
    if boolarray == 71:
    np.concate(values, mask)
    out = 2,4,72 
    delete mask:
    continue np.concate(value, prevvalues)
    print(Full array)

Out = 60,6,6,4,72 

Matrix array looks like this:

[[0, 1, 0, 3, 4, 5],
[0, 2, 0, 3, 4, 5],
[2, 1, 1, 3, 4, 5],
[0, 1, 5, 3, 9, 5],
[4, 1, 0, 3, 8, 5],
[4, 1, 0, 3, 8, 5]]

Then with 60 layers of different values shown like above.

Matrix2 array looks like this:

 [[1, 1, 4, 5]] x72 times with different values

Let me know if i should explain anything further, and or if you have an idea of how to put data into an array dynamicly. Your help is much appreciated.

Desired array should look like this:

[[False False False False False False],
    [False True False False False False],
    [False False True False True False],
    [False True True False True False],
    [False False False True False True ],
    [False False False False False False]] 60x times = 60,6,6

[[False True True False],
    [True True True False],
    [False False True False],
    [False True True False],
    [False False False True],
    [False False False False]] 72x times 4, 72

each 4 values iterates through 1 cell of 6,6 at a time, so maybe i suppose the array might look like this 72, 4, 6, 6, 1 where 1 is 1 layer out of 60

So the last is likely how it would look like, im sorry i cant represent the figure much better as it is a complex shape, compared to that of 3 dimensional object, but the Np.isclose gives this kind of relation, by 1 cell at the time. and which yield: 10,368 values in total, which is 72times,4times,6times,6,times1.

1 Answers

I think this is what you want:

import numpy as np

#input data
Matrix = np.random.randint(5, size=(60, 6, 6))
arr = np.random.randint(5, size=(1, 240))
arr1 = np.random.randint(5, size=(1, 240))
arr2 = np.random.randint(5, size=(1, 240))
arr3 = np.random.randint(5, size=(1, 240))


# construct matrix 2
Matrix2=np.vstack((arr,arr1,arr2,arr3))

# slice matrix 2
kkk = 3
vvv = 79
Matrix2_slice=Matrix2[:,kkk:vvv]


# methods
def compare_value(x,y):
    return np.isclose(x, y, rtol=0.005, atol=0.0)

def compare_to_matrix2(x):
    return np.apply_along_axis(compare_value, 0, Matrix2_slice,x)

# output
vectorized_ctm = np.vectorize(compare_to_matrix2, signature='()->(l,p)')
output = vectorized_ctm(Matrix)

# some checks
print(output.shape)
print(output[0,0,0,0,0:10])

some remarks though:

  • the difference between kkk and vvv is 76, not 72 so the shape is a bit different than you requested (60,6,6,4,76)
  • Matrix2_slice is not used as a function input, which is not ideal
  • place shift per iteration (different values for kkk and vvv) are not included
Related