Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Imanol Perez
identifying-which-country-is-a-stock-from
Commits
e2ec5c64
Commit
e2ec5c64
authored
Jun 15, 2017
by
Imanol Perez
Browse files
Upload new file
parent
bbe079e5
Changes
1
Hide whitespace changes
Inline
Side-by-side
sigLearn.py
0 → 100644
View file @
e2ec5c64
import
numpy
as
np
import
iisignature
from
sklearn.ensemble
import
RandomForestRegressor
import
numbers
class
sigLearn
:
def
__init__
(
self
,
order
=
2
,
alpha
=
0.1
):
if
not
isinstance
(
order
,
numbers
.
Integral
)
or
order
<
1
:
raise
NameError
(
'The order must be a positive integer.'
)
if
not
isinstance
(
alpha
,
numbers
.
Real
)
or
alpha
<=
0.0
:
raise
NameError
(
'Alpha must be a positive real.'
)
self
.
order
=
int
(
order
)
self
.
reg
=
None
self
.
alpha
=
alpha
def
train
(
self
,
x
,
y
):
'''
Trains the model using signatures.
x: list of inputs.
y: list of outputs.
'''
# We check that types of x and y are the expected ones
if
x
is
None
or
y
is
None
:
return
if
not
(
type
(
x
)
is
list
or
type
(
x
)
is
tuple
)
or
not
(
type
(
y
)
is
list
or
type
(
y
)
is
tuple
):
raise
NameError
(
'Input and output must be lists or tuples.'
)
if
len
(
x
)
!=
len
(
y
):
raise
NameError
(
'The number of inputs and the number of outputs must coincide.'
)
X
=
[
list
(
iisignature
.
sig
(
np
.
array
(
stream
),
self
.
order
))
for
stream
in
x
]
self
.
reg
=
RandomForestRegressor
(
n_estimators
=
100
,
oob_score
=
True
)
self
.
reg
.
fit
(
X
,
y
)
def
predict
(
self
,
x
):
'''
Predicts the outputs of the inputs x using the the pre-trained model.
x: list of inputs.
Returns:
list of predicted outputs.
'''
if
self
.
reg
is
None
:
raise
NameError
(
'The model is not trained.'
)
X
=
[
list
(
iisignature
.
sig
(
np
.
array
(
stream
),
self
.
order
))
for
stream
in
x
]
return
self
.
reg
.
predict
(
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