-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpredictions.py
More file actions
46 lines (39 loc) · 1.83 KB
/
Copy pathpredictions.py
File metadata and controls
46 lines (39 loc) · 1.83 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 sys, os
import numpy as np
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
import regressionAnalysis as reg
#TODO find way to grab last item in yFit to get predictions offset
def predict(predictMatrix, model, modelBuilder, dataset, knownYCumulative):
print(f"\nPredicting {modelBuilder.__name__} for {len(predictMatrix[0])} iterations")
dataset = np.array(dataset)
#print(f"\n\n{dataset}\n\n")
#print(f"\n\n{predictMatrix}\n\n")
# Put last item in covariates at front of predictMatrix
predictMatrixMod = []
for cov in range(len(predictMatrix)):
predictMatrixMod.append([dataset[cov + 1][-1]])
for x in predictMatrix[cov]:
predictMatrixMod[cov].append(x)
print(f"Prediction Matrix: {predictMatrixMod}")
predictMatrixMod = np.array(predictMatrixMod)
length = len(predictMatrixMod[0])
# handling for when an interaction is zero
culled = []
if modelBuilder == reg.interactionCoefficients:
knownCoeff_eqs = [modelBuilder(dataset[1:, i]) for i in range(len(dataset[1:]))]
culled = reg.cullZeros(knownCoeff_eqs)
coeff_eqs = [modelBuilder(predictMatrixMod[:, i]) for i in range(length)]
if culled:
for interaction in culled[::-1]:
for i, eq in enumerate(coeff_eqs):
coeff_eqs[i] = np.delete(eq, interaction)
# Construct Y Cumulative from betas
# Do not use model[:-1] here. reg.main returns it like that already
YFitList = [np.dot(model, coeff_eqs[i]) for i in range(len(coeff_eqs))]
YPredictions = reg.yFitCumulative(YFitList, length)
# Fix offset by comparing to last known yCumulative in original dataset
# I have no idea why, but yPredictions is off by a constant offset. This fixes it
offset = YPredictions[0] - knownYCumulative[-1]
YPredictionsCorrected = [x - offset for x in YPredictions]
return YPredictionsCorrected[1:]
# ex usage: predict(userMatrix, MLRRules, mlrCoefficients, dataset)