Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
A
alpha-mind
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dr.李
alpha-mind
Commits
e4487d31
Commit
e4487d31
authored
Jan 08, 2018
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added xgb classifier
parent
38de1f0f
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
62 additions
and
3 deletions
+62
-3
api.py
alphamind/api.py
+2
-0
__init__.py
alphamind/model/__init__.py
+2
-0
loader.py
alphamind/model/loader.py
+3
-0
treemodel.py
alphamind/model/treemodel.py
+37
-1
test_treemodel.py
alphamind/tests/model/test_treemodel.py
+18
-2
No files found.
alphamind/api.py
View file @
e4487d31
...
...
@@ -31,6 +31,7 @@ from alphamind.model import ConstLinearModel
from
alphamind.model
import
LogisticRegression
from
alphamind.model
import
RandomForestRegressor
from
alphamind.model
import
XGBRegressor
from
alphamind.model
import
XGBClassifier
from
alphamind.model
import
load_model
from
alphamind.model.data_preparing
import
fetch_data_package
from
alphamind.model.data_preparing
import
fetch_train_phase
...
...
@@ -70,6 +71,7 @@ __all__ = [
'LogisticRegression'
,
'RandomForestRegressor'
,
'XGBRegressor'
,
'XGBClassifier'
,
'load_model'
,
'NaiveExecutor'
,
'ThresholdExecutor'
,
...
...
alphamind/model/__init__.py
View file @
e4487d31
...
...
@@ -12,6 +12,7 @@ from alphamind.model.linearmodel import LogisticRegression
from
alphamind.model.treemodel
import
RandomForestRegressor
from
alphamind.model.treemodel
import
XGBRegressor
from
alphamind.model.treemodel
import
XGBClassifier
from
alphamind.model.loader
import
load_model
...
...
@@ -22,4 +23,5 @@ __all__ = ['LinearRegression',
'LogisticRegression'
,
'RandomForestRegressor'
,
'XGBRegressor'
,
'XGBClassifier'
,
'load_model'
]
\ No newline at end of file
alphamind/model/loader.py
View file @
e4487d31
...
...
@@ -12,6 +12,7 @@ from alphamind.model.linearmodel import LassoRegression
from
alphamind.model.linearmodel
import
LogisticRegression
from
alphamind.model.treemodel
import
RandomForestRegressor
from
alphamind.model.treemodel
import
XGBRegressor
from
alphamind.model.treemodel
import
XGBClassifier
def
load_model
(
model_desc
:
dict
)
->
ModelBase
:
...
...
@@ -31,5 +32,7 @@ def load_model(model_desc: dict) -> ModelBase:
return
RandomForestRegressor
.
load
(
model_desc
)
elif
'XGBRegressor'
in
model_name_parts
:
return
XGBRegressor
.
load
(
model_desc
)
elif
'XGBClassifier'
in
model_name_parts
:
return
XGBClassifier
.
load
(
model_desc
)
else
:
raise
ValueError
(
'{0} is not currently supported in model loader.'
.
format
(
model_name
))
alphamind/model/treemodel.py
View file @
e4487d31
...
...
@@ -11,6 +11,7 @@ from sklearn import __version__ as sklearn_version
from
sklearn.ensemble
import
RandomForestRegressor
as
RandomForestRegressorImpl
from
xgboost
import
__version__
as
xgbboot_version
from
xgboost
import
XGBRegressor
as
XGBRegressorImpl
from
xgboost
import
XGBClassifier
as
XGBClassifierImpl
from
alphamind.model.modelbase
import
ModelBase
from
alphamind.utilities
import
alpha_logger
...
...
@@ -49,7 +50,8 @@ class XGBRegressor(ModelBase):
n_estimators
:
int
=
100
,
learning_rate
:
float
=
0.1
,
max_depth
:
int
=
3
,
features
:
List
=
None
,
**
kwargs
):
features
:
List
=
None
,
**
kwargs
):
super
()
.
__init__
(
features
)
self
.
impl
=
XGBRegressorImpl
(
n_estimators
=
n_estimators
,
learning_rate
=
learning_rate
,
...
...
@@ -77,5 +79,39 @@ class XGBRegressor(ModelBase):
return
self
.
impl
.
feature_importances_
.
tolist
()
class
XGBClassifier
(
ModelBase
):
def
__init__
(
self
,
n_estimators
:
int
=
100
,
learning_rate
:
float
=
0.1
,
max_depth
:
int
=
3
,
features
:
List
=
None
,
**
kwargs
):
super
()
.
__init__
(
features
)
self
.
impl
=
XGBClassifierImpl
(
n_estimators
=
n_estimators
,
learning_rate
=
learning_rate
,
max_depth
=
max_depth
,
**
kwargs
)
def
save
(
self
)
->
dict
:
model_desc
=
super
()
.
save
()
model_desc
[
'xgbboot_version'
]
=
xgbboot_version
model_desc
[
'importances'
]
=
self
.
importances
return
model_desc
@
classmethod
def
load
(
cls
,
model_desc
:
dict
):
obj_layout
=
super
()
.
load
(
model_desc
)
if
LooseVersion
(
sklearn_version
)
<
LooseVersion
(
model_desc
[
'xgbboot_version'
]):
alpha_logger
.
warning
(
'Current xgboost version {0} is lower than the model version {1}. '
'Loaded model may work incorrectly.'
.
format
(
xgbboot_version
,
model_desc
[
'xgbboot_version'
]))
return
obj_layout
@
property
def
importances
(
self
):
return
self
.
impl
.
feature_importances_
.
tolist
()
alphamind/tests/model/test_treemodel.py
View file @
e4487d31
...
...
@@ -10,6 +10,7 @@ import numpy as np
from
alphamind.model.loader
import
load_model
from
alphamind.model.treemodel
import
RandomForestRegressor
from
alphamind.model.treemodel
import
XGBRegressor
from
alphamind.model.treemodel
import
XGBClassifier
class
TestTreeModel
(
unittest
.
TestCase
):
...
...
@@ -28,7 +29,7 @@ class TestTreeModel(unittest.TestCase):
sample_x
=
np
.
random
.
randn
(
100
,
10
)
np
.
testing
.
assert_array_almost_equal
(
model
.
predict
(
sample_x
),
new_model
.
predict
(
sample_x
))
def
tes
_xgb_regress
(
self
):
def
tes
t_xgb_regress_persistence
(
self
):
model
=
XGBRegressor
(
features
=
list
(
range
(
10
)))
x
=
np
.
random
.
randn
(
1000
,
10
)
y
=
np
.
random
.
randn
(
1000
)
...
...
@@ -41,3 +42,18 @@ class TestTreeModel(unittest.TestCase):
sample_x
=
np
.
random
.
randn
(
100
,
10
)
np
.
testing
.
assert_array_almost_equal
(
model
.
predict
(
sample_x
),
new_model
.
predict
(
sample_x
))
def
test_xgb_classify_persistence
(
self
):
model
=
XGBClassifier
(
features
=
list
(
range
(
10
)))
x
=
np
.
random
.
randn
(
1000
,
10
)
y
=
np
.
random
.
randn
(
1000
)
y
=
np
.
where
(
y
>
0
,
1
,
0
)
model
.
fit
(
x
,
y
)
desc
=
model
.
save
()
new_model
=
load_model
(
desc
)
self
.
assertEqual
(
model
.
features
,
new_model
.
features
)
sample_x
=
np
.
random
.
randn
(
100
,
10
)
np
.
testing
.
assert_array_almost_equal
(
model
.
predict
(
sample_x
),
new_model
.
predict
(
sample_x
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment