Ex 4: Plot randomly generated multilabel dataset 多標籤數據集
這個範例示範了如何使用make_multilabel_classification函數,每個樣本都包含兩個特徵的計數(總共最多50個), 這兩個特徵在兩個類別的每個類別中的分佈不同。
點的標記如下,其中Y表示類別是否存在:
設定分類的顏色
1
COLORS = np.array(['!',
2
'#FF3333', # red
3
'#0198E1', # blue
4
'#BF5FFF', # purple
5
'#FCD116', # yellow
6
'#FF7216', # orange
7
'#4DBD33', # green
8
'#87421F' # brown
9
])
Copied!
從0~1024中隨機設定種子,使用相同的隨機種子多次調用make_ml_clf,確保相同的分佈
1
RANDOM_SEED = np.random.randint(2 ** 10)
Copied!

(一)Make multilabel classification

使用make_ml_clf生成隨機的多標籤分類,其中回傳四個變數: X 表示產生的樣本 Y 表示標籤的集合 p_c 表示每個分類被選中的機率 p_w_c 表示給定每一個分類,特徵被選中的機率
1
X, Y, p_c, p_w_c = make_ml_clf(n_samples=150, n_features=2,
2
n_classes=n_classes, n_labels=n_labels,
3
length=length, allow_unlabeled=False,
4
return_distributions=True,
5
random_state=RANDOM_SEED)
6
7
ax.scatter(X[:, 0], X[:, 1], color=COLORS.take((Y * [1, 2, 4]
8
).sum(axis=1)),
9
marker='.'
Copied!
星號標記每個類別的預期樣本;它的大小反映了選擇該類別標籤的可能性。
1
ax.scatter(p_w_c[0] * length, p_w_c[1] * length,
2
marker='*', linewidth=.5, edgecolor='black',
3
s=20 + 1500 * p_c ** 2,
4
color=COLORS.take([1, 2, 4]))
Copied!

(二)顯示圖形與結果

請注意,由於此範例過於簡化:特徵的數量通常會比“文檔長度”大得多,而此範例的文檔長度比特徵量大得多。也就是說n_classes> n_features,特徵要分辨特定分類的機率相對小得很多。

(三)完整程式碼

Python source code:plot_random_multilabel_dataset.py
1
import numpy as np
2
import matplotlib.pyplot as plt
3
4
from sklearn.datasets import make_multilabel_classification as make_ml_clf
5
6
print(__doc__)
7
8
COLORS = np.array(['!',
9
'#FF3333', # red
10
'#0198E1', # blue
11
'#BF5FFF', # purple
12
'#FCD116', # yellow
13
'#FF7216', # orange
14
'#4DBD33', # green
15
'#87421F' # brown
16
])
17
18
# Use same random seed for multiple calls to make_multilabel_classification to
19
# ensure same distributions
20
RANDOM_SEED = np.random.randint(2 ** 10)
21
22
23
def plot_2d(ax, n_labels=1, n_classes=3, length=50):
24
X, Y, p_c, p_w_c = make_ml_clf(n_samples=150, n_features=2,
25
n_classes=n_classes, n_labels=n_labels,
26
length=length, allow_unlabeled=False,
27
return_distributions=True,
28
random_state=RANDOM_SEED)
29
30
ax.scatter(X[:, 0], X[:, 1], color=COLORS.take((Y * [1, 2, 4]
31
).sum(axis=1)),
32
marker='.')
33
ax.scatter(p_w_c[0] * length, p_w_c[1] * length,
34
marker='*', linewidth=.5, edgecolor='black',
35
s=20 + 1500 * p_c ** 2,
36
color=COLORS.take([1, 2, 4]))
37
ax.set_xlabel('Feature 0 count')
38
return p_c, p_w_c
39
40
41
_, (ax1, ax2) = plt.subplots(1, 2, sharex='row', sharey='row', figsize=(8, 4))
42
plt.subplots_adjust(bottom=.15)
43
44
p_c, p_w_c = plot_2d(ax1, n_labels=1)
45
ax1.set_title('n_labels=1, length=50')
46
ax1.set_ylabel('Feature 1 count')
47
48
plot_2d(ax2, n_labels=3)
49
ax2.set_title('n_labels=3, length=50')
50
ax2.set_xlim(left=0, auto=True)
51
ax2.set_ylim(bottom=0, auto=True)
52
53
plt.show()
54
55
print('The data was generated from (random_state=%d):' % RANDOM_SEED)
56
print('Class', 'P(C)', 'P(w0|C)', 'P(w1|C)', sep='\t')
57
for k, p, p_w in zip(['red', 'blue', 'yellow'], p_c, p_w_c.T):
58
print('%s\t%0.2f\t%0.2f\t%0.2f' % (k, p, p_w[0], p_w[1]))
Copied!
Last modified 1yr ago