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)
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:]