forked from aws/amazon-sagemaker-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_data.py
59 lines (50 loc) · 1.78 KB
/
prepare_data.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
#!/usr/bin/env python
import os
import pandas as pd
from glob import glob
os.system("du -a /opt/ml")
SRCTRAINFILE = glob("/opt/ml/processing/input_train/*.csv")[0]
print(SRCTRAINFILE)
SRCTESTFILE = glob("/opt/ml/processing/input_test/*.csv")[0]
print(SRCTESTFILE)
DSTTRAINFILE = "/opt/ml/processing/train/train.csv"
DSTTESTFILE = "/opt/ml/processing/test/test.csv"
# Preparation of the train set
trainFrame = pd.read_csv(SRCTRAINFILE, header=None)
testFrame = pd.read_csv(SRCTESTFILE, header=None)
# AWS recommends that you train an Amazon Comprehend model with at least 50 training documents for
# each class. See: https://docs.aws.amazon.com/comprehend/latest/dg/how-document-classification-training-data.html
#
# The dataset we use has 100,000 documents per class. To limit the costs and training times of this demo,
# we will limit it to 1000 documents per class
#
# If you want to test Amazon Comprehend on the full dataset, set MAXITEM to 100000
MAXITEM = 1000
# Keeping MAXITEM for each label
for i in trainFrame[0].unique():
num = len(trainFrame[trainFrame[0] == i])
dropnum = max(0, num - MAXITEM)
indextodrop = trainFrame[trainFrame[0] == i].sample(n=dropnum).index
trainFrame.drop(indextodrop, inplace=True)
# Escaping commas in preparation to write the data to a CSV file
trainFrame[1] = trainFrame[1].str.replace(",", ",")
# Writing csv file
trainFrame.to_csv(
path_or_buf=DSTTRAINFILE,
header=False,
index=False,
escapechar="\\",
doublequote=False,
quotechar='"',
)
# Escaping commas in preparation to write the data to a CSV file
testFrame[0] = testFrame[0].str.replace(",", ",")
# Writing csv file
testFrame.to_csv(
path_or_buf=DSTTESTFILE,
header=False,
index=False,
escapechar="\\",
doublequote=False,
quotechar='"',
)