aokitools commited on
Commit ·
0ab938c
1
Parent(s): 7ad874f
commit
Browse files
app.py
CHANGED
|
@@ -432,6 +432,8 @@ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
|
|
| 432 |
result["output1"] = e_text1
|
| 433 |
result["output2"] = None
|
| 434 |
result["output3"] = ""
|
|
|
|
|
|
|
| 435 |
|
| 436 |
try:
|
| 437 |
s1 = e_text1
|
|
@@ -446,10 +448,15 @@ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
|
|
| 446 |
pass
|
| 447 |
|
| 448 |
list_vectors = []
|
| 449 |
-
list_groups = []
|
| 450 |
list_labels = []
|
| 451 |
|
| 452 |
-
list1 =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
|
| 454 |
import random
|
| 455 |
random.shuffle(list1)
|
|
@@ -468,12 +475,12 @@ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
|
|
| 468 |
s2 = None
|
| 469 |
|
| 470 |
if e_checkbox1:
|
| 471 |
-
s1 = ss1[
|
| 472 |
-
s2 = " ".join(ss1[
|
| 473 |
pass
|
| 474 |
else:
|
| 475 |
-
s1 =
|
| 476 |
-
s2 = " ".join(ss1[
|
| 477 |
pass
|
| 478 |
|
| 479 |
import io
|
|
@@ -482,9 +489,6 @@ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
|
|
| 482 |
import numpy
|
| 483 |
l1 = numpy.loadtxt(buf, delimiter=" ")
|
| 484 |
|
| 485 |
-
s3 = "0"
|
| 486 |
-
|
| 487 |
-
list_groups.append(s3)
|
| 488 |
list_labels.append(s1)
|
| 489 |
list_vectors.append(l1)
|
| 490 |
|
|
@@ -517,6 +521,8 @@ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
|
|
| 517 |
).fit_transform(X)
|
| 518 |
pass
|
| 519 |
|
|
|
|
|
|
|
| 520 |
if n_dimension == 3:
|
| 521 |
x = tsne[:,0]
|
| 522 |
y = tsne[:,1]
|
|
@@ -524,23 +530,95 @@ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
|
|
| 524 |
|
| 525 |
l1 = []
|
| 526 |
for i1 in range(len(x)):
|
| 527 |
-
s1 = "{0:.3f} {
|
| 528 |
l1.append(s1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
pass
|
|
|
|
| 530 |
result["output3"] = "\n".join(l1)
|
| 531 |
pass
|
| 532 |
else:
|
| 533 |
x = tsne[:,0]
|
| 534 |
y = tsne[:,1]
|
| 535 |
|
|
|
|
|
|
|
| 536 |
l1 = []
|
| 537 |
for i1 in range(len(x)):
|
| 538 |
-
s1 = "{0:.3f} {
|
| 539 |
l1.append(s1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 540 |
pass
|
|
|
|
| 541 |
result["output3"] = "\n".join(l1)
|
| 542 |
pass
|
| 543 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 544 |
import plotly.graph_objects as go
|
| 545 |
trace1 = None
|
| 546 |
if n_dimension == 3:
|
|
@@ -552,7 +630,7 @@ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
|
|
| 552 |
mode="markers",
|
| 553 |
marker=dict(
|
| 554 |
sizemode="diameter",
|
| 555 |
-
color =
|
| 556 |
colorscale = "Portland",
|
| 557 |
line=dict(color="rgb(255, 255, 255)"),
|
| 558 |
opacity=0.9,
|
|
@@ -568,7 +646,7 @@ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
|
|
| 568 |
mode="markers",
|
| 569 |
marker=dict(
|
| 570 |
sizemode="diameter",
|
| 571 |
-
color =
|
| 572 |
colorscale = "Portland",
|
| 573 |
line=dict(color="rgb(255, 255, 255)"),
|
| 574 |
opacity=0.9,
|
|
@@ -587,7 +665,7 @@ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
|
|
| 587 |
print(traceback.format_exc())
|
| 588 |
pass
|
| 589 |
|
| 590 |
-
return (result["output1"], result["output2"], result["output3"])
|
| 591 |
pass
|
| 592 |
|
| 593 |
# // ---------------------------------------------- // #
|
|
@@ -742,23 +820,17 @@ with gr.Blocks() as sample01:
|
|
| 742 |
|
| 743 |
|
| 744 |
with gr.Tab("次元削減"):
|
| 745 |
-
|
| 746 |
-
|
| 747 |
e_radio1 = gr.Radio(["2D", "3D"], value="2D", label="dimension")
|
| 748 |
e_radio2 = gr.Radio(["TSNE", "UMAP"], value="TSNE", label="algorithm")
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
gr.
|
| 754 |
-
フォーマット
|
| 755 |
-
- ベクトル数値列
|
| 756 |
-
- 空白区切り
|
| 757 |
-
- 31行以上~1000行まで
|
| 758 |
-
""")
|
| 759 |
|
| 760 |
-
|
| 761 |
-
e_button1.click(f_tsne, inputs=[e_text1, e_checkbox1, e_radio1, e_radio2], outputs=[e_text1, e_plot1, e_text2])
|
| 762 |
pass
|
| 763 |
|
| 764 |
|
|
|
|
| 432 |
result["output1"] = e_text1
|
| 433 |
result["output2"] = None
|
| 434 |
result["output3"] = ""
|
| 435 |
+
result["output4"] = ""
|
| 436 |
+
result["output5"] = None
|
| 437 |
|
| 438 |
try:
|
| 439 |
s1 = e_text1
|
|
|
|
| 448 |
pass
|
| 449 |
|
| 450 |
list_vectors = []
|
|
|
|
| 451 |
list_labels = []
|
| 452 |
|
| 453 |
+
list1 = []
|
| 454 |
+
index1 = 0
|
| 455 |
+
for i1 in s1.splitlines():
|
| 456 |
+
s1 = "{0:08} {1}".format(index1, i1.strip())
|
| 457 |
+
list1.append(s1)
|
| 458 |
+
index1 += 1
|
| 459 |
+
pass
|
| 460 |
|
| 461 |
import random
|
| 462 |
random.shuffle(list1)
|
|
|
|
| 475 |
s2 = None
|
| 476 |
|
| 477 |
if e_checkbox1:
|
| 478 |
+
s1 = ss1[1]
|
| 479 |
+
s2 = " ".join(ss1[2:])
|
| 480 |
pass
|
| 481 |
else:
|
| 482 |
+
s1 = ss1[0]
|
| 483 |
+
s2 = " ".join(ss1[1:])
|
| 484 |
pass
|
| 485 |
|
| 486 |
import io
|
|
|
|
| 489 |
import numpy
|
| 490 |
l1 = numpy.loadtxt(buf, delimiter=" ")
|
| 491 |
|
|
|
|
|
|
|
|
|
|
| 492 |
list_labels.append(s1)
|
| 493 |
list_vectors.append(l1)
|
| 494 |
|
|
|
|
| 521 |
).fit_transform(X)
|
| 522 |
pass
|
| 523 |
|
| 524 |
+
list_vectors2 = []
|
| 525 |
+
|
| 526 |
if n_dimension == 3:
|
| 527 |
x = tsne[:,0]
|
| 528 |
y = tsne[:,1]
|
|
|
|
| 530 |
|
| 531 |
l1 = []
|
| 532 |
for i1 in range(len(x)):
|
| 533 |
+
s1 = "{0} {1:.3f} {2:.3f} {3:.3f}".format(list_labels[i1], x[i1], y[i1], z[i1])
|
| 534 |
l1.append(s1)
|
| 535 |
+
|
| 536 |
+
l2 = []
|
| 537 |
+
l2.append(x[i1].astype(int))
|
| 538 |
+
l2.append(y[i1].astype(int))
|
| 539 |
+
l2.append(z[i1].astype(int))
|
| 540 |
+
|
| 541 |
+
list_vectors2.append(l2)
|
| 542 |
+
|
| 543 |
pass
|
| 544 |
+
l1.sort()
|
| 545 |
result["output3"] = "\n".join(l1)
|
| 546 |
pass
|
| 547 |
else:
|
| 548 |
x = tsne[:,0]
|
| 549 |
y = tsne[:,1]
|
| 550 |
|
| 551 |
+
|
| 552 |
+
|
| 553 |
l1 = []
|
| 554 |
for i1 in range(len(x)):
|
| 555 |
+
s1 = "{0} {1:.3f} {2:.3f}".format(list_labels[i1], x[i1], y[i1])
|
| 556 |
l1.append(s1)
|
| 557 |
+
|
| 558 |
+
l2 = []
|
| 559 |
+
l2.append(x[i1].astype(int))
|
| 560 |
+
l2.append(y[i1].astype(int))
|
| 561 |
+
list_vectors2.append(l2)
|
| 562 |
+
|
| 563 |
pass
|
| 564 |
+
l1.sort()
|
| 565 |
result["output3"] = "\n".join(l1)
|
| 566 |
pass
|
| 567 |
|
| 568 |
+
X = numpy.array(list_vectors2)
|
| 569 |
+
|
| 570 |
+
from sklearn.cluster import KMeans
|
| 571 |
+
|
| 572 |
+
list_squarederrors = []
|
| 573 |
+
for i1 in range(1, 32):
|
| 574 |
+
model = KMeans(n_clusters=i1, init="k-means++", n_init="auto")
|
| 575 |
+
model.fit(X)
|
| 576 |
+
list_squarederrors.append(model.inertia_)
|
| 577 |
+
pass
|
| 578 |
+
|
| 579 |
+
n_clusters = 32
|
| 580 |
+
n1 = (list_squarederrors[0] - list_squarederrors[-1]) * 0.06
|
| 581 |
+
n2 = (list_squarederrors[0] - list_squarederrors[-1]) * 0.5
|
| 582 |
+
|
| 583 |
+
index1 = 1
|
| 584 |
+
for i1 in list_squarederrors:
|
| 585 |
+
if n1 > i1:
|
| 586 |
+
n_clusters = index1
|
| 587 |
+
break
|
| 588 |
+
pass
|
| 589 |
+
index1 += 1
|
| 590 |
+
pass
|
| 591 |
+
|
| 592 |
+
import matplotlib.pyplot as plt
|
| 593 |
+
plt.cla()
|
| 594 |
+
plt.clf()
|
| 595 |
+
plt.plot(range(1, 32), list_squarederrors, marker='o')
|
| 596 |
+
|
| 597 |
+
plt.vlines(n_clusters, 0, list_squarederrors[0], color="r", linestyles="dotted")
|
| 598 |
+
plt.text(n_clusters, n2, "{0}".format(n_clusters), fontsize=32, verticalalignment="bottom", horizontalalignment="center")
|
| 599 |
+
|
| 600 |
+
plt.xlabel("number of clusters")
|
| 601 |
+
plt.ylabel("sum of squared errors")
|
| 602 |
+
plt.subplots_adjust(left=0.16, right=0.95, bottom=0.12, top=0.95)
|
| 603 |
+
|
| 604 |
+
result["output5"] = plt
|
| 605 |
+
|
| 606 |
+
model = KMeans(n_clusters=n_clusters, init="k-means++", n_init="auto")
|
| 607 |
+
model.fit(X)
|
| 608 |
+
clusters = model.predict(X)
|
| 609 |
+
|
| 610 |
+
list_cluster1 = []
|
| 611 |
+
index1 = 0
|
| 612 |
+
for i1 in clusters:
|
| 613 |
+
s1 = "{0} {1}".format(list_labels[index1], i1)
|
| 614 |
+
list_cluster1.append(s1)
|
| 615 |
+
index1 += 1
|
| 616 |
+
pass
|
| 617 |
+
list_cluster1.sort()
|
| 618 |
+
result["output4"] = "\n".join(list_cluster1)
|
| 619 |
+
|
| 620 |
+
|
| 621 |
+
from sklearn import preprocessing
|
| 622 |
import plotly.graph_objects as go
|
| 623 |
trace1 = None
|
| 624 |
if n_dimension == 3:
|
|
|
|
| 630 |
mode="markers",
|
| 631 |
marker=dict(
|
| 632 |
sizemode="diameter",
|
| 633 |
+
color = preprocessing.LabelEncoder().fit_transform(clusters),
|
| 634 |
colorscale = "Portland",
|
| 635 |
line=dict(color="rgb(255, 255, 255)"),
|
| 636 |
opacity=0.9,
|
|
|
|
| 646 |
mode="markers",
|
| 647 |
marker=dict(
|
| 648 |
sizemode="diameter",
|
| 649 |
+
color = preprocessing.LabelEncoder().fit_transform(clusters),
|
| 650 |
colorscale = "Portland",
|
| 651 |
line=dict(color="rgb(255, 255, 255)"),
|
| 652 |
opacity=0.9,
|
|
|
|
| 665 |
print(traceback.format_exc())
|
| 666 |
pass
|
| 667 |
|
| 668 |
+
return (result["output1"], result["output2"], result["output3"], result["output4"], result["output5"])
|
| 669 |
pass
|
| 670 |
|
| 671 |
# // ---------------------------------------------- // #
|
|
|
|
| 820 |
|
| 821 |
|
| 822 |
with gr.Tab("次元削減"):
|
| 823 |
+
e_text1 = gr.TextArea(show_label=True, label="入力ベクトル(空白区切り,31行以上~1000行まで)")
|
| 824 |
+
e_checkbox1 = gr.Checkbox(label="1カラム目をラベルとして設定")
|
| 825 |
e_radio1 = gr.Radio(["2D", "3D"], value="2D", label="dimension")
|
| 826 |
e_radio2 = gr.Radio(["TSNE", "UMAP"], value="TSNE", label="algorithm")
|
| 827 |
+
e_button1 = gr.Button("プロット")
|
| 828 |
+
e_plot1 = gr.Plot(show_label=True, label="プロット")
|
| 829 |
+
e_plot2 = gr.Plot(show_label=True, label="クラスタ数の推定")
|
| 830 |
+
e_text2 = gr.TextArea(show_label=True, label="次元削減後のベクトル")
|
| 831 |
+
e_text3 = gr.TextArea(show_label=True, label="クラスタリング結果")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 832 |
|
| 833 |
+
e_button1.click(f_tsne, inputs=[e_text1, e_checkbox1, e_radio1, e_radio2], outputs=[e_text1, e_plot1, e_text2, e_text3, e_plot2])
|
|
|
|
| 834 |
pass
|
| 835 |
|
| 836 |
|