Skip to content

Commit

Permalink
Make confusion_matrix and roc generic (#1285)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffwan authored and k8s-ci-robot committed May 8, 2019
1 parent 22620b5 commit cb87b94
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
7 changes: 4 additions & 3 deletions components/local/confusion_matrix/src/confusion_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import argparse
import json
import os
import urlparse
import pandas as pd
from sklearn.metrics import confusion_matrix, accuracy_score
from tensorflow.python.lib.io import file_io
Expand All @@ -40,7 +41,8 @@ def main(argv=None):
'If not set, the input must include a "target" column.')
args = parser.parse_args()

on_cloud = args.output.startswith('gs://')
storage_service_scheme = urlparse.urlparse(args.output).scheme
on_cloud = True if storage_service_scheme else False
if not on_cloud and not os.path.exists(args.output):
os.makedirs(args.output)

Expand All @@ -52,7 +54,7 @@ def main(argv=None):
for file in files:
with file_io.FileIO(file, 'r') as f:
dfs.append(pd.read_csv(f, names=names))

df = pd.concat(dfs)
if args.target_lambda:
df['target'] = df.apply(eval(args.target_lambda), axis=1)
Expand All @@ -72,7 +74,6 @@ def main(argv=None):
metadata = {
'outputs' : [{
'type': 'confusion_matrix',
'storage': 'gcs',
'format': 'csv',
'schema': [
{'name': 'target', 'type': 'CATEGORY'},
Expand Down
9 changes: 5 additions & 4 deletions components/local/roc/src/roc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import argparse
import json
import os
import urlparse
import pandas as pd
from sklearn.metrics import roc_curve, roc_auc_score
from tensorflow.python.lib.io import file_io
Expand All @@ -45,7 +46,8 @@ def main(argv=None):
parser.add_argument('--output', type=str, help='GCS path of the output directory.')
args = parser.parse_args()

on_cloud = args.output.startswith('gs://')
storage_service_scheme = urlparse.urlparse(args.output).scheme
on_cloud = True if storage_service_scheme else False
if not on_cloud and not os.path.exists(args.output):
os.makedirs(args.output)

Expand All @@ -64,7 +66,7 @@ def main(argv=None):
for file in files:
with file_io.FileIO(file, 'r') as f:
dfs.append(pd.read_csv(f, names=names))

df = pd.concat(dfs)
if args.target_lambda:
df['target'] = df.apply(eval(args.target_lambda), axis=1)
Expand All @@ -76,11 +78,10 @@ def main(argv=None):
roc_file = os.path.join(args.output, 'roc.csv')
with file_io.FileIO(roc_file, 'w') as f:
df_roc.to_csv(f, columns=['fpr', 'tpr', 'thresholds'], header=False, index=False)

metadata = {
'outputs': [{
'type': 'roc',
'storage': 'gcs',
'format': 'csv',
'schema': [
{'name': 'fpr', 'type': 'NUMBER'},
Expand Down

0 comments on commit cb87b94

Please sign in to comment.