Split data into testing and training and convert to csv or excel files

Question:

I have a large dataset (around 200k rows), i wanted to split the dataset into 2 parts randomly, 70% as the training data and 30% as the testing data. Is there a way to do this in python? Note I also want to get these datasets saved as excel or csv files in my computer. Thanks!

Asked By: huy

||

Answers:

Start by importing the following:

from sklearn.model_selection import train_test_split
import pandas as pd

In order to split you can use the train_test_split function from sklearn package:

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

where X, y is your taken from your original dataframe.

Later, you can export each of them as CSV using the pandas package:

X_train.to_csv(index=False)
X_test.to_csv(index=False)

Same goes for y data as well.

EDIT: as you clarified the question and required both X and y factors on the same file, you can do the following:

train, test = train_test_split(yourdata, test_size=0.3, random_state=42)

and then export them to csv as I mentioned above.

Answered By: My Koryto
from sklearn.model_selection import train_test_split
#split the data into train and test set
train,test = train_test_split(data, test_size=0.30, random_state=0)
#save the data
train.to_csv('train.csv',index=False)
test.to_csv('test.csv',index=False)
Answered By: Rajat Agarwal

I have title (research articles) field and, manually generated field where I extract important keywords from title. These data set is available in CSV and we would like to use this manually generated column as training data in python. On the basis of these training data we would like to further extract the important keywords from another set of articles in csv file. Is it possible in python? Regards,

Answered By: Bhaskar
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.