我写了一个文本分类程序。当我运行该程序时,它会出现一个错误,如下面的屏幕截图所示:
ValueError:对于n_samples=0,test_size=0.2和train_size=None,最终的火车将是空的。调整上述任何参数。
这是我的代码:
from sklearn.model_selection import train_test_split
from gensim.models.word2vec import Word2Vec
from sklearn.preprocessing import scale
from sklearn.linear_model import SGDClassifier
import nltk, string, json
import numpy as np
def cleanText(corpus):
reviews = []
for dd in corpus:
#for d in dd:
try:
words = nltk.word_tokenize(dd['description'])
words = [w.lower() for w in words]
reviews.append(words)
#break
except:
pass
return reviews
with open('C:\\NLP\\bad.json') as fin:
text = json.load(fin)
neg_rev = cleanText(text)
with open('C:\\NLP\\good.json') as fin:
text = json.load(fin)
pos_rev = cleanText(text)
#1 for positive sentiment, 0 for negative
y = np.concatenate((np.ones(len(pos_rev)), np.zeros(len(neg_rev))))
x_train, x_test, y_train, y_test = train_test_split(np.concatenate((pos_rev, neg_rev)), y, test_size=0.2)
我正在使用的数据可以在这里获得:
我将如何修复这个错误?
发布于 2020-08-11 11:30:10
得到了同样的错误:ValueError: With n_samples=0, test_size=0.2 and train_size=None, the resulting train set will be empty. Adjust any of the aforementioned parameters.
--在我的例子中--数据路径无效。检查加载文件的路径是否存在,或者读取文件的变量是否保存任何数据。
发布于 2021-05-14 12:08:58
得到了同样的错误:ValueError: With n_samples=0, test_size=0.2 and train_size=None, the resulting train set will be empty. Adjust any of the aforementioned parameters
。发生错误的原因是找不到数据文件。如果您使用jupyter记事本,您不能使用Dataframe来定义X和y。因此,使用dataframe来定义x和y,问题就解决了。在另一种情况下,由于找不到数据,所以会发生这种情况。因此,检查给定的路径,数据是存在的。
发布于 2022-02-25 04:26:01
我认为下面的代码片段可以帮助您解决这个问题。
ad_data=pd.read_csv("advertising.csv")from sklearn.model_selection import train_test_split
x=ad_data[['Daily Time Spent on Site', 'Age', 'Area Income','Daily Internet Usage', 'Male']]
y=ad_data['Clicked on Ad']
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=42)
https://stackoverflow.com/questions/60043276
复制相似问题