1

I have an array of values with a length of y (y = 7267). I am splitting the data based on x (x = 24) as shown below. I miss some values here because 7267/24 gives 302, not 302.8. This is because I am taking integer values. If I set int to float in the 3rd line, I get an error TypeError: 'float' object cannot be interpreted as an integer. How can I run the following code without losing any values in y? or maybe there is a better way of splitting the data like here?

import numpy as np

y = np.random.rand(7267)
samples = len(y)
x = 24

trim = samples % x
subsequences = int(samples/x)
sequence_trimmed = y[:samples - trim]
sequence_trimmed.shape = (subsequences, time_steps, 1)
ferrelwill
  • 617
  • 2
  • 5
  • 18

4 Answers4

2

Use

subsequences = samples // x

// is integer division so it'll return an integer exp.

var // x + var % x = var / x
Ann Zen
  • 25,080
  • 7
  • 31
  • 51
12ksins
  • 299
  • 1
  • 11
2

The modulo operation (%) is only defined for integers, that's why you cannot change it to float. An array also takes only (integer,integer) shapes. If I understand you correctly and you want to have an array that is shaped (x, z) for some z and that will definitely take all your data, how about introducing some zeros? So

z=samples//x+1
array=np.zeros(x*z)
array[:samples]=y
np.reshape(array,(x,z))

might do the trick?

Ann Zen
  • 25,080
  • 7
  • 31
  • 51
Dididombi
  • 31
  • 3
0
In [28]: import numpy as np
    ...:
    ...: y = list(range(7267))
    ...: samples = len(y)
    ...: x = 24
    ...:
    ...: trim = round(samples / x)
    ...: subsequences = int(samples/x)
    ...: sequence_trimmed = y[:samples - trim]
    ...:
bigbounty
  • 14,834
  • 4
  • 27
  • 58
0

This?

import numpy as np

y = np.random.rand(7267)
samples = len(y)
x = 24

trim = samples % x
subsequences = samples//x
sequence_trimmed = y[:samples - trim]
sequence_trimmed.shape = (subsequences, x)