Home > front end >  Getting array of array objects, want array of the sequences
Getting array of array objects, want array of the sequences

Time:10-15

The array I'm aiming for looks like this one:

array([[[0.77777778],
        [0.77777778],
        [0.77777778],
        [0.83333333],
        [0.83333333],
        [0.83333333],
        [0.83333333]],

       [[0.77777778],
        [0.77777778],
        [0.83333333],
        [0.83333333],
        [0.83333333],
        [0.83333333],
        [0.83333333]],

       [[0.77777778],
        [0.83333333],
        [0.83333333],
        [0.83333333],
        [0.83333333],
        [0.83333333],
        [0.77777778]]])

I'm receiving the following array:

array([array([[0.77777778],
       [0.77777778],
       [0.77777778],
       [0.83333333],
       [0.83333333],
       [0.83333333],
       [0.83333333]]),
       array([[0.77777778],
       [0.77777778],
       [0.83333333],
       [0.83333333],
       [0.83333333],
       [0.83333333],
       [0.83333333]]),
       array([[0.77777778],
       [0.83333333],
       [0.83333333],
       [0.83333333],
       [0.83333333],
       [0.83333333],
       [0.77777778]])], dtype=object)

What kind of data manipulation could I do to achieve this?

For some reason Google Colab is appending the sequences properly to the first array then making an array of array objects for the second one, the code for creating the arrays is identical. It treats the second array as if I ran np.split to produce an array of array objects.

Here is the code to produce the above arrays:

def walk_forward_train(X, stepsBack, stepsForward, trainTestSplit):
  n_records = len(X)
  X = X[:]
  split = int(n_records*trainTestSplit-stepsForward)
  x = []
  y = []
  for i in range(n_records):
    x.append(X[i : i   stepsBack])  
    y.append(X[i   stepsBack : i   stepsBack   stepsForward])

  xtrain = x[:split]
  ytrain = y[:split]

  xtest = x[: n_records - stepsForward]
  ytest = y[: n_records - stepsForward]
    
  return np.asarray(xtrain), np.asarray(ytrain), np.asarray(xtest), np.asarray(ytest)

Edit: Here is a link to a Github I made to recreate this issue: https://github.com/Crayfi/Datasets

CodePudding user response:

your_old_array = array([array([[0.77777778],
       [0.77777778],
       [0.77777778],
       [0.83333333],
       [0.83333333],
       [0.83333333],
       [0.83333333]]),
       array([[0.77777778],
       [0.77777778],
       [0.83333333],
       [0.83333333],
       [0.83333333],
       [0.83333333],
       [0.83333333]]),
       array([[0.77777778],
       [0.83333333],
       [0.83333333],
       [0.83333333],
       [0.83333333],
       [0.83333333],
       [0.77777778]])], dtype=object)

req_array = your_old_array.astype(np.float64)

CodePudding user response:

Try

np.asarray(your_old_array)

output:

array([[[0.77777778],
        [0.77777778],
        [0.77777778],
        [0.83333333],
        [0.83333333],
        [0.83333333],
        [0.83333333]],

       [[0.77777778],
        [0.77777778],
        [0.83333333],
        [0.83333333],
        [0.83333333],
        [0.83333333],
        [0.83333333]],

       [[0.77777778],
        [0.83333333],
        [0.83333333],
        [0.83333333],
        [0.83333333],
        [0.83333333],
        [0.77777778]]], dtype=object)
  • Related