Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
J
Jupyter Angewandte Mathe
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
Christian Höfert
Jupyter Angewandte Mathe
Commits
6fad5d0a
Commit
6fad5d0a
authored
3 years ago
by
Christian Höfert
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
736e1e49
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
crh_LinearRegression.py
+74
-0
74 additions, 0 deletions
crh_LinearRegression.py
with
74 additions
and
0 deletions
crh_LinearRegression.py
0 → 100644
+
74
−
0
View file @
6fad5d0a
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 17 20:17:16 2021
Provides functions for calculating the coeffs (m,c) of the
linear regression y=mx+c for a given set of point-data [x,y]
flag: plot
@author: hoefert
"""
## https://www.mathematik.de/algebra/74-erste-hilfe/algebra/matrizen/2432-anwendungen-der-matrizenrechnung
import
matplotlib.pyplot
as
plt
import
numpy
as
np
def
linearRegression
(
x
,
y
,
plot
=
False
):
# creating the LES A'A ( m c)' = A'y
# with A = (x , 1)
# creating LES (over determined)
n
=
len
(
x
)
A
=
np
.
ones
((
n
,
2
))
A
[:,
0
]
=
np
.
transpose
(
x
)
b
=
np
.
transpose
(
y
)
M
=
np
.
matmul
(
A
.
T
,
A
)
bTrans
=
np
.
matmul
(
A
.
T
,
b
)
[
m
,
c
]
=
np
.
linalg
.
solve
(
M
,
bTrans
)
if
plot
:
plotLinearRegression
(
x
,
y
,
m
,
c
)
return
m
,
c
def
logLinearRegression
(
x
,
y
,
plot
=
False
):
xLog
=
np
.
log10
(
x
)
yLog
=
np
.
log10
(
y
)
m
,
c
=
linearRegression
(
xLog
,
yLog
)
if
plot
:
plotLog10LinearRegression
(
x
,
y
,
m
,
c
)
return
m
,
c
def
plotLinearRegression
(
x
,
y
,
m
,
c
):
xLin
=
np
.
linspace
(
min
(
x
),
max
(
x
),
100
)
yLin
=
xLin
*
m
+
c
plt
.
plot
(
x
,
y
,
'
o
'
)
plt
.
plot
(
xLin
,
yLin
,
linewidth
=
2
)
plt
.
grid
(
'
both
'
)
plt
.
show
()
def
plotLog10LinearRegression
(
x
,
y
,
m
,
c
,
plotLog
=
False
):
plt
.
plot
(
x
,
y
,
'
o
'
)
xLin
=
np
.
linspace
(
min
(
x
),
max
(
x
),
100
)
yLin
=
10
**
c
*
xLin
**
m
plt
.
plot
(
xLin
,
yLin
,
linewidth
=
2
)
if
plotLog
:
plt
.
xscale
(
'
log
'
)
plt
.
yscale
(
'
log
'
)
plt
.
grid
(
'
both
'
)
plt
.
show
()
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