在Python中进行文本分类的过采样?

我有一个文本数据框架需要进行分类。但我首先需要进行过采样。请查看下面的样本数据:

df=[['I am going to class today','I am going to class today','I am going to class today','I am going to class today','I am going to class today','I am going to class today','I am going to class today','I am going to class today','I am going to class today','I am going to class today','I am not going to class today','I am not going to class today','I am not going to class today','I am not going to class today'],['Positive','Positive','Positive','Positive','Positive','Positive','Positive','Positive','Positive','Positive','Negative','Negative','Negative','Negative']]df=pd.DataFrame(df)df=df.transpose()df.columns=['Features','Class']df          Features                       Class0   I am going to class today       Positive1   I am going to class today       Positive2   I am going to class today       Positive3   I am going to class today       Positive4   I am going to class today       Positive5   I am going to class today       Positive6   I am going to class today       Positive7   I am going to class today       Positive8   I am going to class today       Positive9   I am going to class today       Positive10  I am not going to class today   Negative11  I am not going to class today   Negative12  I am not going to class today   Negative13  I am not going to class today   Negativeoversample = RandomOverSampler(sampling_strategy='minority')# fit and apply the transformX_over, y_over = oversample.fit_resample(df['Features'], df['Class'])# summarize class distributionprint(Counter(y_over))

但这不起作用,并返回了ValueError: Expected 2D array, got 1D array instead:错误。我该如何对这些数据进行过采样?


回答:

我找到了问题所在。我需要重塑我的数据。

X_over, y_over = oversample.fit_resample(df['Features'].values.reshape(-1,1), df['Class'])

现在这起作用了。

Counter({'Positive': 10, 'Negative': 10})

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注