ValueError: Multi-dimensional indexing (e.g. `obj[:, None]`) is no longer supported. Convert to a numpy array before indexing instead

I am following this tutorial and I got into a value error, I tried looking to create numpy array but couldn’t find any.

Here’s the code

import ssl
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow import keras
from keras import layers

print(tf.version)

try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
# Legacy Python that doesn’t verify HTTPS certificates by default
pass
else:
# Handle target environment that doesn’t support HTTPS verification
ssl._create_default_https_context = _create_unverified_https_context

dataset_url = ‘http://storage.googleapis.com/download.tensorflow.org/data/petfinder-mini.zip
csv_file = ‘datasets/petfinder-mini/petfinder-mini.csv’

tf.keras.utils.get_file(‘petfinder_mini.zip’, dataset_url,
extract=True, cache_dir=‘.’)
dataframe = pd.read_csv(csv_file)

print(dataframe.head())

#In the original dataset, ‘AdoptionSpeed’ of ‘4’ indicates a pet was not adopted
dataframe[‘target’] = np.where(dataframe[‘AdoptionSpeed’] == 4, 0, 1)

#Drop unused features
dataframe = dataframe.drop(columns=[‘AdoptionSpeed’, ‘Description’])

train, val, test = np.split(dataframe.sample(frac=1), [int(0.8len(dataframe)), int(0.9len(dataframe))])

print(len(train), ‘training examples’)
print(len(val), ‘validation examples’)
print(len(test), ‘test examples’)

def df_to_dataset(dataframe, shuffle=True, batch_size=32):
df = dataframe.copy()
labels = df.pop(‘target’)
df = {key: value[:,tf.newaxis] for key, value in dataframe.items()}
ds = tf.data.Dataset.from_tensor_slices((dict(df), labels))
if shuffle:
ds = ds.shuffle(buffer_size=len(dataframe))
ds = ds.batch(batch_size)
ds = ds.prefetch(batch_size)
return ds

batch_size = 5
train_ds = df_to_dataset(train, batch_size=batch_size)

[(train_features, label_batch)] = train_ds.take(1)
print(‘Every feature:’, list(train_features.keys()))
print(‘A batch of ages:’, train_features[‘Age’])
print(‘A batch of targets:’, label_batch)

Hi @Ata_Tekeli, I have executed the given code in the colab i did not face any error. Please refer to this gist for working code. Could you please provide in which environment you are executing the code and facing the error. Thank You.

Hi! I replaced this line ‘df = {key: value[:,tf.newaxis] for key, value in dataframe.items()}’ with this ‘df = {key: np.array(value)[:,tf.newaxis] for key, value in dataframe.items()}’ and it works