-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
101 lines (90 loc) · 3.61 KB
/
app.py
File metadata and controls
101 lines (90 loc) · 3.61 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import argparse
import pandas as pd
import import_ipynb
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression, LogisticRegression, SGDRegressor
from sklearn import preprocessing
from csvload import FULL_COLUMNS, df, train, test, all_column_train, all_column_y, all_column_test, all_columns_test_result, no_na_train, no_na_train_y, no_na_test, no_na_test_y
def MSE(predictions, results):
sum = 0.0
for i in range(len(predictions)):
sum += (predictions[i] - results['Outcome SE/refraction\n(after surgery vision)'][i])**2
return sum/len(predictions)
def MAE(predictions, results):
sum = 0.0
for i in range(len(predictions)):
sum += abs(predictions[i] - results['Outcome SE/refraction\n(after surgery vision)'][i])
return sum/len(predictions)
def fit_to_model(args, model):
if args.model_type == "Full":
data = all_column_train[FULL_COLUMNS]
y_data = all_column_y
test_data = all_column_test[FULL_COLUMNS]
test_results = all_columns_test_result
else:
data = no_na_train
y_data = no_na_train_y
test_data = no_na_test
test_results = no_na_test_y
model.fit(data, y_data)
predictions = np.array(model.predict(test_data))
predictions = np.array([prediction[0] for prediction in predictions])
test_results.reset_index(inplace=True)
prediction_errors = []
if len(predictions) != test_results.shape[0]:
print("Wrong Prediction Sizing")
return -1
for i in range(len(predictions)):
prediction_errors.append(abs(predictions[i] - test_results['Outcome SE/refraction\n(after surgery vision)'][i]))
return np.mean(prediction_errors)
def MLR(args):
regr = LinearRegression()
return fit_to_model(args, regr)
def logistic(args):
regr = LogisticRegression()
return fit_to_model(args, regr)
def SGD(args):
if args.model_type == "Full":
data = all_column_train[FULL_COLUMNS]
y_data = all_column_y
test_data = all_column_test[FULL_COLUMNS]
test_results = all_columns_test_result
else:
data = no_na_train
y_data = no_na_train_y
test_data = no_na_test
test_results = no_na_test_y
scaler = preprocessing.StandardScaler().fit(data)
data = scaler.transform(data)
test_data = scaler.transform(test_data)
n_iter = 100
regr = SGDRegressor(max_iter=n_iter)
regr.fit(data, y_data.values.ravel())
predictions = np.array(regr.predict(test_data))
test_results.reset_index(inplace=True)
return MAE(predictions, test_results)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Script for Eyelabs AI")
parser.add_argument("-tech", "--technique", required=True, help="enter technique to run predictions (MLR, Log, SGD)")
parser.add_argument("-mt", "--model_type", required=True, help="Full or All")
args = parser.parse_args()
if args.technique == "MLR":
result = MLR(args)
elif args.technique == "Log":
result = logistic(args)
elif args.technique == "SGD":
result = SGD(args)
print(result)
# data = all_column_train[FULL_COLUMNS]
# y_data = all_column_y
# test_data = all_column_test[FULL_COLUMNS]
# test_results = all_columns_test_result
# regr = LinearRegression()
# regr.fit(data, y_data)
# predictions = np.array(regr.predict(test_data))
# test_results.reset_index(inplace=True)
# for i in range(len(predictions)):
# print(predictions[i][0])
# print(test_results['IOL Power'])
# print(predictions[i][0] - test_results['IOL Power'][i])