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
  • 771
  • 2
  • 8
  • 20

4 Answers4

2

Use

subsequences = samples // x

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

var // x + var % x = var / x
Red
  • 26,798
  • 7
  • 36
  • 58
12ksins
  • 307
  • 1
  • 12
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?

Red
  • 26,798
  • 7
  • 36
  • 58
Dididombi
  • 43
  • 4
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
  • 16,526
  • 5
  • 37
  • 65
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)