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
7af779f2
Commit
7af779f2
authored
4 years ago
by
lukas leufen
Browse files
Options
Downloads
Patches
Plain Diff
docstrings for linear_model.py
parent
ae104ccb
No related branches found
No related tags found
3 merge requests
!125
Release v0.10.0
,
!124
Update Master to new version v0.10.0
,
!91
WIP: Resolve "create sphinx docu"
Pipeline
#35400
passed
4 years ago
Stage: test
Stage: docs
Stage: pages
Stage: deploy
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
CI/update_badge.sh
+4
-4
4 additions, 4 deletions
CI/update_badge.sh
src/model_modules/linear_model.py
+30
-5
30 additions, 5 deletions
src/model_modules/linear_model.py
with
34 additions
and
9 deletions
CI/update_badge.sh
+
4
−
4
View file @
7af779f2
...
@@ -71,10 +71,10 @@ printf "%s\n" "${SHIELDS_IO_NAME//\#/%23}"
...
@@ -71,10 +71,10 @@ printf "%s\n" "${SHIELDS_IO_NAME//\#/%23}"
SHIELDS_IO_NAME
=
"
$(
echo
-e
"
${
SHIELDS_IO_NAME
//\_/__
}
"
)
"
SHIELDS_IO_NAME
=
"
$(
echo
-e
"
${
SHIELDS_IO_NAME
//\_/__
}
"
)
"
SHIELDS_IO_NAME
=
"
$(
echo
-e
"
${
SHIELDS_IO_NAME
//\#/%23
}
"
)
"
SHIELDS_IO_NAME
=
"
$(
echo
-e
"
${
SHIELDS_IO_NAME
//\#/%23
}
"
)
"
curl
"https://img.shields.io/badge/
${
SHIELDS_IO_NAME
}
"
>
${
BADGE_FILENAME
}
curl
"https://img.shields.io/badge/
${
SHIELDS_IO_NAME
}
"
>
"
${
BADGE_FILENAME
}
"
echo
"https://img.shields.io/badge/
${
SHIELDS_IO_NAME
}
"
echo
"https://img.shields.io/badge/
${
SHIELDS_IO_NAME
}
"
SHIELDS_IO_NAME_RECENT
=
"RECENT:
${
SHIELDS_IO_NAME
}
"
SHIELDS_IO_NAME_RECENT
=
"RECENT:
${
SHIELDS_IO_NAME
}
"
curl
"https://img.shields.io/badge/
${
SHIELDS_IO_NAME_RECENT
}
"
>
${
RECENT_BADGE_FILENAME
}
curl
"https://img.shields.io/badge/
${
SHIELDS_IO_NAME_RECENT
}
"
>
"
${
RECENT_BADGE_FILENAME
}
"
echo
"
${
SHIELDS_IO_NAME_RECENT
}
"
>
testRecentName.txt
echo
"
${
SHIELDS_IO_NAME_RECENT
}
"
>
testRecentName.txt
#
#
...
@@ -82,10 +82,10 @@ if [[ ! -d ./badges ]]; then
...
@@ -82,10 +82,10 @@ if [[ ! -d ./badges ]]; then
# Control will enter here if $DIRECTORY doesn't exist.
# Control will enter here if $DIRECTORY doesn't exist.
mkdir
badges/
mkdir
badges/
fi
fi
mv
${
BADGE_FILENAME
}
./badges/.
mv
"
${
BADGE_FILENAME
}
"
./badges/.
# replace outdated recent badge by new badge
# replace outdated recent badge by new badge
mv
${
RECENT_BADGE_FILENAME
}
./badges/
${
RECENT_BADGE_FILENAME
}
mv
"
${
RECENT_BADGE_FILENAME
}
"
"
./badges/
${
RECENT_BADGE_FILENAME
}
"
# set status to failed, this will be overwritten if job ended with exitcode 0
# set status to failed, this will be overwritten if job ended with exitcode 0
echo
"failed"
>
status.txt
echo
"failed"
>
status.txt
...
...
This diff is collapsed.
Click to expand it.
src/model_modules/linear_model.py
+
30
−
5
View file @
7af779f2
"""
Calculate ordinary least squared model.
"""
__author__
=
"
Felix Kleinert, Lukas Leufen
"
__author__
=
"
Felix Kleinert, Lukas Leufen
"
__date__
=
'
2019-12-11
'
__date__
=
'
2019-12-11
'
import
numpy
as
np
import
numpy
as
np
import
statsmodels.api
as
sm
import
statsmodels.api
as
sm
class
OrdinaryLeastSquaredModel
:
class
OrdinaryLeastSquaredModel
:
"""
Implementation of an ordinary least squared model (OLS).
Inputs and outputs are retrieved from a generator. This generator needs to return in xarray format and has to be
iterable. OLS is calculated on initialisation using statsmodels package. Train your personal OLS using:
.. code-block:: python
# next(train_data) should be return (x, y)
my_ols_model = OrdinaryLeastSquaredModel(train_data)
After calculation, use your OLS model with
.. code-block:: python
# input_data needs to be structured like train data
result_ols = my_ols_model.predict(input_data)
:param generator: generator object returning a tuple containing inputs and outputs as xarrays
"""
def
__init__
(
self
,
generator
):
def
__init__
(
self
,
generator
):
"""
Set up OLS model.
"""
self
.
x
=
[]
self
.
x
=
[]
self
.
y
=
[]
self
.
y
=
[]
self
.
generator
=
generator
self
.
generator
=
generator
self
.
model
=
self
.
train_ols_model_from_generator
()
self
.
model
=
self
.
_
train_ols_model_from_generator
()
def
train_ols_model_from_generator
(
self
):
def
_
train_ols_model_from_generator
(
self
):
self
.
set_x_y_from_generator
()
self
.
_
set_x_y_from_generator
()
self
.
x
=
sm
.
add_constant
(
self
.
x
)
self
.
x
=
sm
.
add_constant
(
self
.
x
)
return
self
.
ordinary_least_squared_model
(
self
.
x
,
self
.
y
)
return
self
.
ordinary_least_squared_model
(
self
.
x
,
self
.
y
)
def
set_x_y_from_generator
(
self
):
def
_
set_x_y_from_generator
(
self
):
data_x
=
None
data_x
=
None
data_y
=
None
data_y
=
None
for
item
in
self
.
generator
:
for
item
in
self
.
generator
:
...
@@ -31,16 +53,19 @@ class OrdinaryLeastSquaredModel:
...
@@ -31,16 +53,19 @@ class OrdinaryLeastSquaredModel:
self
.
y
=
data_y
self
.
y
=
data_y
def
predict
(
self
,
data
):
def
predict
(
self
,
data
):
"""
Apply OLS model on data.
"""
data
=
sm
.
add_constant
(
self
.
reshape_xarray_to_numpy
(
data
),
has_constant
=
"
add
"
)
data
=
sm
.
add_constant
(
self
.
reshape_xarray_to_numpy
(
data
),
has_constant
=
"
add
"
)
return
np
.
atleast_2d
(
self
.
model
.
predict
(
data
))
return
np
.
atleast_2d
(
self
.
model
.
predict
(
data
))
@staticmethod
@staticmethod
def
reshape_xarray_to_numpy
(
data
):
def
reshape_xarray_to_numpy
(
data
):
"""
Reshape xarray data to numpy data and flatten.
"""
shape
=
data
.
values
.
shape
shape
=
data
.
values
.
shape
res
=
data
.
values
.
reshape
(
shape
[
0
],
shape
[
1
]
*
shape
[
3
])
res
=
data
.
values
.
reshape
(
shape
[
0
],
shape
[
1
]
*
shape
[
3
])
return
res
return
res
@staticmethod
@staticmethod
def
ordinary_least_squared_model
(
x
,
y
):
def
ordinary_least_squared_model
(
x
,
y
):
"""
Calculate ols model using statsmodels.
"""
ols_model
=
sm
.
OLS
(
y
,
x
)
ols_model
=
sm
.
OLS
(
y
,
x
)
return
ols_model
.
fit
()
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