bvk1ng commited on
Commit
630caba
·
1 Parent(s): 2998b36

Initial Commit

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+ import matplotlib.pyplot as plt
4
+
5
+ from sklearn.datasets import make_multilabel_classification
6
+ from sklearn.multiclass import OneVsRestClassifier
7
+ from sklearn.svm import SVC
8
+ from sklearn.decomposition import PCA
9
+ from sklearn.cross_decomposition import CCA
10
+ from matplotlib import cm
11
+
12
+ plt.switch_backend('agg')
13
+
14
+
15
+ def plot_hyperplane(clf, min_x, max_x, linestyle, linecolor, label):
16
+ """
17
+ This function is used to plot the hyperplane obtained from the classifier.
18
+
19
+ :param clf: the classifier model
20
+ :param min_x: the minimum value of X
21
+ :param max_x: the maximum value of x
22
+ :param linestyle: the style of line one needs in the plot.
23
+ :param label: the label for the hyperplane
24
+ """
25
+
26
+ w = clf.coef_[0]
27
+ a = -w[0] / w[1]
28
+ xx = np.linspace(min_x - 5, max_x + 5)
29
+ yy = a * xx - (clf.intercept_[0]) / w[1]
30
+ plt.plot(xx, yy, linestyle, color=linecolor, linewidth=2.5, label=label)
31
+
32
+
33
+
34
+ def multilabel_classification(n_samples:int, n_classes: int, n_labels: int, allow_unlabeled: bool, decompostion: str) -> "plt.Figure":
35
+ """
36
+ This function is used to perform multilabel classification.
37
+
38
+ :param n_samples: the number of samples.
39
+ :param n_classes: the number of classes for the classification problem.
40
+ :param n_labels: the average number of labels per instance.
41
+ :param allow_unlabeled: if set to True some instances might not belong to any class.
42
+ :param decompostion: the type of decomposition algorithm to use.
43
+
44
+ :returns: a matplotlib figure.
45
+ """
46
+
47
+ X, Y = make_multilabel_classification(
48
+ n_samples=n_samples,
49
+ n_classes=n_classes, n_labels=n_labels, allow_unlabeled=allow_unlabeled, random_state=42)
50
+
51
+ if decomposition == "PCA":
52
+ X = PCA(n_components=2).fit_transform(X)
53
+
54
+ else:
55
+ X = CCA(n_components=2).fit(X, Y).transform(X)
56
+
57
+ min_x = np.min(X[:, 0])
58
+ max_x = np.max(X[:, 0])
59
+
60
+
61
+ min_y = np.min(X[:, 1])
62
+ max_y = np.max(X[:, 1])
63
+
64
+ model = OneVsRestClassifier(SVC(kernel="linear"))
65
+ model.fit(X, Y)
66
+
67
+ fig, ax = plt.subplots(1, 1, figsize=(24, 15))
68
+
69
+ ax.scatter(X[:, 0], X[:, 1], s=40, c="gray", edgecolors=(0, 0, 0))
70
+ # colors = cm.rainbow(np.linspace(0, 1, n_classes))
71
+ colors = cm.get_cmap('tab10', 10)(np.linspace(0, 1, 10))
72
+
73
+ for nc in range(n_classes):
74
+ cl = np.where(Y[:, nc])
75
+ ax.scatter(X[cl, 0], X[cl, 1], s=np.random.random_integers(20, 200),
76
+ edgecolors=colors[nc], facecolors="none", linewidths=2, label=f"Class {nc+1}")
77
+
78
+ plot_hyperplane(model.estimators_[nc], min_x, max_x, "--", colors[nc], f"Boundary for class {nc+1}")
79
+ ax.set_xticks(())
80
+ ax.set_yticks(())
81
+
82
+ ax.set_xlim(min_x - .5 * max_x, max_x + .5 * max_x)
83
+ ax.set_ylim(min_y - .5 * max_y, max_y + .5 * max_y)
84
+
85
+ ax.legend()
86
+
87
+
88
+ return fig
89
+
90
+
91
+
92
+
93
+ with gr.Blocks() as demo:
94
+
95
+ n_samples = gr.Slider(100, 10_000, label="n_samples", info="the number of samples")
96
+ n_classes = gr.Slider(2, 10, label="n_classes", info="the number of classes that data should have.", step=1)
97
+ n_labels = gr.Slider(1, 10, label="n_labels", info="the average number of labels per instance", step=1)
98
+ allow_unlabeled = gr.Checkbox(True, label="allow_unlabeled", info="If set to True some instances might not belong to any class.")
99
+ decomposition = gr.Dropdown(['PCA', 'CCA'], label="decomposition", info="the type of decomposition algorithm to use.")
100
+
101
+ output = gr.Plot(label="Plot")
102
+
103
+ compute_btn = gr.Button("Compute")
104
+ compute_btn.click(fn=multilabel_classification, inputs=[n_samples, n_classes, n_labels, allow_unlabeled, decomposition],
105
+ outputs=output, api_name="multilabel")
106
+
107
+
108
+ demo.launch()