Skip to content
Snippets Groups Projects
Commit 81b41582 authored by Christian Höfert's avatar Christian Höfert
Browse files

Update Lineareregression.ipynb

parent 89362e12
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import numpy as np import numpy as np
import crh_LinearRegression as LinReg import crh_LinearRegression as LinReg
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
""" """
Creating random points Creating random points
""" """
n = 1000 n = 1000
# reference line # reference line
m = np.random.rand() m = np.random.rand()
if np.random.rand()<.5: if np.random.rand()<.5:
m = -m m = -m
c = np.random.rand() c = np.random.rand()
# generating point set # generating point set
points = [[],[]] points = [[],[]]
for i in range(n): for i in range(n):
x = np.random.rand() x = np.random.rand()
y = m*x+c + .5*(np.random.rand()-.5)*np.random.rand() y = m*x+c + .5*(np.random.rand()-.5)*np.random.rand()
points[0].append(x) points[0].append(x)
points[1].append(y) points[1].append(y)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
""" """
Calling regression function Calling regression function
""" """
m,c = LinReg.linearRegression(points[0],points[1],True) m,c = LinReg.linearRegression(points[0],points[1],True)
print("m=",m,"c=",c) print("m=",m,"c=",c)
``` ```
%% Output %% Output
m= -0.12246486054984394 c= 0.4159820876090123 m= -0.12246486054984394 c= 0.4159820876090123
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import scipy.linalg as lina import scipy.linalg as lina
import random import random
import time import time
import math import math
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
N = 100 N = 100
nMin = 500 nMin = 500
nMax = 4000 nMax = 1500
T = np.zeros((2,N)) T = np.zeros((2,N))
for i in range(N): for i in range(N):
n = random.randint(nMin, nMax) n = random.randint(nMin, nMax)
A= np.random.rand(n,n) A= np.random.rand(n,n)
""" """
Determing det(A) Determing det(A)
""" """
start = time.time() start = time.time()
D =lina.det(A) D =lina.det(A)
end = time.time() end = time.time()
duration = end-start duration = end-start
#print("i:",i, n,duration) #print("i:",i, n,duration)
print("\r","Progress:", i+1," \ ",N,end="") print("\r","Progress:", i+1," \ ",N,end="")
T[0,i] = n T[0,i] = n
T[1,i] = duration T[1,i] = duration
print("Done!") print("Done!")
``` ```
%% Output %% Output
Progress: 100 \ 100 \ 100 Progress: 100 \ 100 \ 100
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
#m,c = LinReg.linearRegression([math.log(n) for n in T[0]],[math.log(n) for n in T[1]],True) #m,c = LinReg.linearRegression([math.log(n) for n in T[0]],[math.log(n) for n in T[1]],True)
m,c = LinReg.linearRegression(np.log10(T[0]),np.log10(T[1]),True) m,c = LinReg.linearRegression(np.log10(T[0]),np.log10(T[1]),True)
print(m,c) print(m,c)
LinReg.logLinearRegression(T[0],T[1],True) LinReg.logLinearRegression(T[0],T[1],True)
``` ```
%% Output %% Output
2.656156789776084 -9.603855797451011 2.656156789776084 -9.603855797451011
(2.656156789776084, -9.603855797451011) (2.656156789776084, -9.603855797451011)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment