Skip to content

Instantly share code, notes, and snippets.

@chrisking
Created September 17, 2018 12:26
Show Gist options
  • Select an option

  • Save chrisking/ee2db702c0081469d2dd5f4ded500602 to your computer and use it in GitHub Desktop.

Select an option

Save chrisking/ee2db702c0081469d2dd5f4ded500602 to your computer and use it in GitHub Desktop.
DataScience Help
# First we need a data structure of a datframe with 3 columns ( visitID, landing_page, and converted )
# Then we need to run a probability loop for 10,000 iterations with a 50/50 split getting old or new page, and their respective
# probabilities of converting
# Borrowed this because it worked well
# https://stackoverflow.com/questions/439115/random-decimal-in-python
import decimal
def gen_random_decimal(i,d):
return decimal.Decimal('%d.%d' % (random.randint(0,i),random.randint(0,d)))
# First create the dataframe tol hold the data
simulated_df_nnew = pd.DataFrame(columns=['user_id', 'landing_page', 'converted'])
# Create a dictionary to store the results:
dict = {}
# Now loop to fill the dictionary
for i in range(nnew):
# Get a random probability to see if the page converts
random_conversion = gen_random_decimal(0,100000000)
#print(simulated_df_nnew)
# Set to new page:
landing_page = "new_page"
# Set default to did not covert:
conversion = 0
# Compare to determine conversion
if random_conversion >= (1.0 - convert_rate_pnew):
conversion = 1
# Append the results to the dictionary
dict[i] = {"user_id": i, "landing_page":landing_page, "conversion": conversion}
# Finally build the dataframe from the dictionary
simulated_df_nnew = pd.DataFrame.from_dict(dict, "index")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment