-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript v4.py
More file actions
65 lines (59 loc) · 2.18 KB
/
script v4.py
File metadata and controls
65 lines (59 loc) · 2.18 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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']
coefficients = {}
for refcode, entry in normdata.items():
print('Now processing', refcode)
y = entry['dn2']
# print('single-variate linear regression results:')
inputs = ['dm2', 'cn2_x', 'cn2_y', 'cn2_z', 'm2n2_angle']
removed = False
slopeList = {}
for p in predictors:
x = entry[p]
slope, intercept, r_value, p_value, std_err = linregress(x, y)
slopeList[p] = abs(slope * 1000)
for i in range(0, 1000):
for vari in inputs:
if slopeList[vari] < i and len(inputs) > 1:
inputs.remove(vari)
variable = {}
# multi-variant linear regression
# note that i didn't make any selection of the predictors
X = [entry[p] for p in inputs]
X = np.transpose(X)
reg = LinearRegression()
fit = reg.fit(X, y)
print(reg.score(X, y))
if reg.score(X, y) > 0.95: # print R^2
for j in range(len(inputs)):
variable[inputs[j]] = fit.coef_[j]
coefficients[refcode] = variable
else:
break
"""
# check for orthonogality
df = pd.DataFrame(normdata[refcode], columns=inputs)
corr = df.corr()
print(corr)
"""
print(coefficients)
with open('coefficients4.txt', 'w') as outfile:
json.dump(coefficients, outfile, indent=4)