-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpredict.py
141 lines (105 loc) · 4.26 KB
/
predict.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import argparse
import paddle
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
from paddle_tabnet.tab_model import TabNetClassifier
np.random.seed(0)
import os
import wget
from pathlib import Path
import shutil
import gzip
from matplotlib import pyplot as plt
parser = argparse.ArgumentParser(description='Model evaluation')
parser.add_argument(
'--model_path',
dest='model_path',
help='The path of model for evaluation',
type=str,
default=None)
args = parser.parse_args()
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/covtype/covtype.data.gz"
dataset_name = 'forest-cover-type'
tmp_out = Path('./data/'+dataset_name+'.gz')
out = Path(os.getcwd()+'/data/'+dataset_name+'.csv')
out.parent.mkdir(parents=True, exist_ok=True)
if out.exists():
print("File already exists.")
else:
print("Downloading file...")
wget.download(url, tmp_out.as_posix())
with gzip.open(tmp_out, 'rb') as f_in:
with open(out, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
target = "Covertype"
bool_columns = [
"Wilderness_Area1", "Wilderness_Area2", "Wilderness_Area3",
"Wilderness_Area4", "Soil_Type1", "Soil_Type2", "Soil_Type3", "Soil_Type4",
"Soil_Type5", "Soil_Type6", "Soil_Type7", "Soil_Type8", "Soil_Type9",
"Soil_Type10", "Soil_Type11", "Soil_Type12", "Soil_Type13", "Soil_Type14",
"Soil_Type15", "Soil_Type16", "Soil_Type17", "Soil_Type18", "Soil_Type19",
"Soil_Type20", "Soil_Type21", "Soil_Type22", "Soil_Type23", "Soil_Type24",
"Soil_Type25", "Soil_Type26", "Soil_Type27", "Soil_Type28", "Soil_Type29",
"Soil_Type30", "Soil_Type31", "Soil_Type32", "Soil_Type33", "Soil_Type34",
"Soil_Type35", "Soil_Type36", "Soil_Type37", "Soil_Type38", "Soil_Type39",
"Soil_Type40"
]
int_columns = [
"Elevation", "Aspect", "Slope", "Horizontal_Distance_To_Hydrology",
"Vertical_Distance_To_Hydrology", "Horizontal_Distance_To_Roadways",
"Hillshade_9am", "Hillshade_Noon", "Hillshade_3pm",
"Horizontal_Distance_To_Fire_Points"
]
feature_columns = (
int_columns + bool_columns + [target])
train = pd.read_csv(out, header=None, names=feature_columns)
n_total = len(train)
# Train, val and test split follows
# Rory Mitchell, Andrey Adinets, Thejaswi Rao, and Eibe Frank.
# Xgboost: Scalable GPU accelerated learning. arXiv:1806.11248, 2018.
train_val_indices, test_indices = train_test_split(
range(n_total), test_size=0.2, random_state=0)
train_indices, valid_indices = train_test_split(
train_val_indices, test_size=0.2 / 0.8, random_state=0)
categorical_columns = []
categorical_dims = {}
for col in train.columns[train.dtypes == object]:
print(col, train[col].nunique())
l_enc = LabelEncoder()
train[col] = train[col].fillna("VV_likely")
train[col] = l_enc.fit_transform(train[col].values)
categorical_columns.append(col)
categorical_dims[col] = len(l_enc.classes_)
for col in train.columns[train.dtypes == 'float64']:
train.fillna(train.loc[train_indices, col].mean(), inplace=True)
unused_feat = []
features = [ col for col in train.columns if col not in unused_feat+[target]]
cat_idxs = [ i for i, f in enumerate(features) if f in categorical_columns]
cat_dims = [ categorical_dims[f] for i, f in enumerate(features) if f in categorical_columns]
clf = TabNetClassifier(
n_d=64, n_a=64, n_steps=5,
gamma=1.5, n_independent=2, n_shared=2,
cat_idxs=cat_idxs,
cat_dims=cat_dims,
cat_emb_dim=1,
lambda_sparse=1e-4, momentum=0.7, clip_value=2.,
optimizer_fn=paddle.optimizer.Adam,
optimizer_params=dict(learning_rate=2e-2),
scheduler_params={
"learning_rate": 2e-2, "gamma": 0.95},
scheduler_fn=paddle.optimizer.lr.ExponentialDecay,
epsilon=1e-15
)
X_train = train[features].values[train_indices]
y_train = train[target].values[train_indices]
X_valid = train[features].values[valid_indices]
y_valid = train[target].values[valid_indices]
X_test = train[features].values[test_indices]
y_test = train[target].values[test_indices]
clf.load_model(args.model_path)
y_pred = clf.predict(X_test)
test_acc = accuracy_score(y_pred=y_pred, y_true=y_test)
print(f"FINAL TEST SCORE FOR {dataset_name} : {test_acc}")