SMOTE初始化期望n_neighbors< = n_samples,但是n_samples< n_邻居 [英] SMOTE initialisation expects n_neighbors <= n_samples, but n_samples < n_neighbors

查看:1544
本文介绍了SMOTE初始化期望n_neighbors< = n_samples,但是n_samples< n_邻居的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经预先清理了数据,下面显示了前4行的格式:

I have already pre-cleaned the data, and below shows the format of the top 4 rows:

     [IN] df.head()

    [OUT]   Year    cleaned
         0  1909    acquaint hous receiv follow letter clerk crown...
         1  1909    ask secretari state war whether issu statement...
         2  1909    i beg present petit sign upward motor car driv...
         3  1909    i desir ask secretari state war second lieuten...
         4  1909    ask secretari state war whether would introduc...

我已经按如下方式调用train_test_split():

I have called train_test_split() as follows:

     [IN] X_train, X_test, y_train, y_test = train_test_split(df['cleaned'], df['Year'], random_state=2)
   [Note*] `X_train` and `y_train` are now Pandas.core.series.Series of shape (1785,) and `X_test` and `y_test` are also Pandas.core.series.Series of shape (595,)

然后我使用以下TfidfVectorizer和拟合/转换过程对X训练和测试数据进行矢量化处理:

I have then vectorized the X training and testing data using the following TfidfVectorizer and fit/transform procedures:

     [IN] v = TfidfVectorizer(decode_error='replace', encoding='utf-8', stop_words='english', ngram_range=(1, 1), sublinear_tf=True)
          X_train = v.fit_transform(X_train)
          X_test = v.transform(X_test)

我现在正处于通常应用分类器等的阶段(如果这是一组平衡的数据).但是,我初始化了imblearn的 SMOTE()类(执行过采样)...

I'm now at the stage where I would normally apply a classifier, etc (if this were a balanced set of data). However, I initialize imblearn's SMOTE() class (to perform over-sampling)...

     [IN] smote_pipeline = make_pipeline_imb(SMOTE(), classifier(random_state=42))
          smote_model = smote_pipeline.fit(X_train, y_train)
          smote_prediction = smote_model.predict(X_test)

...但这会导致:

     [OUT] ValueError: "Expected n_neighbors <= n_samples, but n_samples = 5, n_neighbors = 6.

我试图减少n_neighbors的数量,但无济于事,任何提示或建议将不胜感激.感谢您的阅读.

I've attempted to whittle down the number of n_neighbors but to no avail, any tips or advice would be much appreciated. Thanks for reading.

-------------------------------------------- -------------------------------------------------- --------------------------------------

完整追溯

数据集/数据框(df)在两列中包含2380行,如上面的df.head()所示. X_train以字符串列表(df['cleaned'])的格式包含1785行,y_train也包含字符串(df['Year'])格式的1785行.

The dataset/dataframe (df) contains 2380 rows across two columns, as shown in df.head() above. X_train contains 1785 of these rows in the format of a list of strings (df['cleaned']) and y_train also contains 1785 rows in the format of strings (df['Year']).

使用TfidfVectorizer()的后向量化:X_trainX_test分别从形状为((1785,)'和'(595,)'的pandas.core.series.Series转换为形状为((1785)的scipy.sparse.csr.csr_matrix ,126459)"和((595,126459)".

Post-vectorization using TfidfVectorizer(): X_train and X_test are converted from pandas.core.series.Series of shape '(1785,)' and '(595,)' respectively, to scipy.sparse.csr.csr_matrix of shape '(1785, 126459)' and '(595, 126459)' respectively.

关于类的数量:使用Counter(),我计算出有199个类(年),每个类的实例都附加到上述df['cleaned']数据的一个元素中,该元素包含一个字符串列表从文本语料库中提取.

As for the number of classes: using Counter(), I've calculated that there are 199 classes (Years), each instance of a class is attached to one element of aforementioned df['cleaned'] data which contains a list of strings extracted from a textual corpus.

此过程的目标是根据语音提示自动确定/猜测输入文本数据的年,十年或世纪(可以进行任何分类!).

The objective of this process is to automatically determine/guess the year, decade or century (any degree of classification will do!) of input textual data based on vocabularly present.

推荐答案

由于训练集中大约有200个班级和1800个样本,因此每个班级平均有9个样本.出现错误消息的原因是:a)数据可能不完全平衡,并且某些类的样本数少于6,并且b)邻居数为6.针对您的问题的一些解决方案:

Since there are approximately 200 classes and 1800 samples in the training set, you have on average 9 samples per class. The reason for the error message is that a) probably the data are not perfectly balanced and there are classes with less than 6 samples and b) the number of neighbors is 6. A few solutions for your problem:

  1. 计算199个类别中的最小样本数(n_samples),然后选择SMOTE类别的n_neighbors参数小于或等于n_samples.

  1. Calculate the minimum number of samples (n_samples) among the 199 classes and select n_neighbors parameter of SMOTE class less or equal to n_samples.

排除使用n_samples< n_neighbors使用SMOTE类的ratio参数.

Exclude from oversampling the classes with n_samples < n_neighbors using the ratio parameter of SMOTE class.

使用没有类似限制的RandomOverSampler类.

Use RandomOverSampler class which does not have a similar restriction.

组合3和4解决方案:创建使用SMOTERandomOversampler的管道,该管道满足smoted类的条件n_neighbors< = n_samples,并在不满足条件时使用随机过采样.

Combine 3 and 4 solutions: Create a pipeline that is using SMOTE and RandomOversampler in a way that satisfies the condition n_neighbors <= n_samples for smoted classes and uses random oversampling when the condition is not satisfied.

这篇关于SMOTE初始化期望n_neighbors&lt; = n_samples,但是n_samples&lt; n_邻居的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆