-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__main__.py
More file actions
86 lines (69 loc) · 3.19 KB
/
Copy path__main__.py
File metadata and controls
86 lines (69 loc) · 3.19 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
import argparse
import numpy as np
from rembrandtml.configuration import DataConfig, ModelConfig, ContextConfig, VisualizationConfig
from rembrandtml.factories import ContextFactory
from rembrandtml.visualization import Visualizer
from rembrandtml.models import ModelType
from sklearn.metrics import roc_curve, auc
def main(args=None):
print('you are running safetoswim')
# 1. Define the datasource.
# dataset = 'iris'
# data_file = os.path.abspath(os.path.abspath(os.path.join(os.getcwd(), '..', '..', 'data', 'gapminder', 'gm_2008_region.csv')))
# data_config = DataConfig('pandas', dataset, data_file)
data_config = DataConfig('sklearn', 'iris')
# 2. Define the models.
model_configs = []
model_configs.append(ModelConfig('Sklearn LogReg', 'sklearn', ModelType.LOGISTIC_REGRESSION))
model_configs.append(ModelConfig('Sklearn SVC', 'sklearn', ModelType.SVC))
# 3. Create the Context.
context_config = ContextConfig(model_configs, data_config)
context_config.visualization_config = VisualizationConfig((8, 8), 'ggplot')
context = ContextFactory.create(context_config)
# 4. Prepare the data.
# Use only two features for plotting
# features = ('sepal length (cm)', 'sepal width (cm)')
features = ('petal length (cm)', 'petal width (cm)')
'''
plt.imshow(train_set_x_orig[index])
### START CODE HERE ### (≈ 2 lines of code)
train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T
train_set_x = train_set_x_flatten / 255.
test_set_x = test_set_x_flatten / 255.
'''
# override data management to turn multiclassification problem into binary classification
from sklearn import datasets
iris = datasets.load_iris()
X = iris["data"][:, 3:] # petal width
y = (iris["target"] == 2).astype(np.int)
context.data_container.X = X
context.data_container.y = y
context.data_container.split()
# context.prepare_data(features=features)
# 5. Train the model.
context.train()
# 6 Evaluate the model.
scores = context.evaluate()
print('Scores:')
for name, score in scores.items():
print(f'\n\tScore[{name}] - {score}')
# 7. Make predictions.
predictions = context.predict(context.data_container.X_test)
for name, prediction in predictions.items():
# df = pd.DataFrame({'Prediction': [[max(i) for i in predictions.values]], 'Predictions': [predictions.values], 'Labels:': [context.data_container.y_test]})
results = zip(context.data_container.y_test, prediction.values)
for result in results:
print(f'Label: {result[0]} Prediction: {result[1]}')
# Plot outputs
if plot:
vis = Visualizer(context.config.visualization_config)
# The ROC curve is for 1 class only, so we'll plot each class separately
for name, prediction in predictions.items():
fpr, tpr, th = roc_curve(context.data_container.y_test, prediction.values)
roc_auc = auc(fpr, tpr)
vis.plot_roc_curve(fpr, tpr, roc_auc, label=name)
vis.show()
if __name__ == '__main__':
args = ('p', 'true')
main(args)