Skip to content

Add Non-numeric data check #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/sagemaker_xgboost_container/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import csv
import logging
import os
import re

import mlio
from mlio.integ.arrow import as_arrow_file
Expand Down Expand Up @@ -133,6 +134,16 @@ def _get_csv_delimiter(sample_csv_line):
return delimiter


def _validate_numeric_characters(sample_csv_line, file_path):
match = re.search('[a-df-zA-DF-Z]', sample_csv_line)
if match is not None:
raise exc.UserError("Non-numeric value '{}' found in the header line '{}...' of file '{}'."
"CSV format requires no header in it. If the header line is already removed,"
"XGBoost does not support non-numeric values in the data.".format(match.group(0),
sample_csv_line[:50],
file_path.split('/')[-1]))


def _get_num_valid_libsvm_features(libsvm_line):
"""Get number of valid LIBSVM features.

Expand Down Expand Up @@ -199,6 +210,7 @@ def _validate_csv_format(file_path):
with open(file_path, 'r', errors='ignore') as read_file:
line_to_validate = read_file.readline()
_get_csv_delimiter(line_to_validate)
_validate_numeric_characters(line_to_validate, file_path)


def _validate_libsvm_format(file_path):
Expand Down
5 changes: 5 additions & 0 deletions test/resources/data/csv/train_nonnumeric.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
a,1,0,0,d,0
0,1,0,0,0,0
0,1,0,0,0,0
0,1,0,0,0,0
1,0,1,0,0,0
7 changes: 5 additions & 2 deletions test/unit/test_data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@ def test_get_content_type(self):
data_utils.get_content_type('label_size=1; text/csv')

def test_validate_csv_files(self):
csv_file_paths = ['train.csv', 'train.csv.weights', 'csv_files']
csv_file_paths = ['train.csv', 'train_nonnumeric.csv', 'train.csv.weights', 'csv_files']

for file_path in csv_file_paths:
with self.subTest(file_path=file_path):
csv_path = os.path.join(self.data_path, 'csv', file_path)
data_utils.validate_data_file_path(csv_path, 'csv')
if file_path == 'train_nonnumeric.csv':
self.assertRaises(exc.UserError, data_utils.validate_data_file_path, *{csv_path, 'csv'})
else:
data_utils.validate_data_file_path(csv_path, 'csv')

def test_validate_libsvm_files(self):
libsvm_file_paths = ['train.libsvm', 'train.libsvm.weights', 'libsvm_files']
Expand Down