From 31f879118851d03307b351828dbf8f8d205203c9 Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Sat, 6 Apr 2019 14:59:13 +0200 Subject: [PATCH] Svm strat and some parking data --- .../strategies/parking_aarhus_1430.json | 1 + server/nightr/strategies/strat_utils.py | 10 ++++- server/nightr/strategies/svm_strat.py | 44 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 server/nightr/strategies/parking_aarhus_1430.json create mode 100644 server/nightr/strategies/svm_strat.py diff --git a/server/nightr/strategies/parking_aarhus_1430.json b/server/nightr/strategies/parking_aarhus_1430.json new file mode 100644 index 0000000..aab6620 --- /dev/null +++ b/server/nightr/strategies/parking_aarhus_1430.json @@ -0,0 +1 @@ +{"help": "https://portal.opendata.dk/api/3/action/help_show?name=datastore_search", "success": true, "result": {"include_total": true, "resource_id": "2a82a145-0195-4081-a13c-b0e587e9b89c", "fields": [{"type": "int", "id": "_id"}, {"type": "text", "id": "date"}, {"type": "text", "id": "garageCode"}, {"type": "int4", "id": "totalSpaces"}, {"type": "int4", "id": "vehicleCount"}], "records_format": "objects", "records": [{"_id": 1, "date": "2019/04/06 14:30:01", "garageCode": "NORREPORT", "totalSpaces": 80, "vehicleCount": 61}, {"_id": 2, "date": "2019/04/06 14:30:01", "garageCode": "SCANDCENTER", "totalSpaces": 1240, "vehicleCount": 1033}, {"_id": 6, "date": "2019/04/06 14:30:01", "garageCode": "SALLING", "totalSpaces": 700, "vehicleCount": 575}, {"_id": 7, "date": "2019/04/06 14:30:01", "garageCode": "DOKK1", "totalSpaces": 1000, "vehicleCount": 0}, {"_id": 8, "date": "2019/04/06 14:30:01", "garageCode": "Navitas", "totalSpaces": 449, "vehicleCount": 208}, {"_id": 9, "date": "2019/04/06 14:30:01", "garageCode": "NewBusgadehuset", "totalSpaces": 105, "vehicleCount": 101}, {"_id": 3, "date": "2019/04/06 14:30:01", "garageCode": "BRUUNS", "totalSpaces": 953, "vehicleCount": 598}, {"_id": 4, "date": "2019/04/06 14:30:01", "garageCode": "MAGASIN", "totalSpaces": 378, "vehicleCount": 361}, {"_id": 5, "date": "2019/04/06 14:30:01", "garageCode": "KALKVAERKSVEJ", "totalSpaces": 210, "vehicleCount": 278}, {"_id": 10, "date": "2019/04/06 14:30:01", "garageCode": "Urban Level 1", "totalSpaces": 319, "vehicleCount": 99}, {"_id": 11, "date": "2019/04/06 14:30:01", "garageCode": "Urban Level 2+3", "totalSpaces": 654, "vehicleCount": 170}], "_links": {"start": "/api/3/action/datastore_search?resource_id=2a82a145-0195-4081-a13c-b0e587e9b89c", "next": "/api/3/action/datastore_search?offset=100&resource_id=2a82a145-0195-4081-a13c-b0e587e9b89c"}, "total": 11}} \ No newline at end of file diff --git a/server/nightr/strategies/strat_utils.py b/server/nightr/strategies/strat_utils.py index 97fc3ea..2e77640 100644 --- a/server/nightr/strategies/strat_utils.py +++ b/server/nightr/strategies/strat_utils.py @@ -1,6 +1,7 @@ import pandas as pd import urllib.request - +import json +import requests def determine_month(): ds = pd.read_excel(urllib.request.urlopen('https://sundogbaelt.dk/wp-content/uploads/2019/04/trafiktal-maaned.xls')) @@ -12,3 +13,10 @@ def determine_month(): last_year_total = sum(ds['Total'][amount_of_cur_year+1:amount_of_cur_year+13]) return ((12/(last_year_total//cur_year_total))+1), cur_year_total, last_year_total + + + +def write_json(url, data_name, time): + r = requests.get(url) + with open(f"{data_name}_{time}.json", 'w') as f: + json.dump(r.json(), f) diff --git a/server/nightr/strategies/svm_strat.py b/server/nightr/strategies/svm_strat.py new file mode 100644 index 0000000..78a760c --- /dev/null +++ b/server/nightr/strategies/svm_strat.py @@ -0,0 +1,44 @@ +from sklearn import svm +from sklearn.externals import joblib +import requests +import glob +import json +import numpy as np + + +from server.nightr.strategies.strat_utils import write_json + + +def find_data(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(): + + X = [] + Y = [] + + for filename in glob.glob("parking_aarhus*"): + p_class = '2330' in filename + + with open(filename) as file: + data = json.load(file) + + records = data['result']['records'] + frequencies = [house['vehicleCount'] / house['totalSpaces'] for house in records] + X.append(frequencies) + Y.append(int(p_class)) + + return np.array(X), np.array(Y) + +def train(): + X, Y = load_data() + classifier = svm.SVC(C=10, gamma=0.01, probability=True) + classifier.fit(X, Y) + joblib.dump(classifier, "nightness_classifier.pkl") + +def predict(X): + classifier = joblib.load("nightness_classifier.pkl") + prob = classifier.predict_proba(X) + return prob[0, 1] + +train()