-
Notifications
You must be signed in to change notification settings - Fork 1
/
reg.py
121 lines (82 loc) · 2.69 KB
/
reg.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from data import Data
import numpy as np
import gmaps
from sklearn.linear_model import Ridge, LogisticRegression, LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import normalize
from sklearn.pipeline import make_pipeline
from sklearn import mixture
from sklearn import svm
from sklearn.metrics import r2_score
import matplotlib.pyplot as plt
CLIENTR = 5000
TASKR = 2500
STATIONR = 10000
if __name__ == "__main__":
data = Data()
taskActive = list()
taskDensity = list()
taskTrans = list()
price = list()
label = list()
for task in data.tasksCom:
price.append(task.price)
score = 0
density = 0
trans = 0
for client in data.clients:
if task.haversine(client) < CLIENTR:
score += np.log10(client.repu)
for other in data.tasksCom:
if task.haversine(other) < TASKR:
density += 1
for station in data.stations:
if task.haversine(station) < STATIONR:
trans += 1
taskActive.append(score)
taskDensity.append(density)
taskTrans.append(trans)
label.append(int(task.finished))
features = list(zip(taskActive, taskDensity))
features = np.array(features)
print(features.shape)
features[:, 0] = features[:, 0] / np.max(features[:, 0])
features[:, 1] = features[:, 1] / np.max(features[:, 1])
# features[:, 2] = features[:, 2] / np.max(features[:, 2])
price = np.array(price)
priceMean = np.mean(price)
price = price - priceMean
priceMax = np.max(np.abs(price))
priceNormal = price / np.max(np.abs(price))
model = make_pipeline(PolynomialFeatures(2), LinearRegression(n_jobs=16))
model.fit(X=features, y=priceNormal)
pred = model.predict(features) * priceMax + priceMean
truth = priceNormal * priceMax + priceMean
idx = np.abs(pred - truth) < 4
features = features[idx]
priceNormal = priceNormal[idx]
model.fit(X=features, y=priceNormal)
pred = model.predict(features) * priceMax + priceMean
truth = priceNormal * priceMax + priceMean
print(r2_score(truth, pred))
idx = np.abs(pred - truth) < 2
features = features[idx]
priceNormal = priceNormal[idx]
model.fit(X=features, y=priceNormal)
pred = model.predict(features) * priceMax + priceMean
truth = priceNormal * priceMax + priceMean
print(r2_score(truth, pred))
print(pred[250:262])
print(truth[250:262])
plt.show()
'''
x0 = 0.7 * np.ones((x0.shape))
x1 = np.arange(0, 1, 0.01, dtype=np.float32)[:, np.newaxis]
y = model.predict(np.concatenate((x0, x1), axis=1))
np.random.seed(seed=1016)
plt.scatter(x0, y, c='c', s=1)
plt.xlabel('Task Density')
plt.ylabel('Price')
plt.show()
'''