text stringlengths 1 93.6k |
|---|
# Inputs:
|
# mat (np.array): array of unicode strings to convert
|
# convertDicts (list): dictionary for each column
|
# counts (list): number of different categories in each column
|
#
|
# Outputs:
|
# out (np.array): array of output integers
|
# convertDicts (list): dictionary for each column
|
# counts (list): number of different categories in each column
|
# check if convertDicts and counts match correct length of mat
|
if len(convertDicts) != mat.shape[1] or len(counts) != mat.shape[1]:
|
print("Length of convertDicts or counts does not match input shape")
|
print("Generating convertDicts and counts...")
|
convertDicts = [{} for _ in range(mat.shape[1])]
|
counts = [0 for _ in range(mat.shape[1])]
|
# initialize output
|
out = np.zeros(mat.shape)
|
for j in range(mat.shape[1]):
|
for i in range(mat.shape[0]):
|
# add to convertDict and increment count
|
if mat[i, j] not in convertDicts[j]:
|
convertDicts[j][mat[i, j]] = counts[j]
|
counts[j] += 1
|
out[i, j] = convertDicts[j][mat[i, j]]
|
return out, convertDicts, counts
|
def convertUStringToDistinctIntsUnique(mat, mat_uni, counts):
|
# mat is an array of 0,...,# samples, with each being 26 categorical features
|
# check if mat_unique and counts match correct length of mat
|
if len(mat_uni) != mat.shape[1] or len(counts) != mat.shape[1]:
|
print("Length of mat_unique or counts does not match input shape")
|
print("Generating mat_unique and counts...")
|
mat_uni = [np.array([]) for _ in range(mat.shape[1])]
|
counts = [0 for _ in range(mat.shape[1])]
|
# initialize output
|
out = np.zeros(mat.shape)
|
ind_map = [np.array([]) for _ in range(mat.shape[1])]
|
# find out and assign unique ids to features
|
for j in range(mat.shape[1]):
|
m = mat_uni[j].size
|
mat_concat = np.concatenate((mat_uni[j], mat[:, j]))
|
mat_uni[j], ind_map[j] = np.unique(mat_concat, return_inverse=True)
|
out[:, j] = ind_map[j][m:]
|
counts[j] = mat_uni[j].size
|
return out, mat_uni, counts
|
def processCriteoAdData(d_path, d_file, npzfile, split, convertDicts, pre_comp_counts):
|
# Process Kaggle Display Advertising Challenge or Terabyte Dataset
|
# by converting unicode strings in X_cat to integers and
|
# converting negative integer values in X_int.
|
#
|
# Loads data in the form "{kaggle|terabyte}_day_i.npz" where i is the day.
|
#
|
# Inputs:
|
# d_path (str): path for {kaggle|terabyte}_day_i.npz files
|
# split (int): total number of splits in the dataset (typically 7 or 24)
|
# process data if not all files exist
|
for i in range(split):
|
filename_i = npzfile + "_{0}_processed.npz".format(i)
|
if path.exists(filename_i):
|
print("Using existing " + filename_i, end="\r")
|
else:
|
with np.load(npzfile + "_{0}.npz".format(i)) as data:
|
# categorical features
|
'''
|
# Approach 1a: using empty dictionaries
|
X_cat, convertDicts, counts = convertUStringToDistinctIntsDict(
|
data["X_cat"], convertDicts, counts
|
)
|
'''
|
'''
|
# Approach 1b: using empty np.unique
|
X_cat, convertDicts, counts = convertUStringToDistinctIntsUnique(
|
data["X_cat"], convertDicts, counts
|
)
|
'''
|
# Approach 2a: using pre-computed dictionaries
|
X_cat_t = np.zeros(data["X_cat_t"].shape)
|
for j in range(26):
|
for k, x in enumerate(data["X_cat_t"][j, :]):
|
X_cat_t[j, k] = convertDicts[j][x]
|
# continuous features
|
X_int = data["X_int"]
|
X_int[X_int < 0] = 0
|
# targets
|
y = data["y"]
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.