-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript v2.py
More file actions
46 lines (40 loc) · 1.43 KB
/
script v2.py
File metadata and controls
46 lines (40 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import json
import numpy as np
from scipy.stats import linregress
import pandas as pd
from sklearn.linear_model import LinearRegression
with open('data.json', 'r') as f:
data = json.load(f)
# data normalization
normdata = {}
for refcode, entry in data.items():
normdata[refcode] = {}
for key, val in entry.items():
normdata[refcode][key] = (np.array(val) - np.mean(val))/np.std(val)
normdata[refcode][key] = [i for i in normdata[refcode][key]]
# save the normalized data
with open('normdata.json', 'w') as f:
json.dump(normdata, f)
# single-variant linear regression fit
descriptor = 'dn2'
predictors = ('dm2', 'cn2_x', 'cn2_y', 'cn2_z', 'm2n2_angle')
for refcode, entry in normdata.items():
print('Now processing', refcode)
y = entry[descriptor]
print('single-variate linear regression results:')
for p in predictors:
x = normdata[refcode][p]
print(p, linregress(x, y))
# check for orthonogality
df = pd.DataFrame(normdata[refcode], columns=predictors)
corr = df.corr()
print('correlation matrix:')
print(corr)
# multi-variant linear regression
# note that i didn't make any selection of the predictors
X = [entry[p] for p in predictors]
X = np.transpose(X)
reg = LinearRegression()
fit = reg.fit(X, y)
print('correlation coefficients: ', fit.coef_) # print the correlation coefficients
print('R^2: ', reg.score(X, y)) # print R^2