Merge remote-tracking branch 'origin/master'

This commit is contained in:
Milo 2019-04-06 22:45:14 +02:00
commit 64108f67f9
2 changed files with 20 additions and 6 deletions

Binary file not shown.

View file

@ -6,10 +6,11 @@ import json
import numpy as np import numpy as np
from server.nightr.strategies.strat_utils import write_json from .strat_utils import write_json
from ..util import Context, Prediction
def find_data(time): def write_data(time):
write_json("https://portal.opendata.dk/api/3/action/datastore_search?resource_id=2a82a145-0195-4081-a13c-b0e587e9b89c", "parking_aarhus", time) write_json("https://portal.opendata.dk/api/3/action/datastore_search?resource_id=2a82a145-0195-4081-a13c-b0e587e9b89c", "parking_aarhus", time)
def load_data(): def load_data():
@ -18,7 +19,7 @@ def load_data():
Y = [] Y = []
for filename in glob.glob("parking_aarhus*"): for filename in glob.glob("parking_aarhus*"):
p_class = '2330' in filename p_class = '2235' in filename
with open(filename) as file: with open(filename) as file:
data = json.load(file) data = json.load(file)
@ -32,13 +33,26 @@ def load_data():
def train(): def train():
X, Y = load_data() X, Y = load_data()
classifier = svm.SVC(C=10, gamma=0.01, probability=True) classifier = svm.SVC(gamma=0.01, probability=True)
classifier.fit(X, Y) classifier.fit(X, Y)
joblib.dump(classifier, "nightness_classifier.pkl") joblib.dump(classifier, "nightness_classifier.pkl")
def predict(X): def predict(X):
classifier = joblib.load("nightness_classifier.pkl") classifier = joblib.load("nightness_classifier.pkl")
prob = classifier.predict_proba(X) prob = classifier.predict_proba(np.array(X).reshape(1, -1))
return prob[0, 1] return prob[0, 1]
train()
def perform_svm_pred(context: Context) -> Prediction:
p = Prediction()
data = requests.get('https://portal.opendata.dk/api/3/action/datastore_search?resource_id=2a82a145-0195-4081-a13c-b0e587e9b89c')
records = data.json()['result']['records']
X = [house['vehicleCount'] / house['totalSpaces'] for house in records]
X = [min(x, 1) for x in X]
p.reasons.append("Since we only have two data points")
p.reasons.append("Since our only two data points have 11 dimensions")
p.reasons.append("Since we are using a SVM")
p.probability = predict(X)
return p