-2

Lets say I've 1010 number of rows in my data frame. Now I want to split them using train_test_split so that first 1000 rows comes to train data and next 10 rows comes to test data.

# Natural Language Processing
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Restaurant_Reviews.tsv', delimiter = '\t', quoting = 3)
newset=pd.read_csv('Test.tsv',delimiter='\t',quoting=3)
frames=[dataset,newset]
res=pd.concat(frames,ignore_index=True)
# Cleaning the texts
import re
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
corpus = []
for i in range(0, 1010):
    review = re.sub('[^a-zA-Z]', ' ', res['Review'][i])
    review = review.lower()
    review = review.split()
    ps = PorterStemmer()
    review = [ps.stem(word) for word in review if not word in set(stopwords.words('english'))]
    review = ' '.join(review)
    corpus.append(review)
from sklearn.feature_extraction.text import CountVectorizer
cv=CountVectorizer(max_features=1500)
#X=cv.fit_transform(corpus).toarray()
X=corpus
y=res.iloc[:,1].values

# Splitting the dataset into the Training set and Test set
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.01, random_state = 0)
Moerwald
  • 10,448
  • 9
  • 43
  • 83
  • 1
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Aug 10 '17 at 08:47

1 Answers1

0

If you know that you need first 1000 samples in train and the last 10 samples in test, it is better to do it manually as train_test_split splits randomly.

X_train = X[:1000]
X_test = X[1000:]
y_train = y[:1000]
y_test = y[1000:]
Andrey Lukyanenko
  • 3,679
  • 2
  • 18
  • 21