mewamuwa commited on
Commit
b2a7669
·
verified ·
1 Parent(s): 9bcdae0

Kode til simpel træmodel

Browse files
Files changed (1) hide show
  1. Simpel_træmodel_Philip.ipynb +134 -0
Simpel_træmodel_Philip.ipynb ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "from __future__ import print_function, division # Ensures Python3 printing & division standard\n",
10
+ "import pandas as pd \n",
11
+ "from pandas import Series, DataFrame \n",
12
+ "from matplotlib import pyplot as plt\n",
13
+ "import numpy as np\n",
14
+ "\n",
15
+ "SavePlots = False"
16
+ ]
17
+ },
18
+ {
19
+ "cell_type": "markdown",
20
+ "metadata": {},
21
+ "source": [
22
+ "# Bedmap_train data"
23
+ ]
24
+ },
25
+ {
26
+ "cell_type": "code",
27
+ "execution_count": 2,
28
+ "metadata": {},
29
+ "outputs": [
30
+ {
31
+ "name": "stdout",
32
+ "output_type": "stream",
33
+ "text": [
34
+ " LON LAT THICK \\\n",
35
+ "0 76.889142 -69.876749 1046.6 \n",
36
+ "1 76.893603 -69.876762 1058.5 \n",
37
+ "2 76.899026 -69.876753 1061.1 \n",
38
+ "3 76.904100 -69.876424 1063.7 \n",
39
+ "4 76.909194 -69.876374 1069.8 \n",
40
+ "\n",
41
+ " geometry EAST \\\n",
42
+ "0 b'\\x01\\x01\\x00\\x00\\x00\\xf4\\r\\xbfW\\xa2h@A\\xe45\\... 2.150725e+06 \n",
43
+ "1 b'\\x01\\x01\\x00\\x00\\x00$\\xf8B\"\\xb5h@A\\x11-)\\xd5... 2.150762e+06 \n",
44
+ "2 b'\\x01\\x01\\x00\\x00\\x00\\xb8\\xdb\\x03T\\xcdh@A\\xb2... 2.150811e+06 \n",
45
+ "3 b'\\x01\\x01\\x00\\x00\\x00\\xe3\\xdcsj\\xf5h@A,8\\xae\\... 2.150891e+06 \n",
46
+ "4 b'\\x01\\x01\\x00\\x00\\x00\\xd5<+a\\x0ei@A\\xf3\\xef\\x... 2.150941e+06 \n",
47
+ "\n",
48
+ " NORTH v ith_bm smb z s \n",
49
+ "0 500918.979443 20.060943 1007.947592 211.410147 1155.742697 0.019393 \n",
50
+ "1 500751.208165 19.999543 1006.453881 211.418109 1158.179379 0.018178 \n",
51
+ "2 500547.878047 19.941658 1004.313773 211.428499 1161.195846 0.016543 \n",
52
+ "3 500365.723321 19.862532 1008.705660 211.463026 1163.372114 0.015692 \n",
53
+ "4 500175.761230 19.754973 1015.940170 211.472767 1165.676239 0.014873 \n"
54
+ ]
55
+ }
56
+ ],
57
+ "source": [
58
+ "import pandas as pd\n",
59
+ "\n",
60
+ "# Læs en Parquet-fil ind i en DataFrame\n",
61
+ "bedmap_train = pd.read_parquet(\"bedmap_train.parquet\")\n",
62
+ "\n",
63
+ "# Vis de første rækker\n",
64
+ "print(bedmap_train.head())"
65
+ ]
66
+ },
67
+ {
68
+ "cell_type": "code",
69
+ "execution_count": 3,
70
+ "metadata": {},
71
+ "outputs": [],
72
+ "source": [
73
+ "from sklearn.model_selection import train_test_split\n",
74
+ "from sklearn.tree import DecisionTreeRegressor\n",
75
+ "from sklearn.metrics import mean_squared_error, r2_score\n",
76
+ "# Tag 10% af dataen tilfældigt\n",
77
+ "sample_df = bedmap_train.sample(frac=0.1, random_state=42)\n",
78
+ "\n",
79
+ "# Fjern ikke-numeriske kolonner som 'geometry'\n",
80
+ "numeric_df = sample_df.select_dtypes(include='number')\n",
81
+ "\n",
82
+ "# Definér target og features\n",
83
+ "X = numeric_df.drop(columns=[\"THICK\"])\n",
84
+ "y = numeric_df[\"THICK\"]\n",
85
+ "\n",
86
+ "# Split i train/test\n",
87
+ "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)"
88
+ ]
89
+ },
90
+ {
91
+ "cell_type": "code",
92
+ "execution_count": null,
93
+ "metadata": {},
94
+ "outputs": [],
95
+ "source": [
96
+ "\n",
97
+ "# Træn beslutningstræ-model\n",
98
+ "model = DecisionTreeRegressor(random_state=42)\n",
99
+ "model.fit(X_train, y_train)\n",
100
+ "\n",
101
+ "# Forudsig på testdata\n",
102
+ "y_pred = model.predict(X_test)\n",
103
+ "\n",
104
+ "# Evaluer\n",
105
+ "mse = mean_squared_error(y_test, y_pred)\n",
106
+ "r2 = r2_score(y_test, y_pred)\n",
107
+ "\n",
108
+ "print(f\"Mean Squared Error: {mse:.2f}\")\n",
109
+ "print(f\"R^2 Score: {r2:.2f}\")\n"
110
+ ]
111
+ }
112
+ ],
113
+ "metadata": {
114
+ "kernelspec": {
115
+ "display_name": "appml",
116
+ "language": "python",
117
+ "name": "python3"
118
+ },
119
+ "language_info": {
120
+ "codemirror_mode": {
121
+ "name": "ipython",
122
+ "version": 3
123
+ },
124
+ "file_extension": ".py",
125
+ "mimetype": "text/x-python",
126
+ "name": "python",
127
+ "nbconvert_exporter": "python",
128
+ "pygments_lexer": "ipython3",
129
+ "version": "3.12.9"
130
+ }
131
+ },
132
+ "nbformat": 4,
133
+ "nbformat_minor": 4
134
+ }