如何使用make_blobs创建平衡样本数据?

时间:2019-05-19 12:38:00

标签: python scikit-learn

我正在使用sklearn包中的make_blobs。

from sklearn.datasets.samples_generator import make_blobs

我想创建具有不平衡功能的样本数据。示例我需要FeatureA的400和FeatureB的50。

下面的代码默认情况下会同时生成两个相同的特征:-

X, y = make_blobs(n_samples=450, centers=2, cluster_std=[10.0, 2],random_state=22,n_features=2)

以下是为从上述代码生成的数据创建的计数图: enter image description here

请提出建议,我该如何达到要求?

1 个答案:

答案 0 :(得分:0)

我想您要创建具有预定std的两类数据,并以400为中心,其他数据以50为中心。我设置为“ centers = None”。对吗?我使用此代码,它给出了您想要的。请参考此链接: sklearn.datasets.make_blobs

import numpy as np
import matplotlib.pyplot as plt

X, y = make_blobs(n_samples=[400,50], centers=None, cluster_std=[10.0, 2],random_state=22,n_features=2)
print(y)
Zero0=np.where(y == 0)[0]
One1=np.where(y == 1)[0]
print(Zero0)
print(One1)
plt.scatter(X[Zero0,0],X[Zero0,1],color=['red'])
plt.scatter(X[One1,0],X[One1,1],color=['green'])
plt.show()
plt.scatter(X[:,0],X[:,1])
plt.show()

enter image description here