Skip to content

Commit

Permalink
Numpy always faster in real-world use
Browse files Browse the repository at this point in the history
  • Loading branch information
smmaurer committed Sep 5, 2018
1 parent 9abd49f commit 0b8a2b9
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions choicemodels/tools/mergedchoicetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,21 +325,12 @@ def _build_table(self):
# replacement, with optional alternative-specific weights but NOT weights that
# apply to combinations of observation x alternative

# No weights
if (self.replace == True) & (self.weights is None):

if (sys.version_info[0] >= 3) & (sys.version_info[1] >= 6):
# 'random.choices' was added in python 3.6, and in my testing it's about
# 2x faster than 'np.random.choice' for unweighted sampling without
# replacement, as of numpy 1.13
alt_ids = random.choices(self.alternatives.index.values,
k = n_obs * samp_size)
else:
alt_ids = np.random.choice(self.alternatives.index.values,
replace = True,
size = n_obs * samp_size)
alt_ids = np.random.choice(self.alternatives.index.values,
replace = True,
size = n_obs * samp_size)

# Alternative-specific weights: numpy is most efficient
elif (self.replace == True) & (self.weights_1d == True):

alt_ids = np.random.choice(self.alternatives.index.values,
Expand Down

0 comments on commit 0b8a2b9

Please sign in to comment.