From 8ff423a4be02bfad5dbef1f740d4cafd7daeb3b3 Mon Sep 17 00:00:00 2001 From: David Neuhauss Date: Thu, 6 Nov 2025 18:47:07 +0100 Subject: [PATCH] push ml code for work on other notebook --- ml_stuff/environment.yml | 17 ++++ ml_stuff/feature_vectors.ipynb | 168 ++++++++++++++++++--------------- ml_stuff/train.ipynb | 98 +++++++++++++++++++ 3 files changed, 205 insertions(+), 78 deletions(-) create mode 100644 ml_stuff/train.ipynb diff --git a/ml_stuff/environment.yml b/ml_stuff/environment.yml index 60b4a73..f9fde56 100644 --- a/ml_stuff/environment.yml +++ b/ml_stuff/environment.yml @@ -34,18 +34,30 @@ dependencies: - zlib=1.3.1=hb25bd0a_0 - pip: - asttokens==3.0.0 + - branca==0.8.2 + - certifi==2025.10.5 + - charset-normalizer==3.4.4 - comm==0.2.3 + - contourpy==1.3.3 + - cycler==0.12.1 - debugpy==1.8.17 - decorator==5.2.1 - executing==2.2.1 + - folium==0.20.0 + - fonttools==4.60.1 - haversine==2.9.0 + - idna==3.11 - ipykernel==7.0.1 - ipython==9.6.0 - ipython-pygments-lexers==1.1.1 - jedi==0.19.2 + - jinja2==3.1.6 - joblib==1.5.2 - jupyter-client==8.6.3 - jupyter-core==5.9.1 + - kiwisolver==1.4.9 + - markupsafe==3.0.3 + - matplotlib==3.10.7 - matplotlib-inline==0.1.7 - nest-asyncio==1.6.0 - numpy==2.3.4 @@ -53,15 +65,18 @@ dependencies: - pandas==2.3.3 - parso==0.8.5 - pexpect==4.9.0 + - pillow==12.0.0 - platformdirs==4.5.0 - prompt-toolkit==3.0.52 - psutil==7.1.1 - ptyprocess==0.7.0 - pure-eval==0.2.3 - pygments==2.19.2 + - pyparsing==3.2.5 - python-dateutil==2.9.0.post0 - pytz==2025.2 - pyzmq==27.1.0 + - requests==2.32.5 - scikit-learn==1.7.2 - scipy==1.16.2 - six==1.17.0 @@ -71,5 +86,7 @@ dependencies: - traitlets==5.14.3 - typing-extensions==4.15.0 - tzdata==2025.2 + - urllib3==2.5.0 - wcwidth==0.2.14 + - xyzservices==2025.10.0 prefix: /home/david/AndroidStudioProjects/SuMoC_Demo/ml_stuff/env diff --git a/ml_stuff/feature_vectors.ipynb b/ml_stuff/feature_vectors.ipynb index 6f508a2..ff425e1 100644 --- a/ml_stuff/feature_vectors.ipynb +++ b/ml_stuff/feature_vectors.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 15, + "execution_count": 16, "id": "c5a83572", "metadata": {}, "outputs": [], @@ -13,19 +13,19 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 17, "id": "66e03da2", "metadata": {}, "outputs": [], "source": [ "\n", - "DATA_PATH = \"data/location_history_good.csv\" # Path format for jupyter notebook\n", + "DATA_PATH = \"data/location_database-location_history.csv\" # Path format for jupyter notebook\n", "df_raw = pd.read_csv(DATA_PATH, sep=\";\")" ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 18, "id": "029361d3", "metadata": {}, "outputs": [ @@ -33,20 +33,20 @@ "name": "stdout", "output_type": "stream", "text": [ - " id latitude longitude transportMode timestamp\n", - "0 1 51.440447 6.983873 Foot 1761153100476\n", - "1 2 51.440398 6.984027 Foot 1761153115459\n", - "2 3 51.440418 6.984282 Foot 1761153130457\n", - "3 4 51.440433 6.984540 Foot 1761153145461\n", - "4 5 51.440438 6.984803 Foot 1761153160455\n", - ".. ... ... ... ... ...\n", - "126 127 51.440375 6.985233 Car 1761155048785\n", - "127 128 51.440392 6.986020 Car 1761155063785\n", - "128 129 51.440765 6.986122 Car 1761155078787\n", - "129 130 51.441315 6.986172 Car 1761155093790\n", - "130 131 51.441793 6.986340 Car 1761155108792\n", + " id tripId latitude longitude transportMode timestamp\n", + "0 1 1 51.440474 6.984306 Walking 1761821697395\n", + "1 2 1 51.440393 6.984244 Walking 1761821705558\n", + "2 3 1 51.440327 6.984411 Walking 1761821710129\n", + "3 4 1 51.440308 6.984443 Walking 1761821714979\n", + "4 5 1 51.440267 6.984537 Walking 1761821719781\n", + ".. ... ... ... ... ... ...\n", + "595 596 5 51.440431 6.983897 Walking 1761837113001\n", + "596 597 5 51.440426 6.983890 Walking 1761837120168\n", + "597 598 5 51.440419 6.983848 Walking 1761837152589\n", + "598 599 5 51.440412 6.983824 Walking 1761837159514\n", + "599 600 5 51.440418 6.983844 Walking 1761837249888\n", "\n", - "[131 rows x 5 columns]\n" + "[600 rows x 6 columns]\n" ] } ], @@ -56,7 +56,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 19, "id": "5294e9db", "metadata": {}, "outputs": [ @@ -65,17 +65,17 @@ "output_type": "stream", "text": [ "0 NaN\n", - "1 51.440447\n", - "2 51.440398\n", - "3 51.440418\n", - "4 51.440433\n", + "1 51.440474\n", + "2 51.440393\n", + "3 51.440327\n", + "4 51.440308\n", " ... \n", - "126 51.440362\n", - "127 51.440375\n", - "128 51.440392\n", - "129 51.440765\n", - "130 51.441315\n", - "Name: latitude, Length: 131, dtype: float64\n" + "595 51.440413\n", + "596 51.440431\n", + "597 51.440426\n", + "598 51.440419\n", + "599 51.440412\n", + "Name: latitude, Length: 600, dtype: float64\n" ] } ], @@ -85,7 +85,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 20, "id": "6200d505", "metadata": {}, "outputs": [ @@ -94,32 +94,32 @@ "output_type": "stream", "text": [ " lat_prev lat_curr lon_prev lon_curr timestamp_prev timestamp \\\n", - "0 51.440447 51.440398 6.983873 6.984027 1.761153e+12 1761153115459 \n", - "1 51.440398 51.440418 6.984027 6.984282 1.761153e+12 1761153130457 \n", - "2 51.440418 51.440433 6.984282 6.984540 1.761153e+12 1761153145461 \n", - "3 51.440433 51.440438 6.984540 6.984803 1.761153e+12 1761153160455 \n", - "4 51.440438 51.440438 6.984803 6.984803 1.761153e+12 1761153174881 \n", + "0 51.440474 51.440393 6.984306 6.984244 1.761822e+12 1761821705558 \n", + "1 51.440393 51.440327 6.984244 6.984411 1.761822e+12 1761821710129 \n", + "2 51.440327 51.440308 6.984411 6.984443 1.761822e+12 1761821714979 \n", + "3 51.440308 51.440267 6.984443 6.984537 1.761822e+12 1761821719781 \n", + "4 51.440267 51.440308 6.984537 6.984584 1.761822e+12 1761821727714 \n", ".. ... ... ... ... ... ... \n", - "125 51.440362 51.440375 6.984472 6.985233 1.761155e+12 1761155048785 \n", - "126 51.440375 51.440392 6.985233 6.986020 1.761155e+12 1761155063785 \n", - "127 51.440392 51.440765 6.986020 6.986122 1.761155e+12 1761155078787 \n", - "128 51.440765 51.441315 6.986122 6.986172 1.761155e+12 1761155093790 \n", - "129 51.441315 51.441793 6.986172 6.986340 1.761155e+12 1761155108792 \n", + "594 51.440413 51.440431 6.983899 6.983897 1.761837e+12 1761837113001 \n", + "595 51.440431 51.440426 6.983897 6.983890 1.761837e+12 1761837120168 \n", + "596 51.440426 51.440419 6.983890 6.983848 1.761837e+12 1761837152589 \n", + "597 51.440419 51.440412 6.983848 6.983824 1.761837e+12 1761837159514 \n", + "598 51.440412 51.440418 6.983824 6.983844 1.761837e+12 1761837249888 \n", "\n", - " transport_mode_curr \n", - "0 Foot \n", - "1 Foot \n", - "2 Foot \n", - "3 Foot \n", - "4 Foot \n", - ".. ... \n", - "125 Car \n", - "126 Car \n", - "127 Car \n", - "128 Car \n", - "129 Car \n", + " transport_mode_curr trip_id \n", + "0 Walking 1 \n", + "1 Walking 1 \n", + "2 Walking 1 \n", + "3 Walking 1 \n", + "4 Walking 1 \n", + ".. ... ... \n", + "594 Walking 5 \n", + "595 Walking 5 \n", + "596 Walking 5 \n", + "597 Walking 5 \n", + "598 Walking 5 \n", "\n", - "[130 rows x 7 columns]\n" + "[599 rows x 8 columns]\n" ] } ], @@ -131,7 +131,8 @@ " 'lon_curr': df_raw['longitude'],\n", " 'timestamp_prev': df_raw['timestamp'].shift(1),\n", " 'timestamp': df_raw['timestamp'],\n", - " 'transport_mode_curr': df_raw['transportMode']\n", + " 'transport_mode_curr': df_raw['transportMode'],\n", + " 'trip_id': df_raw['tripId']\n", "})\n", "df_feature = df_feature.dropna().reset_index(drop=True) # Dropping first row that has nan because of the shift\n", "\n", @@ -140,7 +141,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 21, "id": "57cd55f2", "metadata": {}, "outputs": [], @@ -163,7 +164,18 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, + "id": "f1d291d9", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, "id": "c21b02e9", "metadata": {}, "outputs": [ @@ -172,32 +184,32 @@ "output_type": "stream", "text": [ " lat_prev lat_curr lon_prev lon_curr timestamp_prev timestamp \\\n", - "0 51.440447 51.440398 6.983873 6.984027 1.761153e+12 1761153115459 \n", - "1 51.440398 51.440418 6.984027 6.984282 1.761153e+12 1761153130457 \n", - "2 51.440418 51.440433 6.984282 6.984540 1.761153e+12 1761153145461 \n", - "3 51.440433 51.440438 6.984540 6.984803 1.761153e+12 1761153160455 \n", - "4 51.440438 51.440438 6.984803 6.984803 1.761153e+12 1761153174881 \n", + "0 51.440474 51.440393 6.984306 6.984244 1.761822e+12 1761821705558 \n", + "1 51.440393 51.440327 6.984244 6.984411 1.761822e+12 1761821710129 \n", + "2 51.440327 51.440308 6.984411 6.984443 1.761822e+12 1761821714979 \n", + "3 51.440308 51.440267 6.984443 6.984537 1.761822e+12 1761821719781 \n", + "4 51.440267 51.440308 6.984537 6.984584 1.761822e+12 1761821727714 \n", ".. ... ... ... ... ... ... \n", - "125 51.440362 51.440375 6.984472 6.985233 1.761155e+12 1761155048785 \n", - "126 51.440375 51.440392 6.985233 6.986020 1.761155e+12 1761155063785 \n", - "127 51.440392 51.440765 6.986020 6.986122 1.761155e+12 1761155078787 \n", - "128 51.440765 51.441315 6.986122 6.986172 1.761155e+12 1761155093790 \n", - "129 51.441315 51.441793 6.986172 6.986340 1.761155e+12 1761155108792 \n", + "594 51.440413 51.440431 6.983899 6.983897 1.761837e+12 1761837113001 \n", + "595 51.440431 51.440426 6.983897 6.983890 1.761837e+12 1761837120168 \n", + "596 51.440426 51.440419 6.983890 6.983848 1.761837e+12 1761837152589 \n", + "597 51.440419 51.440412 6.983848 6.983824 1.761837e+12 1761837159514 \n", + "598 51.440412 51.440418 6.983824 6.983844 1.761837e+12 1761837249888 \n", "\n", - " transport_mode_curr distance speed \n", - "0 Foot 11.916805 0.795355 \n", - "1 Foot 17.813678 1.187737 \n", - "2 Foot 17.980562 1.198385 \n", - "3 Foot 18.258052 1.217691 \n", - "4 Foot 0.000000 0.000000 \n", - ".. ... ... ... \n", - "125 Car 52.808044 3.520302 \n", - "126 Car 54.558633 3.637242 \n", - "127 Car 42.103378 2.806518 \n", - "128 Car 61.255403 4.082877 \n", - "129 Car 54.448773 3.629434 \n", + " transport_mode_curr trip_id distance speed \n", + "0 Walking 1 9.980398 1.222638 \n", + "1 Walking 1 13.734566 3.004718 \n", + "2 Walking 1 3.062350 0.631412 \n", + "3 Walking 1 7.970360 1.659800 \n", + "4 Walking 1 5.689791 0.717231 \n", + ".. ... ... ... ... \n", + "594 Walking 5 2.073842 0.260010 \n", + "595 Walking 5 0.798796 0.111455 \n", + "596 Walking 5 3.033092 0.093553 \n", + "597 Walking 5 1.869658 0.269987 \n", + "598 Walking 5 1.612701 0.017845 \n", "\n", - "[130 rows x 9 columns]\n" + "[599 rows x 10 columns]\n" ] } ], diff --git a/ml_stuff/train.ipynb b/ml_stuff/train.ipynb new file mode 100644 index 0000000..ee286cd --- /dev/null +++ b/ml_stuff/train.ipynb @@ -0,0 +1,98 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "id": "371388bb", + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "c03fee70", + "metadata": {}, + "outputs": [], + "source": [ + "FEATURE_PATH = \"data/feature_vectors.csv\"" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "ca2999bb", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'pd' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[1]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m df_features = \u001b[43mpd\u001b[49m.read_csv(FEATURE_PATH, sep=\u001b[33m\"\u001b[39m\u001b[33m;\u001b[39m\u001b[33m\"\u001b[39m)\n", + "\u001b[31mNameError\u001b[39m: name 'pd' is not defined" + ] + } + ], + "source": [ + "df_features = pd.read_csv(FEATURE_PATH, sep=\";\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "2866fa6c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " ,lat_prev,lat_curr,lon_prev,lon_curr,timestamp_prev,timestamp,transport_mode_curr,distance,speed\n", + "0 0,51.4404467,51.4403983,6.9838733,6.9840267,17... \n", + "1 1,51.4403983,51.4404183,6.9840267,6.9842817,17... \n", + "2 2,51.4404183,51.4404333,6.9842817,6.98454,1761... \n", + "3 3,51.4404333,51.4404383,6.98454,6.9848033,1761... \n", + "4 4,51.4404383,51.4404383,6.9848033,6.9848033,17... \n", + ".. ... \n", + "125 125,51.4403617,51.440375,6.9844717,6.9852333,1... \n", + "126 126,51.440375,51.4403917,6.9852333,6.98602,176... \n", + "127 127,51.4403917,51.440765,6.98602,6.9861217,176... \n", + "128 128,51.440765,51.441315,6.9861217,6.9861717,17... \n", + "129 129,51.441315,51.4417933,6.9861717,6.98634,176... \n", + "\n", + "[130 rows x 1 columns]\n" + ] + } + ], + "source": [ + "print(df_features)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "env", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.14" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}