我有一个不平衡的数据集,它包含在一个名为city\u country的数据框中,它由5列组成:
在名为city\u country的数据帧中,类(event_id)是不平衡的。在测试不同文本分类器的预测能力之前,为了从tweet的内容(预处理)预测事件,我想对少数类进行过采样。在
重要的是,当我复制属于少数类的条目时,我要复制所有5列。在
到目前为止,我所做的(错误的)只是对tweet内容(预处理的和事件进行了过采样。在下面的代码中,我将tweets转换成向量(我不想这么做,但据我所知,我必须这样做),然后过度代表少数族裔类。这只会过采样矢量化tweet(x_words)和事件_id(y)。在
tfidf_words = TfidfVectorizer(sublinear_tf=True, min_df=0, norm='l2', encoding='latin-1', ngram_range=(1,1), stop_words='english')
x_words = tfidf_words.fit_transform(city_country.preprocessed).toarray()
# new dataframe 'label' that contains the event_id for each preprocessed tweet
y = city_country.event_id
x_train_words, x_test_words, y_train, y_test = train_test_split(x_words, y, test_size = 0.25, random_state = 0)
# Use SMOTE to oversample the minority classes
from imblearn.over_sampling import SMOTE
sm = SMOTE(random_state=12)
x_train_words_sm, y_train_words_sm = sm.fit_sample(x_train_words, y_train)
# Count the number of occurences in the y_train sets to ensure that the oversampling worked
from collections import Counter
class_check_woSMOTE = Counter(y_train)
class_check_words = Counter(y_train_words_sm)
据我所知,在imblearn.over_采样要求您输入实值(而不是字符串),并且只能输入2个值-“x”和“y”。在本例中,'x'是我的矢量化训练tweets集,'y'是我的事件标签。在
有没有一种方法可以让我简单地将数据帧分成训练集和测试集,然后对少数类的所有5列进行过采样,这样输出的是一个包含所有5列的更大的数据帧吗?然后我就可以用它来预测事件,并希望执行类似于vlookup的操作,这样我就可以用tweet各自的lat和long值加入tweet。在
突然袭击imblearn.over_采样稀疏向量可以接受为U。你可以进行过采样,然后分成你的测试/训练集。在
如果我正确地理解了你的问题,以下几点对我很有用
请尝试以下操作:
然后可以根据输出进行拆分
相关问题 更多 >
编程相关推荐