批量生成数据集_使用Python+sklearn实现绘制随机生成的多标签数据集

本示例描述了 make_multilabel_classification 数据集生成器的使用过程,该函数生成的数据中每个样本都包含两个特征的计数(总共最多50个),这两个特征在两个类中的分布不同。 样本点标记如下,其中Y表示类存在:
1 2 3 Color
Y N N Red
N Y N Blue
N N Y Yellow
Y Y N Purple
Y N Y Orange
Y Y N Green
Y Y Y Brown
星号标记每个类的预测样本;其大小反映选择该标签的概率。 左右示例强调 n_labels 参数:右图中的很多示例具有2或3个标签。 注意,这个二维示例是非常简陋的,一般来说,特征数量要远远大于“文档长度”,而这里我们有比词汇表大得多的文档。同样地,当 n_classes > n_features 时,特征区分特定类的可能性要小得多。 批量生成数据集_使用Python+sklearn实现绘制随机生成的多标签数据集_第1张图片 sphx_glr_plot_random_multilabel_dataset_001 输出:
The data was generated from (random_state=1013):Class   P(C)    P(w0|C) P(w1|C)
red     0.64    0.97    0.03
blue    0.06    0.60    0.40
yellow  0.30    0.09    0.91
import numpy as npimport matplotlib.pyplot as pltfrom sklearn.datasets import make_multilabel_classification as make_ml_clf
print(__doc__)
COLORS = np.array(['!','#FF3333',  # red'#0198E1',  # blue'#BF5FFF',  # purple'#FCD116',  # yellow'#FF7216',  # orange'#4DBD33',  # green'#87421F'   # brown
                   ])# 对多个make_multilabel_classification调用使用相同的随机种子# 确保分布相同
RANDOM_SEED = np.random.randint(2 ** 10)def plot_2d(ax, n_labels=1, n_classes=3, length=50):
    X, Y, p_c, p_w_c = make_ml_clf(n_samples=150, n_features=2,
                                   n_classes=n_classes, n_labels=n_labels,
                                   length=length, allow_unlabeled=False,
                                   return_distributions=True,
                                   random_state=RANDOM_SEED)
    ax.scatter(X[:, 0], X[:, 1], color=COLORS.take((Y * [1, 2, 4]
                                                    ).sum(axis=1)),
               marker='.')
    ax.scatter(p_w_c[0] * length, p_w_c[1] * length,
               marker='*', linewidth=.5, edgecolor='black',
               s=20 + 1500 * p_c ** 2,
               color=COLORS.take([1, 2, 4]))
    ax.set_xlabel('Feature 0 count')return p_c, p_w_c
_, (ax1, ax2) = plt.subplots(1, 2, sharex='row', sharey='row', figsize=(8, 4))
plt.subplots_adjust(bottom=.15)
p_c, p_w_c = plot_2d(ax1, n_labels=1)
ax1.set_title('n_labels=1, length=50')
ax1.set_ylabel('Feature 1 count')
plot_2d(ax2, n_labels=3)
ax2.set_title('n_labels=3, length=50')
ax2.set_xlim(left=0, auto=True)
ax2.set_ylim(bottom=0, auto=True)
plt.show()
print('The data was generated from (random_state=%d):' % RANDOM_SEED)
print('Class', 'P(C)', 'P(w0|C)', 'P(w1|C)', sep='\t')for k, p, p_w in zip(['red', 'blue', 'yellow'], p_c, p_w_c.T):
    print('%s\t%0.2f\t%0.2f\t%0.2f' % (k, p, p_w[0], p_w[1]))
脚本的总运行时间: ( 0 分 0.257 秒) 估计的内存使用量: 8 MB a470a9e1e72d5dee42d280df176bbd5e.png

下载python源代码:plot_random_multilabel_dataset.py

下载Jupyter notebook源代码:plot_random_multilabel_dataset.ipynb

由Sphinx-Gallery生成的画廊

4014ec6cfa73af819c7e85037d4a1376.png

☆☆☆为方便大家查阅,小编已将scikit-learn学习路线专栏文章统一整理到公众号底部菜单栏,同步更新中,关注公众号,点击左下方“系列文章”,如图:

批量生成数据集_使用Python+sklearn实现绘制随机生成的多标签数据集_第2张图片

欢迎大家和我一起沿着scikit-learn文档这条路线,一起巩固机器学习算法基础。(添加微信:mthler,备注:sklearn学习,一起进【sklearn机器学习进步群】开启打怪升级的学习之旅。)

批量生成数据集_使用Python+sklearn实现绘制随机生成的多标签数据集_第3张图片

你可能感兴趣的:(批量生成数据集)