Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have installed sklearn module in anaconda however the train_test_split is showing error. "name 'x_train' is not defined"

from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import TensorBoard
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

however in the code it is throwing error

train_df = pd.read_csv(r'fashion-mnist_train.csv')
test_df = pd.read_csv(r'fashion-mnist_test.csv')
train_data = np.array(train_df, dtype='float32')
test_data = np.array(test_df, dtype='float32')
x_train = train_data[:, 1:] / 255
y_train = train_data[:, 0]
x_test = test_data[:, 1:] / 255
y_test = test_data[:, 0]
x_train, x_validate, y_train, y_validate = train_test_split(
    x_train, y_train, test_size=0.2, random_state=12345,

when i run the cell with train test split the following error occurs:

NameError Traceback (most recent call last) 3 x_train, x_validate, y_train, y_validate = train_test_split( ----> 4 x_train, y_train, test_size=0.2, random_state=12345, NameError: name 'x_train' is not defined

please help! This is my first project in ML

In the NameError message, the parameters passed to train_test_split don't match exactly the code: There is a value 5 passed after the parameter random_state. – Eric Marchand Nov 20, 2021 at 11:15

There is the problem in dataset csv file naming convention in below code:

train_df = pd.read_csv(r'fashion-mnist_train.csv')
test_df = pd.read_csv(r'fashion-mnist_test.csv')

You need change to it to underscore( _ ) from hyphen ( - ) as below:

train_df = pd.read_csv(r'fashion_mnist_train.csv')
test_df = pd.read_csv(r'fashion_mnist_test.csv')

Also, make the same changes on these file names in your local directory from you are importing these files.

Please let us know if still issue persists.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.