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
104437fd
Commit
104437fd
authored
May 10, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update linear model
parent
6f03cfe2
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
1 deletion
+36
-1
linearmodel.py
alphamind/model/linearmodel.py
+36
-1
No files found.
alphamind/model/linearmodel.py
View file @
104437fd
...
...
@@ -11,6 +11,35 @@ from alphamind.cyimpl import groupby
from
alphamind.data.neutralize
import
ls_fit
class
LinearModel
(
object
):
def
__init__
(
self
,
init_param
=
None
):
self
.
model_parameter
=
init_param
def
calibrate
(
self
,
x
,
y
,
groups
=
None
):
self
.
model_parameter
=
_train
(
x
,
y
,
groups
)
def
predict
(
self
,
x
,
groups
=
None
):
if
groups
is
not
None
and
isinstance
(
self
.
model_parameter
,
dict
):
names
=
np
.
unique
(
groups
)
return
multiple_prediction
(
names
,
self
.
model_parameter
,
x
,
groups
)
elif
self
.
model_parameter
is
None
:
raise
ValueError
(
"linear model is not calibrated yet"
)
elif
groups
is
None
:
return
x
@
self
.
model_parameter
else
:
raise
ValueError
(
"grouped x value can't be used for vanilla linear model"
)
def
multiple_prediction
(
names
,
model_parames
,
x
,
groups
):
pred_v
=
np
.
zeros
(
x
.
shape
[
0
])
for
name
in
names
:
this_param
=
model_parames
[
name
]
idx
=
groups
==
name
pred_v
[
idx
]
=
x
[
idx
]
@
this_param
return
pred_v
def
_train
(
x
:
np
.
ndarray
,
y
:
np
.
ndarray
,
groups
:
np
.
ndarray
=
None
)
->
Union
[
np
.
ndarray
,
dict
]:
if
groups
is
None
:
return
ls_fit
(
x
,
y
)
...
...
@@ -31,4 +60,10 @@ if __name__ == '__main__':
y
=
np
.
random
.
randn
(
3000
)
groups
=
np
.
random
.
randint
(
30
,
size
=
3000
)
print
(
_train
(
x
,
y
,
groups
))
\ No newline at end of file
to_x
=
np
.
random
.
randn
(
100
,
10
)
to_groups
=
np
.
random
.
randint
(
30
,
size
=
100
)
model
=
LinearModel
()
model
.
calibrate
(
x
,
y
,
groups
)
model
.
predict
(
to_x
,
to_groups
)
\ No newline at end of file
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