FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
PythonMachineLearningExamples/p330_dendrogram.py at master · rrlyman/PythonMachineLearningExamples · GitHub
rrlyman
/
PythonMachineLearningExamples
Public
Notifications
You must be signed in to change notification settings
Fork
15
Star
24
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
PythonMachineLearningExamples
/
p330_dendrogram.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
100 lines (71 loc) · 3.24 KB
Breadcrumbs
PythonMachineLearningExamples
/
p330_dendrogram.py
Copy path
File metadata and controls
100 lines (71 loc) · 3.24 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'''
Created on Jul 21, 2016
A dendrogram is a diagram that shows the cluster composition of a dataset by
by showing cluster as levels in a tree. At each node, the cluster is
shown as subclusters. This is somewhat like the separation of the data by
a Decision Tree.
from Python Machine Learning by Sebastian Raschka under the following license
The MIT License (MIT)
Copyright (c) 2015, 2016 SEBASTIAN RASCHKA (mail@sebastianraschka.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@author: richard lyman
'''
import
ocr_utils
import
matplotlib
.
pyplot
as
plt
##############################################
# separate the original images by cluster
# print(km.cluster_centers_.shape)
n
=
300
variables
=
[
'X'
,
'Y'
,
'Z'
]
labels
=
[
'ID_0'
,
'ID_1'
,
'ID_2'
,
'ID_3'
,
'ID_4'
]
chars_to_train
=
range
(
48
,
51
)
columnsXY
=
(
9
,
17
)
column_str
=
'column_sum{}'
.
format
(
list
(
columnsXY
))
input_filters_dict
=
{
'm_label'
:
chars_to_train
,
'font'
:
'E13B'
}
# output the character label and the image and column sums
output_feature_list
=
[
'm_label'
,
'image'
,
column_str
]
# read the complete image (20x20) = 400 pixels for each character
ds
=
ocr_utils
.
read_data
(
input_filters_dict
=
input_filters_dict
,
output_feature_list
=
output_feature_list
,
random_state
=
0
)
y
=
ds
.
train
.
features
[
0
][:
n
]
X_image
=
ds
.
train
.
features
[
1
][:
n
]
X
=
ds
.
train
.
features
[
2
][:
n
]
from
scipy
.
spatial
.
distance
import
pdist
row_dist
=
pdist
(
X
,
metric
=
'euclidean'
)
print
(
row_dist
)
from
scipy
.
cluster
.
hierarchy
import
linkage
#method 1 using a condensed matrix
row_clusters
=
linkage
(
row_dist
,
method
=
'complete'
,
metric
=
'euclidean'
)
print
(
row_clusters
)
#method 2 using raw data
row_clusters
=
linkage
(
X
,
method
=
'complete'
,
metric
=
'euclidean'
)
print
()
print
(
row_clusters
)
from
scipy
.
cluster
.
hierarchy
import
dendrogram
# make dendrogram black (part 1/2)
# from scipy.cluster.hierarchy import set_link_color_palette
# set_link_color_palette(['black'])
row_dendr
=
dendrogram
(
row_clusters
,
p
=
12
,
truncate_mode
=
'lastp'
)
plt
.
tight_layout
()
plt
.
ylabel
(
'Euclidean distance'
)
#plt.savefig('./figures/dendrogram.png', dpi=300,
# bbox_inches='tight')
title
=
"Dendogram"
plt
.
title
(
title
)
ocr_utils
.
show_figures
(
plt
,
title
)
print
(
'
\n
########################### No Errors ####################################'
)
Back
|
FazBrowse Home
|
New Git URL