Home > Mobile >  Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tens
Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tens

Time:10-19

I have three different sensors. For each I have the following shape according tf:

sensor1.shape = (1134, 100, 400)
sensor2.shape = (1134, 100, 400)
sensor3.shape = (1134, 100, 400)

Each tensor has the same size.

In the next step I stack these 3 sensors in order to get one with just 3 dimensions:

final = tf.stack([sensor1, sensor2, sensor3], axis=-1)

final.shape

TensorShape([1134, 100, 400, 3])

Now I want to do a train test split

X_train, X_test, y_train, y_test = train_test_split(final, y, test_size=0.25, random_state=42, stratify=y)

but following error occurs: Only integers, slices (:), ellipsis (...), tf.newaxis (None) and scalar tf.int32/tf.int64 tensors are valid indices, got array([ 219, 928, 636, 862, 606, 793, 621, 118, 1047, 635, .. Any ideas? What's wrong?

Edit: Current working solution with just 1 sensor. So I think the problem are the three 3 dimensions.

sensor1.shape
(100, 400, 1134)

final = np.moveaxis(sensor1, -1, 0)

final.shape
(1134, 100, 400)

X_train, X_test, y_train, y_test = train_test_split(final, y, test_size=0.25, random_state=42, stratify=y)

print("X:", final.shape)
print("y:", y.shape)

print("Xtrain:", X_train.shape)
print("y_train:", y_train.shape)
print("X_test:", X_test.shape)
print("y_test:", y_test.shape)

X: (1134, 100, 400)
y: (1134,)
Xtrain: (850, 100, 400)
y_train: (850,)
X_test: (284, 100, 400)
y_test: (284,)

X_train = tf.expand_dims(X_train, axis=-1)
X_test = tf.expand_dims(X_test, axis=-1)

print("X_train shape new:", X_train.shape)
X_train shape new: (850, 100, 400, 1)

CodePudding user response:

Trying using numpy and train_test_split for everything:

import tensorflow as tf
import numpy as np
from sklearn.model_selection import train_test_split

sensor1 = np.random.random((1134, 100, 400))
sensor2 = np.random.random((1134, 100, 400))
sensor3 = np.random.random((1134, 100, 400))
Y = np.random.random((1134))
final = np.stack([sensor1, sensor2, sensor3], axis=-1)

X_train, X_test, y_train, y_test = train_test_split(final, Y, test_size=0.25, random_state=42)

  • Related