【发布时间】:2018-01-18 09:48:30
【问题描述】:
假设我的数据框中有 1010 行。现在我想使用train_test_split 拆分它们,以便前 1000 行用于训练数据,接下来 10 行用于测试数据。
# 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)
【问题讨论】:
-
欢迎来到 StackOverflow。请花时间阅读how to provide a great pandas example 上的这篇文章以及如何提供minimal, complete, and verifiable example 并相应地修改您的问题。 how to ask a good question 上的这些提示也可能有用。
标签: python-3.x pandas machine-learning spyder sklearn-pandas