.csv中的数据集不平衡

时间:2018-11-01 09:37:21

标签: python python-3.x csv

我有一个.csv格式的数据集,其中包含一些声学特征,我将用于性别语音识别以根据数据集预测其性别。

我的问题是我有25 samples of females152 samples of males的数据集。总计:177 samples (or rows)

当我使用算法训练数据时,这会给我带来麻烦。

我的问题是,如何平衡男女比例?我该如何ignore或减少152 to 25中男性样本的数量,以使男性与女性之间的比例可以是1:1?这样我有25位女性样本和25位男性样本。

我可以使用任何方法吗?

我的csv文件示例:

> 1 - female
> 
> .
> 
> 25 - female 
> 
> 26 - male
> 
> .
> 
> .
> 
> .
> 
> 177 - male

代码在python中。

2 个答案:

答案 0 :(得分:0)

使用pandas.DataFrame.sample

如果您已经使用熊猫加载了文件,那么您将遇到以下情况:

示例:

# 177 samples = 177 rows, with females in rows 1 to 25
df.shape 
(177,1)

# define a subset containg all the males data
males_all = df.loc[26:,:]

# randomly sample this and get 25 samples of MALES
sampled_males = males_all.sample(n = 25, random_state=0)

print(sampled_males.shape)
(25, 1)

答案 1 :(得分:-1)

如何创建一个男性声音的数据帧,然后使用df.Sample获得25个样本。

或更妙的是,如果创建两个数据帧,males_DF和females_DF,则可以执行以下操作:

sample_size = min(len(males_DF),len(females_DF))
male_sample_DF = males_DF.sample(sample_size)
female_sample_DF = females_DF.sample(sample_size)

(未经测试,可能需要更改)

相关问题