Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
EDdA Classification
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
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
Projet GEODE
EDdA Classification
Commits
780f702c
Commit
780f702c
authored
3 years ago
by
Khalleud
Browse files
Options
Downloads
Patches
Plain Diff
[ADD] save and load trained models
parent
9e575a9d
No related branches found
Branches containing commit
No related tags found
1 merge request
!2
[ADD] save and load trained models
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
experimentsClassicClassifiers.py
+29
-4
29 additions, 4 deletions
experimentsClassicClassifiers.py
with
29 additions
and
4 deletions
experimentsClassicClassifiers.py
+
29
−
4
View file @
780f702c
...
...
@@ -12,6 +12,7 @@ from sklearn import preprocessing
from
evaluate_model
import
evaluate_model
from
sklearn.model_selection
import
GridSearchCV
import
configparser
import
pickle
import
nltk
nltk
.
download
(
'
stopwords
'
)
...
...
@@ -33,7 +34,7 @@ maxOfInstancePerClass = args.maxOfInstancePerClass
if
not
os
.
path
.
exists
(
'
reports
'
):
os
.
makedirs
(
'
reports
'
)
if
not
os
.
path
.
exists
(
os
.
path
.
join
(
'
reports
'
,
columnClass
)):
os
.
makedirs
(
os
.
path
.
join
(
'
reports
'
,
columnClass
))
...
...
@@ -42,6 +43,11 @@ dir_name_report = str(minOfInstancePerClass) + '_' + str(maxOfInstancePerClass)
if
not
os
.
path
.
exists
(
os
.
path
.
join
(
'
reports
'
,
columnClass
,
dir_name_report
)):
os
.
makedirs
(
os
.
path
.
join
(
'
reports
'
,
columnClass
,
dir_name_report
))
# create directory to save and load models
if
not
os
.
path
.
exists
(
'
models
'
):
os
.
makedirs
(
'
models
'
)
# Reading data and preprocessings steps
preprocessor
=
Preprocessor
()
...
...
@@ -89,22 +95,42 @@ for columnInput in [columnText, 'firstParagraph']:
clf_name
,
clf
=
tmp_clf
grid_param_name
,
grid_param
=
tmp_grid_params
print
(
clf_name
,
clf
,
grid_param_name
,
grid_param
)
model_file_name
=
columnInput
+
'
_
'
+
feature_technique_name
+
'
_
'
+
clf_name
+
str
(
minOfInstancePerClass
)
+
'
_
'
+
str
(
maxOfInstancePerClass
)
+
"
.pkl
"
if
clf_name
==
'
bayes
'
:
if
feature_technique_name
==
'
doc2vec
'
:
continue
else
:
t_begin
=
time
.
time
()
clf
.
fit
(
train_x
,
train_y
)
# if model exist
if
os
.
path
.
isfile
(
os
.
path
.
join
(
'
./model
'
,
model_file_name
)):
with
open
(
model_file_name
,
'
rb
'
)
as
file
:
clf
=
pickle
.
load
(
file
)
else
:
#if model not exists we save
with
open
(
Pkl_Filename
,
'
wb
'
)
as
file
:
clf
.
fit
(
train_x
,
train_y
)
pickle
.
dump
(
clf
,
file
)
t_end
=
time
.
time
()
training_time
=
t_end
-
t_begin
y_pred
=
clf
.
predict
(
test_x
)
else
:
clf
=
GridSearchCV
(
clf
,
grid_param
,
refit
=
True
,
verbose
=
3
)
t_begin
=
time
.
time
()
clf
.
fit
(
train_x
,
train_y
)
if
os
.
path
.
isfile
(
os
.
path
.
join
(
'
./model
'
,
model_file_name
)):
with
open
(
model_file_name
,
'
rb
'
)
as
file
:
clf
=
pickle
.
load
(
file
)
else
:
with
open
(
Pkl_Filename
,
'
wb
'
)
as
file
:
clf
.
fit
(
train_x
,
train_y
)
pickle
.
dump
(
clf
,
file
)
t_end
=
time
.
time
()
training_time
=
t_end
-
t_begin
y_pred
=
clf
.
predict
(
test_x
)
...
...
@@ -126,4 +152,3 @@ for columnInput in [columnText, 'firstParagraph']:
print
(
'
training time : {}
'
.
format
(
training_time
))
#sys.stdout = sys.stdout # Reset the standard output to its original value
sys
.
stdout
=
sys
.
__stdout__
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