Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Allzweckmesser
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Messerschleifer
Allzweckmesser
Commits
6dd4f34e
Commit
6dd4f34e
authored
6 years ago
by
Victor Zimmermann
Browse files
Options
Downloads
Patches
Plain Diff
Add machine learning scripts.
parent
f95764a0
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
allzweckmesser/decision_tree.py
+56
-0
56 additions, 0 deletions
allzweckmesser/decision_tree.py
allzweckmesser/random_forest.py
+56
-0
56 additions, 0 deletions
allzweckmesser/random_forest.py
allzweckmesser/svm.py
+53
-0
53 additions, 0 deletions
allzweckmesser/svm.py
with
165 additions
and
0 deletions
allzweckmesser/decision_tree.py
0 → 100644
+
56
−
0
View file @
6dd4f34e
from
sklearn
import
tree
from
sklearn.externals
import
joblib
import
graphviz
# doctest: +SKIP
import
json
import
numpy
as
np
#load data
train_file
=
open
(
'
../train0-9.json
'
,
'
r
'
)
dev_file
=
open
(
'
../dev.json
'
)
train
=
json
.
load
(
train_file
)
dev
=
json
.
load
(
dev_file
)
X
,
Y
=
[],[]
for
verse
in
train
:
for
reading
in
verse
[
2
]:
X
.
append
(
reading
[
0
])
Y
.
append
(
reading
[
1
])
#build model
clf
=
tree
.
DecisionTreeClassifier
(
max_depth
=
3
,
criterion
=
'
entropy
'
,
splitter
=
'
best
'
)
#fit
clf
.
fit
(
X
,
Y
)
correct
=
0
total
=
0
for
verse
in
dev
:
vectors
=
[
reading
[
0
]
for
reading
in
verse
[
2
]]
probs
=
clf
.
predict_proba
(
vectors
)
sort_probs
=
sorted
([(
probs
[
i
],
verse
[
2
][
i
][
1
])
for
i
in
range
(
len
(
probs
))],
key
=
lambda
x
:
x
[
0
][
0
])
gold
=
sort_probs
[
0
][
1
]
if
gold
==
1
:
correct
+=
1
total
+=
1
print
(
"
Recall: {}/{} ({})
"
.
format
(
correct
,
total
,
correct
/
total
))
#precision = tp/(tp+fp)
#recall = tp/(tp+fn)
#accuracy = (tp+tn)/(tp+tn+fp+fn)
#f1 = 2*((precision*recall)/(precision+recall))
#print('Precision: {}\tRecall:{}'.format(precision,recall))
#print('Accuracy: {}\tF1-Measure:{}\n'.format(accuracy, f1))
joblib
.
dump
(
clf
,
'
tree_classifier.joblib
'
)
dot_data
=
tree
.
export_graphviz
(
clf
,
out_file
=
None
)
# doctest: +SKIP
graph
=
graphviz
.
Source
(
dot_data
)
# doctest: +SKIP
graph
.
render
(
"
latin_tree
"
)
# doctest: +SKIP
This diff is collapsed.
Click to expand it.
allzweckmesser/random_forest.py
0 → 100644
+
56
−
0
View file @
6dd4f34e
from
sklearn.ensemble
import
RandomForestClassifier
from
sklearn.externals
import
joblib
import
graphviz
# doctest: +SKIP
import
json
import
numpy
as
np
#load data
train_file
=
open
(
'
../train0-9.json
'
,
'
r
'
)
dev_file
=
open
(
'
../dev.json
'
)
train
=
json
.
load
(
train_file
)
dev
=
json
.
load
(
dev_file
)
X
,
Y
=
[],[]
for
verse
in
train
:
for
reading
in
verse
[
2
]:
X
.
append
(
reading
[
0
])
Y
.
append
(
reading
[
1
])
#build model
clf
=
RandomForestClassifier
()
#fit
clf
.
fit
(
X
,
Y
)
correct
=
0
total
=
0
for
verse
in
dev
:
vectors
=
[
reading
[
0
]
for
reading
in
verse
[
2
]]
probs
=
clf
.
predict_proba
(
vectors
)
sort_probs
=
sorted
([(
probs
[
i
],
verse
[
2
][
i
][
1
])
for
i
in
range
(
len
(
probs
))],
key
=
lambda
x
:
x
[
0
][
0
])
gold
=
sort_probs
[
0
][
1
]
if
gold
==
1
:
correct
+=
1
total
+=
1
print
(
"
Recall: {}/{} ({})
"
.
format
(
correct
,
total
,
correct
/
total
))
#precision = tp/(tp+fp)
#recall = tp/(tp+fn)
#accuracy = (tp+tn)/(tp+tn+fp+fn)
#f1 = 2*((precision*recall)/(precision+recall))
#print('Precision: {}\tRecall:{}'.format(precision,recall))
#print('Accuracy: {}\tF1-Measure:{}\n'.format(accuracy, f1))
joblib
.
dump
(
clf
,
'
forest_classifier.joblib
'
)
#dot_data = tree.export_graphviz(clf, out_file=None) # doctest: +SKIP
#graph = graphviz.Source(dot_data) # doctest: +SKIP
#graph.render("latin_tree") # doctest: +SKIP
This diff is collapsed.
Click to expand it.
allzweckmesser/svm.py
0 → 100644
+
53
−
0
View file @
6dd4f34e
from
sklearn
import
svm
from
sklearn.externals
import
joblib
from
sklearn.ensemble
import
BaggingClassifier
from
sklearn.calibration
import
CalibratedClassifierCV
import
graphviz
# doctest: +SKIP
import
json
import
numpy
as
np
#load data
train_file
=
open
(
'
../train0-9.json
'
,
'
r
'
)
dev_file
=
open
(
'
../dev.json
'
)
train
=
json
.
load
(
train_file
)
dev
=
json
.
load
(
dev_file
)
X
,
Y
=
[],[]
for
verse
in
train
:
for
reading
in
verse
[
2
]:
X
.
append
(
reading
[
0
])
Y
.
append
(
reading
[
1
])
#build model
n_estimators
=
10
svm
=
BaggingClassifier
(
svm
.
LinearSVC
(),
max_samples
=
1.0
/
n_estimators
,
n_estimators
=
n_estimators
)
clf
=
CalibratedClassifierCV
(
svm
)
#fit
clf
.
fit
(
X
,
Y
)
correct
=
0
total
=
0
for
verse
in
dev
:
vectors
=
[
reading
[
0
]
for
reading
in
verse
[
2
]]
probs
=
clf
.
predict_proba
(
vectors
)
sort_probs
=
sorted
([(
probs
[
i
],
verse
[
2
][
i
][
1
])
for
i
in
range
(
len
(
probs
))],
key
=
lambda
x
:
x
[
0
][
0
])
gold
=
sort_probs
[
0
][
1
]
if
gold
==
1
:
correct
+=
1
total
+=
1
print
(
"
Recall: {}/{} ({})
"
.
format
(
correct
,
total
,
correct
/
total
))
joblib
.
dump
(
clf
,
'
svm_classifier.joblib
'
)
#dot_data = tree.export_graphviz(clf, out_file=None) # doctest: +SKIP
#graph = graphviz.Source(dot_data) # doctest: +SKIP
#graph.render("male_female") # doctest: +SKIP
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment