close

How to fix NameError: name ‘X_train’ is not defined?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to fix NameError: name ‘X_train’ is not defined in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to fix NameError: name ‘X_train’ is not defined?

  1. How to fix NameError: name 'X_train' is not defined?

    You forgot to split the dataset into train and test sets.
    Import the library
    from sklearn.model_selection import train_test_split

  2. fix NameError: name 'X_train' is not defined

    You forgot to split the dataset into train and test sets.
    Import the library
    from sklearn.model_selection import train_test_split

Method 1

You forgot to split the dataset into train and test sets.

Import the library

from sklearn.model_selection import train_test_split

Add this line before classifier.fit()

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read