Implementing Neural Networks Project

I am getting an error on features_test_scaled that says:

Traceback (most recent call last):
** File “life_expectancy.py”, line 36, in **
** features_test_scaled = ct.transform(features_test)**
** File “/usr/local/lib/python3.6/dist-packages/sklearn/compose/_column_transformer.py”, line 580, in transform**
** if self._n_features > X.shape[1]:**
IndexError: tuple index out of range

I feel like I’ve looked over this quite a few times. Has anyone else had a similar issue?

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler
import tensorflow
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import InputLayer, Dense
from tensorflow.keras.optimizers import Adam

dataset = pd.read_csv(‘life_expectancy.csv’)

#print(dataset.head())
#print(dataset.describe())

df = dataset.copy()
df.drop(‘Country’, axis=1)

features = df.iloc[:, :-1]
labels = df.iloc[:,-1]
#print(features)

features = pd.get_dummies(features)

num_features = features.select_dtypes(include=[‘float64’, ‘int64’])
num_cols = num_features.columns

print(features.sum())
features_train, labels_train, features_test, labels_test = train_test_split(features, labels, random_state=27, test_size=0.3)

ct = ColumnTransformer([(‘only_numeric’, StandardScaler(), num_cols)], remainder=‘passthrough’)

features_train_scaled = ct.fit_transform(features_train)

features_test_scaled = ct.transform(features_test)

my_model = Sequential()
input = InputLayer(input_shape = (features.shape[1], ))
my_model.add(input)
my_model.add(Dense(64, activation=‘relu’))
my_model.add(Dense(1))

print(my_model.summary())

opt = Adam(learning_rate= 0.01)

my_model.compile(loss=‘mse’, metrics=‘mae’, opt=opt)

my_model.fit(features_train_scaled, labels_train, epochs= 20, batch_size= 5, verbose=1)

res_mse, res_mae = my_model.evaluate(features_train_scaled, labels_test, verbose=0)

print(res_mse, res_mae)

1 Like

Found the answer. Thanks!

how do I make new predictions with my own data??? Thanks you:)!!!

I have same problem. How did you find the answer?
Could you give me a tip?

I also found my mistake. :slightly_smiling_face:

hey
I’ve got the same problem what was ur mistakes?
how may i fix this?

the train_test_split() performs the split and returns four sequences in this order: x_train ,x_test , y_train ,y_test

Hence, instead of:
features_train, labels_train, features_test, labels_test = train_test_split(features, labels, random_state=27, test_size=0.3)

It should be:
features_train, features_test, labels_train, labels_test = train_test_split(features, labels, test_size = 0.3, random_state=27)

I hope this solves the problem

2 Likes

Solved it for me :slight_smile: Thank you!