I have a dataset in CSV format that consists of a training dataset with around 300 instances and a test dataset with around 100 instances. The issue is that the target variable (the column we want to predict) is completely missing in the test dataset other than this column the two datasets have the same commun columns, I want to split the data into "X_train, X_test, y_train, y_test" using the train_test_split function in Python's scikit-learn library. However, when I combine the two files and fill the missing target values using fillna, I'm concerned that it might alter the real results. What would be the best approach to handle this situation and split the data correctly while ensuring the integrity of the results.?


1 Answer 1


In your case, you don't need to combine the two datasets. The training dataset is used to train the model and the test dataset is used to evaluate the model's performance. The target variable is not supposed to be in the test dataset because it's what you want to predict with your model.

Here's how you can split your data:

import pandas as pd
from sklearn.model_selection import train_test_split

# Load your datasets
train_data = pd.read_csv('train.csv')
test_data = pd.read_csv('test.csv')

# Split your training data into X_train and y_train
X_train = train_data.drop('target_column', axis=1)
y_train = train_data['target_column']

# Your test data is already your X_test
X_test = test_data

# You don't have y_test because it's what you want to predict

After training your model with X_train and y_train, you can predict the target variable for the test data:

# Assume you have a model named 'model'
y_test_pred = model.predict(X_test)

y_test_pred is the predicted target variable for your test data. You don't have the actual values (y_test) to compare with the predicted values, because the target variable is what you want to predict in real-world scenarios.

But if you want a validation data to test your model then use the following:

X_train, X_valid, y_train, y_valid = train_test_split(train_data.drop('target_column', axis=1), train_data['target_column']), test_size = 0.2, random_state = 2023)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.