Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
MLAir
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
esde
machine-learning
MLAir
Commits
4608b470
Commit
4608b470
authored
5 years ago
by
lukas leufen
Browse files
Options
Downloads
Patches
Plain Diff
refactored ordinary least squared model into class
parent
057b47fe
No related branches found
No related tags found
2 merge requests
!24
include recent development
,
!23
Lukas issue018 feat evaluate train val
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/model_modules/linear_model.py
+39
-3
39 additions, 3 deletions
src/model_modules/linear_model.py
with
39 additions
and
3 deletions
src/model_modules/linear_model.py
+
39
−
3
View file @
4608b470
...
...
@@ -3,8 +3,44 @@ __date__ = '2019-12-11'
import
statsmodels.api
as
sm
import
numpy
as
np
def
ordinary_least_squared_model
(
x
,
y
):
ols_model
=
sm
.
OLS
(
y
,
x
)
return
ols_model
.
fit
()
class
OrdinaryLeastSquaredModel
:
def
__init__
(
self
,
generator
):
self
.
x
=
[]
self
.
y
=
[]
self
.
generator
=
generator
self
.
model
=
self
.
train_ols_model_from_generator
()
def
train_ols_model_from_generator
(
self
):
self
.
set_x_y_from_generator
()
self
.
x
=
sm
.
add_constant
(
self
.
x
)
return
self
.
ordinary_least_squared_model
(
self
.
x
,
self
.
y
)
def
set_x_y_from_generator
(
self
):
data_x
=
None
data_y
=
None
for
item
in
self
.
generator
:
x
=
self
.
reshape_xarray_to_numpy
(
item
[
0
])
y
=
item
[
1
].
values
data_x
=
np
.
concatenate
((
data_x
,
x
),
axis
=
0
)
if
data_x
is
not
None
else
x
data_y
=
np
.
concatenate
((
data_y
,
y
),
axis
=
0
)
if
data_y
is
not
None
else
y
self
.
x
=
data_x
self
.
y
=
data_y
def
predict
(
self
,
data
):
data
=
sm
.
add_constant
(
self
.
reshape_xarray_to_numpy
(
data
))
return
self
.
model
.
predict
(
data
)
@staticmethod
def
reshape_xarray_to_numpy
(
data
):
shape
=
data
.
values
.
shape
res
=
data
.
values
.
reshape
(
shape
[
0
],
shape
[
1
]
*
shape
[
3
])
return
res
@staticmethod
def
ordinary_least_squared_model
(
x
,
y
):
ols_model
=
sm
.
OLS
(
y
,
x
)
return
ols_model
.
fit
()
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