-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoML.py
More file actions
254 lines (170 loc) · 6.03 KB
/
Copy pathAutoML.py
File metadata and controls
254 lines (170 loc) · 6.03 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# -*- coding: utf-8 -*-
"""Copy of AutoML.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1n3DBE2sxY28sYUksQH6tofMmqOYF5UEq
"""
!pip install tpot mljar-supervised
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from supervised.automl import AutoML
"""# Options Available
- mode — the package ships with four built-in models.
- The Explain mode is ideal for explaining and understanding the data. It results in visualizations of feature importance as well as tree visualizations.
- The Perform is used when building ML models for production.
- The Compete is meant to build models used in machine learning competitions.
- The Optuna mode is used to search for highly-tuned ML models.
- algorithms — specifies the algorithms you would like to use. They are usually passed in as a list.
- results_path — the path where the results will be stored
- total_time_limit — the total time in seconds for training the model
- train_ensemble — dictates if an ensemble will be created at the end of the training process
- stack_models — determines if a models stack will be created
- eval_metric — the metric that will be optimized. If auto the logloss is used for classification problems while the rmse is used for regression problems
"""
#automl = AutoML(
# mode="Explain"
# algorithms=""
# results_path="AutoML_22",
# total_time_limit=30 * 60,
# train_ensemble=True,
# stack_models="",
# eval_metric=""
#)
"""# Healthcare Dataset - [Modified SPARCS dataset](https://raw.githubusercontent.com/hantswilliams/HHA-507-2022/main/autoML/datasets/data_sparcs.csv)
## Load In Dataset
"""
import pandas as pd
sparcs = pd.read_csv('https://raw.githubusercontent.com/hantswilliams/HHA-507-2022/main/autoML/datasets/data_sparcs.csv')
sparcs
sparcs.columns
"""## Potential Variables Of Interest
- Type of Admission (categorical)
- Total Charges (continuous)
- Gender (categorical)
- Race (categorical)
"""
sparcs['Total Charges'].describe()
sparcs['Gender'].describe()
sparcs['Type of Admission'].value_counts()
"""## Create Some Simplified Binary Versions"""
sparcs['Race'] = pd.to_numeric(sparcs['Race'], errors='coerce')
sparcs['sparcs_race'] = sparcs['Race'].apply(lambda x: 'long' if x > 3 else 'short')
sparcs.drop('Race', axis=1, inplace=True)
sparcs['sparcs_race'].value_counts()
"""# MLJar Examples
## Binary Classifier Example 1 - SPARCS
### **Create new model**
"""
x = sparcs.drop(columns=['Race'])
y = sparcs["sparcs_race"]
x
y
x_train, x_test, y_train, y_test = train_test_split(x, y, stratify=y, test_size=0.25)
x_test
automl = AutoML(results_path="Race", mode="Explain")
automl.fit(X_train, y_train)
pred = automl.predict(x_test)
pred
automl.report()
"""### **Test New (not really) Data**"""
# load in the data model
automl_sparcs_race = AutoML(results_path="Race")
# create a new dataset that follows the same data structure as the training set
X_withrace = sparcs.sample(25)
X_withoutrace = X_withrace.drop(columns=['Race'])
X_withrace
X_withoutrace
predict = automl.predict(X_withoutrace)
predict
# actual values from X_withrace
values_actual = X_withrace['Race'].values.tolist()
values_predicted = predict.tolist()
output = pd.DataFrame({'actual': values_actual, 'predicted': values_predicted})
output
"""## **Binary Classifier Example 2 - GENERIC**"""
import pandas as pd
from supervised.automl import AutoML
import os
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
df = pd.read_csv("https://raw.githubusercontent.com/hantswilliams/HHA-507-2022/main/autoML/datasets/data_binary_bank.csv")
X = df[df.columns[:-1]]
y = df["y"]
X
y
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, test_size=0.25)
automl = AutoML(
# results_path="AutoML_22",
# total_time_limit=30 * 60,
# start_random_models=10,
# hill_climbing_steps=3,
# top_models_to_improve=3,
# train_ensemble=True,
mode="Explain"
)
automl.fit(X_train, y_train)
pred = automl.predict(X_test)
pred
# print("Test accuracy", accuracy_score(y_test, pred["label"]))
automl.report()
"""## **Regression - Example - GENERIC**"""
import numpy as np
import pandas as pd
from supervised.automl import AutoML
df = pd.read_csv("https://raw.githubusercontent.com/hantswilliams/HHA-507-2022/main/autoML/datasets/data_sparcs.csv")
x_cols = [c for c in df.columns if c != "Total Charges"]
X = df[x_cols]
y = df["Total Charges"]
df
x_cols
X
y
automl = AutoML(results_path="sparcs_regression", mode="Explain")
automl.fit(X, y)
df["Predictions"] = automl.predict(X)
print("Predictions")
print(df[["Total Charges", "Predictions"]].head())
"""## **Multiclass Classifier - GENERIC**"""
import pandas as pd
import numpy as np
from supervised.automl import AutoML
import supervised
import warnings
from sklearn import datasets
from sklearn.pipeline import make_pipeline
from sklearn.decomposition import PCA
from supervised import AutoML
from supervised.exceptions import AutoMLException
# warnings.filterwarnings('error')
warnings.filterwarnings(
"error", category=pd.core.common.SettingWithCopyWarning
) # message="*ndarray*")
df = pd.read_csv("https://raw.githubusercontent.com/hantswilliams/HHA-507-2022/main/autoML/datasets/data_classes_iris.csv")
X = df[["feature_1", "feature_2", "feature_3", "feature_4"]]
y = df["class"]
df
X
y.value_counts()
automl = AutoML()
automl.fit(X, y)
predictions = automl.predict_all(X)
print(predictions.head())
print(predictions.tail())
print(X.shape)
print(predictions.shape)
"""## **Download Outputs**
### **Binary Classifications Download**
"""
# get current working directory
import os
os.getcwd()
folders = os.listdir()
foldersML = [x for x in folders if x.startswith('Race')]
print(foldersML)
!zip -r /content/Race.zip /content/Race
"""##**Regression Download**"""
os.getcwd()
folders = os.listdir()
foldersML = [x for x in folders if x.startswith('sparcs_regression')]
print(foldersML)
!zip -r /content/Regression.zip /content/sparcs_regression